SurfaceTexture preview for camera2 stretches on some devices

I am developing a camera2 application. Everything works fine on Nexus 5 and Nexus 4, but on LG G2 the preview for video is stretched (only for the rear camera, for the front camera everything is in order). I install SurfaceTexture as follows:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        float ratioSurface = width > height ? (float) width / height : (float) height / width;
        float ratioPreview = (float) mRatioWidth / mRatioHeight;
        int scaledHeight = height;
        int scaledWidth = width;
        if (ratioPreview > ratioSurface) {
            scaledHeight = (int) (((float) mRatioWidth / mRatioHeight) * width);
        } else if (ratioPreview < ratioSurface) {
            scaledWidth = (int) (height / ((float) mRatioWidth / mRatioHeight));
        }
        setMeasuredDimension(scaledWidth, scaledHeight);
    }

After setAspectRatio (), they call me setSurfaceTexture ():

private void setSurfaceTexture(Size size) {
        final Matrix matrix = new Matrix();
        int width = resource.cameraTexture.getWidth();
        int height = resource.cameraTexture.getHeight();
        int rotation = controller.getRotation();
        if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            matrix.postRotate(90 * (rotation - 2), width / 2, height / 2);
        }
        resource.mForegroundHandler.post(new Runnable() {
            @Override
            public void run() {
                resource.cameraTexture.setTransform(matrix);
            }
        });
    }

I tried to use SurfaceView, but the result is the same: when switching to video mode, the preview is stretched. I tried many ways, but the result is always the same. Please help me...

EDIT: - LG G2 CyanogenMod, Lollipop : LEGACY 16: 9, 4: 3.

+4

Source: https://habr.com/ru/post/1599544/


All Articles