The camera preview interface image stretch processing, by changing the display window size to adapt to the camera preview size

1. Preface
The control that displays the frame: SurfaceView.
Control size ratio: 1:1 (square size is more suitable for face recognition)
Display window size: not full screen
2. Stretching reason The
camera preview size is read from the manufacturer list

Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();//获取预览的各种分辨率

Get the best size (source code of google developer)

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio;
    if (w > h) {
        targetRatio = (double) w / h;  /* w / h : 横向   h / w :竖向*/
    } else {
        targetRatio = (double) h / w;  /* w / h : 横向   h / w :竖向*/
    }
    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    Log.e(TAG, "finally choose size:optimalSize width=" + optimalSize.width + "   optimalSize height=" + optimalSize.height);
    return optimalSize;

}

It is completely called the source code of google, and has not changed.
So, what happens if the aspect ratio of all sizes in the read list does not satisfy our window?
Of course it is the deformation of the preview interface and the ratio is not satisfied. The selected size may be larger than what we expected. For example,
the size of our preview window is: 800x800, and the selected camera preview size is: 1280x720, which is larger than the window size. Compress and stretch when small to make up for the lack of pixels.
3. The solution is to dynamically change the window size to adapt to the camera preview size.
Taking the above size as an example, the square preview window does not get a 1:1 size on some mobile phones.
At this time, you need to adjust your own window ratio. If you don't want to be too different from the original window size, choose 4:3 ratio (the specific situation needs to be determined according to your own project).
800x800 window, after adjustment, 800x600

private boolean surfaceChanged = true;

Define a global variable to control whether the surfaceView needs to be adjusted. If you don’t need it, you don’t need to add it, but you should note that surfaceChanged will be called invisibly. If there is an adjustment method in the rewrite method of SurfaceView, you must do it, otherwise it will Endless loop.

double ratioSurface = (double) surfaceView.getWidth() / (double) surfaceView.getHeight();
double ratioCamera = (double) optionSize.width / (double) optionSize.height;
Log.v(TAG, "ratioSurface:" + ratioSurface + "    ratioCamera:" + ratioCamera);
if (surfaceChanged && ratioCamera != ratioSurface) {
   RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(600, 800);//这里根据自己的需要,是竖屏还是横屏,我们是竖屏,所以选择了宽高对换了一下。
   layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);//这一行是让控件在父控件完全居中
   surfaceView.setLayoutParams(layoutParams);
   surfaceChanged = false;
}

Where is the above code added?

 mCamera.setParameters(parameters);

It can be done before parameter setting.
4. Possible problems
A. Picture rotation: In
this case, you need to manually adjust the camera by yourself

PS: Since this was written when the problem was solved in the project, it is not convenient to upload the source code, but the source code is estimated to be not portable.
Thanks for coming.

Guess you like

Origin blog.csdn.net/mozushixin_1/article/details/93997938