首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Camera2 API问题

Camera2 API问题
EN

Stack Overflow用户
提问于 2018-12-28 10:54:37
回答 1查看 1.8K关注 0票数 1

我让一些相机应用程序使用camera2 APi示例camera2 api google示例,所以我的相机输出没有全屏。

然后,我从camera2 api isue中读到,我必须在AUtofitTextureView类中修改它:

代码语言:javascript
复制
if (width < height * mRatioWidth / mRatioHeight) {

从<到>,如下所示:

代码语言:javascript
复制
 if (width > height * mRatioWidth / mRatioHeight) {

然后我的相机视图会修正到全屏

但现在我有了另外一个问题,我的捕捉图像比我的预览大。

请检查我的应用程序的屏幕截图。

  1. 我的相机预览没有全屏

  1. 如果相机预览没有全屏,我的捕获图像

  1. 我的相机在Meisure方法上改变后的全屏预览

  1. 我的拍摄图像从相机预览后,改变了Meisure方法。

问题是让我的拍摄图像与我的相机预览不一样。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-17 14:05:07

我尝试了以下方法,这里是我的代码,用于维护16:9,18:9,19:9的纵横比。希望这个答案会有所帮助,基于设备分辨率的选择ArrayList有最大到最小的分辨率,这就是我解决预览拉伸问题的方法。

代码语言:javascript
复制
//Samsung-S6-choices[0]
//Samsung-S7-edge-choices[6]
//OnePlus-5T-choices[15]
/*
 * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose
 * width and height are at least as large as the respective requested values, and whose aspect
 * ratio matches with the specified value.
 *
 * @param choices     The list of sizes that the camera supports for the intended output class
 * @param width       The minimum desired width
 * @param height      The minimum desired height
 * @param aspectRatio The aspect ratio
 * @return The optimal {@code Size}, or an arbitrary one if none were big enough
 */
private  Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
    // Collect the supported resolutions that are at least as big as the preview Surface
    List<Size> bigEnough = new ArrayList<>();
    int w = aspectRatio.getWidth();
    int h = aspectRatio.getHeight();
    double ratio = (double) h / w;
    int loopCounter=0;
    for (Size size : choices) {
        int orientation = getActivityContext.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=720) {
            //if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=3840 ) {
            //if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=5120 ) {//Retina 5K
            if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=7680 ) {//8K UHDTV Super Hi-Vision
                Log.e(TAG1, "chooseOptimalSize:"+size);
                return size;
            }
        } else {
            Log.e(TAG1, "chooseOptimalSize:--given--"+size);
            DisplayMetrics metrics = getActivityContext.getResources().getDisplayMetrics();
            float mCameraAspectRatio = ((float)metrics.heightPixels / (float)metrics.widthPixels);
            Log.e(TAG1, "chooseOptimalSize:--AspectRatio--"+mCameraAspectRatio);
            if((size.getWidth()/16) == (size.getHeight()/9) && ((size.getWidth() <=1280)||(size.getHeight()<=1920))) {
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=4320 ) ) {//8K UHDTV Super Hi-Vision
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=2880 ) ) {//Retina 5K
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=2160 ) ) {
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=1280 ) ) {
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=4480 && size.getWidth() >=1280) ) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-16:9");
                return size;
            }else if((size.getWidth()/18) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-18:9");
                return size;
            }else if((size.getWidth()/18.5) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-18.5:9");
                return size;
            }else if((size.getWidth()/19) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-19:9");
                return size;
            }else if((size.getWidth()/19.5) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-19.5:9");
                return size;
            }else{
                Log.e(TAG1, "chooseOptimalSize"+" not proper aspect resolution");
                Log.e(TAG1, "chooseOptimalSize"+" ---=>Width---=>"+size.getWidth());
                Log.e(TAG1, "chooseOptimalSize"+" ---=>Height---=>"+size.getHeight());
            }
            //2340
        }

        if(screenWidth==size.getWidth()){
            Log.e(TAG1, loopCounter+".choose:width Matched:"+screenWidth+"="+size.getWidth());
        }else{
            Log.e(TAG1, loopCounter+".choose:width Not Matched:"+screenWidth+"="+size.getWidth());
        }

        if(screenHeight==size.getHeight()){
            Log.e(TAG1, loopCounter+".choose:height Matched:"+screenHeight+"="+size.getHeight());
        }else{
            Log.e(TAG1, loopCounter+".choose:height Not Matched:"+screenHeight+"="+size.getHeight());
        }
        loopCounter++;
    }
    // Pick the smallest of those, assuming we found any
    if (bigEnough.size() > 0) {
        return Collections.min(bigEnough, new CompareSizesByArea());
    } else {
        Log.e(TAG1, "Couldn't find any suitable preview size");
        return choices[0];
    }
}


/*
 * Compares two {@code Size}s based on their areas.
 */
static class CompareSizesByArea implements Comparator<Size> {
    @Override
    public int compare(Size lhs, Size rhs) {
        // We cast here to ensure the multiplications won't overflow
        return Long.signum((long) lhs.getWidth() * lhs.getHeight() -
                (long) rhs.getWidth() * rhs.getHeight());
    }
}

希望能有所帮助,如有任何问题,请留下评论。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53957388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档