我对SurfaceView和相机预览的问题很着迷。我的问题是预览(在portrati模式下)被拉伸了,我不明白为什么(在nexus 7设备上)。
这是包含surfaceview的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".pic" >

    <SurfaceView
        android:id="@+id/surfaceview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <View
        android:id="@+id/bottom_border"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true" />
    <!-- android:layout_below="@+id/surfaceview" -->

    <ImageView
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:scaleType="fitXY"
        android:src="@drawable/footer" />



 </RelativeLayout>

因此,页脚imageview包含一个页脚和一些其他视图(拍照按钮、接受或拒绝拍照按钮等),view(以编程方式初始化)是一个半透明视图,显示用户的图片限制(拍照将是正方形)。
这就是我初始化相机对象的方法(surfacechanged方法)
Camera.Parameters params = mCamera.getParameters();
    Camera.Size result = getBestPreviewSize(params, width, height);

    params.setPreviewSize(result.width, result.height);
    params.setPictureFormat(ImageFormat.JPEG);
    params.setJpegQuality(100);
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    // params.set
    params.setPictureSize(dpWidth, dpWidth);
    // default orientation is in landscape
    params.setRotation(90);
    mCamera.setParameters(params);

    mCamera.startPreview();

这就是我计算最佳预览大小的方法:
public Camera.Size getBestPreviewSize(Camera.Parameters params, int width,
        int height) {

    Camera.Size result = null;
    for (Camera.Size size : params.getSupportedPreviewSizes()) {
        if (size.width <= width && size.height <= height) {
            if (result == null) {
                result = size;
            } else {
                int resultArea = result.width * result.height;
                int newArea = size.width * size.height;

                if (newArea > resultArea) {
                    result = size;
                }
            }
        }
    }
    return result;
}

支持的预览大小为
w: 1920 h: 1080
w: 1280 h: 768
w: 1280 h: 720
w: 1024 h: 768
w: 800 h: 600
w: 800 h: 480
w: 720 h: 480
w: 640 h: 480
w: 352 h: 288
w: 320 h: 240
w: 176 h: 144

被选中的一个是
1024 768

预览时的图像比实际图像更薄、更高。
我怎么能修好它?

最佳答案

正在缩放预览以匹配SurfaceView的大小。您需要使用可以调整以保持相机输入的纵横比的布局包装surfaceview。
其中一个例子是AspectFrameLayout中的Grafika类。您可以在显示相机预览的活动中看到它(例如“连续拍摄”和“显示+拍摄相机”)。您的布局将如下所示:

<com.android.grafika.AspectFrameLayout
    android:id="@+id/continuousCapture_afl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" >

    <SurfaceView
        android:id="@+id/continuousCapture_surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />
</com.android.grafika.AspectFrameLayout>

然后根据相机参数设置大小:
    Camera.Size cameraPreviewSize = parms.getPreviewSize();
    AspectFrameLayout layout = (AspectFrameLayout) findViewById(R.id.continuousCapture_afl);
    layout.setAspectRatio((double) cameraPreviewSize.width / cameraPreviewSize.height);

10-08 08:14
查看更多