具有 Camera Preview 宽高比的倾斜图像

Skew image with aspect ratio of Camera Preview

我在相机预览时遇到图像倾斜问题。

我的目标是在其容器(FrameLayout)上缩放和填充相机预览

我很困惑,当我 setPreviewSize() 作为 16:9 并且当我尝试通过将其大小设置为 FrameLayout 来拉伸图像时=44=](此视图可能大于显示屏幕尺寸以填满整个屏幕区域)相机预览 上的图像开始倾斜。

但是,如果我 setPreviewSize() 为 16:9 并且 FrameLayout 为 4:3 图像在相机预览中根本不会倾斜。

Ex.Secenario #1 图像倾斜

preview size -> w: 1920 h: 1080 (16:9) 

display size -> w: 1774 h:1080 (887:540) 

container size -> w: 1920 h: 1080 (16:9)

Ex.Secenario #2 图像不倾斜

preview size -> w: 1920 h: 1080 (16:9)

display size -> w: 1774 h:1080 (887:540)

container size -> w: 1800 h: 1350 (4:3)

那么,相机预览的纵横比和它的容器(FrameLayout)之间的关系是什么,我现在很困惑为什么16:9 预览大小与 16:9 的容器大小不兼容。或者我想念一些东西请指教。

我发现意外行为来自不包含以下代码.. surfaceChanged()

@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // Before do any configuration to camera
        // StopPreview() and StartPreview()
        // after finish it

        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }

}

Important: Some camera features cannot be changed at will. In particular, changing the size or orientation of the camera preview requires that you first stop the preview, change the preview size, and then restart the preview. Starting with Android 4.0 (API Level 14) preview orientation can be changed without restarting the preview.

我只是误解了 android 文档中的这条重要说明。因此,如果您使用停止然后配置您的相机预览分辨率或将您的父布局调整为与相机预览的纵横比相同。开始预览后,不会出现歪斜图片预览。