SurfaceView相机预览平方:避免图像失真

时间:2014-04-10 09:30:54

标签: android camera surfaceview

我有后续场景:方形的表面视图(在活力之上)和下面的绿色视图(我将在其他视图中填充未来)。现在我想在SurfaceView中预览相机,它是平方的,因为我需要拍摄平方图片,但预览失真(可能是因为我给了预览尺寸不平方)。我给你看了代码。

我如何计算运行时的方形尺寸并将尺寸分配给绿色视图

surfaceView = (SurfaceView) findViewById(R.id.surfaceview);

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
dpHeight = displayMetrics.heightPixels;
dpWidth = displayMetrics.widthPixels;
surfaceViewDimension = dpWidth;
int borderHeight = dpHeight - dpWidth;

View bottomBorder = findViewById(R.id.bottom_border);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, borderHeight);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

bottomBorder.setLayoutParams(params);

bottomBorder.setBackgroundColor(Color.GREEN);

这就是我设置相机选项的方式:

@Override
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.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 个答案:

没有答案