android - 创建无拉伸(stretch)的全屏相机表面 View

标签 android

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

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

最佳答案

您可以将适当的 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;
}

希望这对您有所帮助。

关于android - 创建无拉伸(stretch)的全屏相机表面 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26036055/

相关文章:

Android:重心的Edittext在设备上不起作用

android - 如果全屏,Webview 不显示进度条

c# - 找不到与给定名称匹配的资源

android - 将 fragment 添加到对话框 fragment

android - ListView 内的 CheckedTextView : blinking issue upon selection

android - "Use Facebook Login"SDK编译错误

android - 在 PreferenceActivity 中找不到自定义首选项的首选项

android - 软键盘不存在,无法隐藏键盘 - Appium android

java - 我按照教程编写了一个 witre/read android 应用程序,但为了我的目的更改了一些内容。应用程序一启动就崩溃

android - Gradle 在 linux box 上构建失败,出现 "Malformed input or input contains unmappable characters"