Camera2的SurfaceTexture预览在某些设备上延伸

Gra*_*sie 5 android android-layout android-camera

我正在开发一个camera2应用程序.在Nexus 5和Nexus 4上一切正常,但在LG G2预览视频是拉伸的(仅适用于后置摄像头,前置摄像头一切都很好).我正在设置SurfaceTexture:

@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);
    }
Run Code Online (Sandbox Code Playgroud)

在setAspectRatio()之后,我称之为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);
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

我试图使用SurfaceView,但结果是一样的:当切换到视频模式时,预览会被拉伸.我尝试了很多方法,但结果总是一样的.请帮我...

编辑:使用CyanogenMod,LG G2的视频仍在延伸,但在官方Lollipop的手机中,下一个问题是:LEGACY设备并不总是支持预览视频16:9,因此我将视频大小更改为4:3.