Android Camera2 API stretching the preview

让人想犯罪 __ 提交于 2021-02-04 21:24:08

问题


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();
}

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 ?


回答1:


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

previously--->

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

modified--->

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

previously--->

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

modified---->

  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;
    }


来源:https://stackoverflow.com/questions/41752733/android-camera2-api-stretching-the-preview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!