本文介绍了Android Camera2 API 拉伸预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Google 示例项目,但如果不拉伸它,我似乎无法使预览正常工作.

I am working with the Google sample project, but I cannot seem to get the preview to work without stretching it.

public void setAspectRatio(int width, int height) {
     if (width < 0 || height < 0)
     {
        throw new IllegalArgumentException("Size cannot be negative.");
     }
     mRatioWidth =  width;
     mRatioHeight = height;
     requestLayout();
}

我已经尝试在 AutoFitTextureView 类上物理更改纵横比,这使其全屏显示,但导致它拉伸.

I have tried physically changing the aspect ration on the AutoFitTextureView class, this makes it full screen, but causes it to stretch.

有没有人想出一个成功的实现?

Has anyone figured out a successful implementation of this ?

推荐答案

需要修改setUpCameraOutputs方法.修改下面一行

you need to modify the setUpCameraOutputs method. Modify the following line

以前--->

   Size largest = Collections.max(
                        Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                        new CompareSizesByArea());

修改--->

largest =getFullScreenPreview(map.getOutputSizes(ImageFormat.JPEG),width,height);

以前--->

mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                    rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                    maxPreviewHeight, largest);

修改---->

  mPreviewSize = getFullScreenPreview(map.getOutputSizes(SurfaceTexture.class),
                    width, height);

获取全屏预览的方法如下-

and method for getting fullscreen preview is as follows-

 private Size getFullScreenPreview(Size[] outputSizes, int width, int height) {

        List<Size> outputSizeList = Arrays.asList(outputSizes);
        outputSizeList = sortListInDescendingOrder(outputSizeList); //because in some phones available list is in ascending order
        Size fullScreenSize = outputSizeList.get(0);
        for (int i = 0; i < outputSizeList.size(); i++) {
            int orginalWidth = outputSizeList.get(i).getWidth();
            int orginalHeight = outputSizeList.get(i).getHeight();
            float orginalRatio = (float) orginalWidth / (float) orginalHeight;
            float requiredRatio;
            if (width > height) {
                requiredRatio = ((float) width / height); //for landscape mode
                if ((outputSizeList.get(i).getWidth() > width && outputSizeList.get(i).getHeight() > height)) {
                    // because if we select preview size hire than device display resolution it may fail to create capture request
                    continue;
                }
            } else {
                requiredRatio = 1 / ((float) width / height); //for portrait mode
                if ((outputSizeList.get(i).getWidth() > height && outputSizeList.get(i).getHeight() > width)) {
                    // because if we select preview size hire than device display resolution it may fail to create capture request
                    continue;
                }
            }
            if (orginalRatio == requiredRatio) {
                fullScreenSize = outputSizeList.get(i);
                break;
            }
        }
        return fullScreenSize;
    }

这篇关于Android Camera2 API 拉伸预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 06:13