创建全屏幕相机表面视图没有拉伸
问题描述:
我正在创建我的相机表面,但横向和纵向两个预览都拉伸像图像给出如下。请帮我设置正确的参数。创建全屏幕相机表面视图没有拉伸
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;
}
希望这有助于。