不要在纵向模式下在SurfaceView中拉伸相机预览

时间:2014-04-16 11:17:19

标签: android android-camera surfaceview

我的代码所做的是从相机拍摄图像并在全屏显示在surfaceview内,但预览向上拉伸(因此像瓶子这样的物体看起来比实际更高更薄)。这是我的代码:

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    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.setPictureSize(dpWidth, dpWidth);
    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()) {
        Log.d("size: ", size.width + ":" + size.height);
        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;
}

出了什么问题?

0 个答案:

没有答案