gpt4 book ai didi

Android 相机预览在预览中拉伸(stretch),而不是在拍照后

转载 作者:太空狗 更新时间:2023-10-29 13:16:46 24 4
gpt4 key购买 nike

我有一个以纵向模式拍照的应用程序,一切正常,只是预览的尺寸与拍照后的图像尺寸不同。预览看起来失真且模糊,但会填满整个屏幕。拍照后,图像清晰,但要么比设备高度短,要么宽度更小(取决于使用的设备)。

下面是一些图像示例和我正在使用的一些代码。

拍照前: pic1

拍照后: pic2

纵向显示的相机预览代码:

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

最佳答案

我找到了这个 comment在另一篇解决了我的问题的帖子中!

它会更改 View 的大小,以便图像在始终尝试填满屏幕时不会被拉伸(stretch)。

关于Android 相机预览在预览中拉伸(stretch),而不是在拍照后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com