简体   繁体   中英

Create full screen camera surfaceview without stretched

I am creating my camera surface but landscape and portrait both preview are stretched Like image are given below. Please help me for set correct parameters.

相机表面快照

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);

You could apply the appropriate Matrix to the SurfaceView , I used the following method to fixate my images:

// 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;
}

This gets the orientation of the Bitmap right. You could then re-scale the Bitmap according to your width and height by the following method:

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;
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM