我有一个应用程序,它以人像模式拍摄照片,并且一切正常,除了预览的大小与拍照后的图像大小不同。预览似乎失真且模糊,但充满了整个屏幕。拍照后,图像清晰,但比设备高度短或宽度更小(取决于所使用的设备)。
下面是一些图像示例和一些我正在使用的代码。
相机预览代码以纵向显示:
protected int getRotationDegrees() {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(0, info);
int rotation = ((WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result = (info.orientation - degrees + 360) % 360;
return result;
}
拍照后旋转图像的代码:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap pictureBitmap = null;
pictureBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int rotationDegrees = mCameraPreview.getRotationDegrees();
//mRotation = setCameraDisplayOrientation(MainActivity.this, Camera.CameraInfo.CAMERA_FACING_BACK, mCamera);
Matrix matrix = new Matrix();
matrix.postRotate(rotationDegrees);
//Bitmap scaledBitmap = Bitmap.createScaledBitmap(pictureBitmap ,previewWidth ,previewHeight ,true);
//Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
Bitmap rotatedBitmap = Bitmap.createBitmap(pictureBitmap , 0, 0, pictureBitmap.getWidth(), pictureBitmap.getHeight(), matrix, true);
//rotatedBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
ImageView previewImage = (ImageView) findViewById(R.id.preview_image);
previewImage.setImageBitmap(rotatedBitmap);
mCameraPreview.setVisibility(View.INVISIBLE);
mButtonCapture.setVisibility(View.INVISIBLE);
//flCameraPreview.setVisibility(View.INVISIBLE);
}
};
有人遇到过这个问题吗?我仍在尝试了解相机API。提前致谢!
编辑:我用来选择预览和图片大小的代码。
private Camera.Size getOptimalSize(List<Camera.Size> sizes, int h, int w) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w/h;
if (sizes == null) {
return null;
}
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
我在另一篇解决我的问题的帖子中找到了此评论!
它更改视图的大小,以便在始终尝试填充屏幕时不会拉伸图像。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句