创建全屏相机surfaceview而不拉伸

时间:2014-09-25 10:17:18

标签: android

我正在创建我的相机表面但横向和纵向两个预览都被拉伸如下图所示。请帮我设置正确的参数。

Snapshot of camera surface

getWindow().setFormat(PixelFormat.UNKNOWN);
           surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
           surfaceHolder = surfaceView.getHolder();
           surfaceHolder.addCallback(this);
           surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


camera = Camera.open();

camera.setPreviewDisplay(surfaceHolder);

1 个答案:

答案 0 :(得分:1)

您可以将适当的Matrix应用于SurfaceView,我使用以下方法来修复我的图片:

// Adjustment for orientation of images
public static Matrix adjustOrientation(SurfaceView preview) {
    Matrix matrix = new Matrix(preview.getMatrix());
    try {
        ExifInterface exifReader = new ExifInterface(path);

        int orientation = exifReader.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, -1);

        if (orientation == ExifInterface.ORIENTATION_NORMAL) {
            // do nothing
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return matrix;
}

这会获得Bitmap权利的方向。然后,您可以通过以下方法根据宽度和高度重新缩放Bitmap

public static Bitmap getScaledBitmap(int bound_width, int bound_height, String path) {

    int new_width;
    int new_height;
    Bitmap original_img;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    original_img = BitmapFactory.decodeFile(path, options);
    int original_width = options.outWidth;
    int original_height = options.outHeight;

    new_width = original_width;
    new_height = original_height;

    // check if we need to scale the width
    if (original_width > bound_width) {
        // scale width to fit
        new_width = bound_width;
        // scale height to maintain aspect ratio
        new_height = (new_width * original_height) / original_width;
    }

    // check if we need to scale even with the new height
    if (new_height > bound_height) {
        // scale height to fit instead
        new_height = bound_height;
        // adjust width, keep in mind the aspect ratio
        new_width = (new_height * original_width) / original_height;
    }

    Bitmap originalBitmap = BitmapFactory.decodeFile(path);
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, new_width, new_height, true);
    return resizedBitmap;
}

希望这有帮助。