gpt4 book ai didi

三星 Galaxy S3、S4、S5 的 Android 相机/图片方向问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:31 24 4
gpt4 key购买 nike

我正在为 Android API 16 到 21 开发一个相机应用程序,主要且唯一的目的是拍摄人像照片。我可以使用多种设备(Nexus 4、Nexus 5、HTC...)拍照,并且它们的方向正确(这意味着我的预览在大小和方向上都与拍摄的照片相同)。

但是我已经在其他几台设备上测试了我的应用程序,其中一些给我带来了很多麻烦:Samsung Galaxy S3/S4/S5。

在这三个设备上,预览都可以正确显示,但是方法onPictureTaken(final byte[] jpeg, Camera camera)返回的图片总是侧身。

这是从 byte[] jpeg 创建的位图,在保存到磁盘之前显示在 ImageView 中给我的用户:

preview

这是保存在磁盘上的图像:

disk

如您所见,图像在预览中被完全拉伸(stretch),一旦保存在磁盘上就会错误地旋转。

这是我的 CameraPreview 类(我混淆了其他方法,因为它们与相机参数无关):

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
{
private SurfaceHolder surfaceHolder;
private Camera camera;

// Removed unnecessary code

public void surfaceCreated(SurfaceHolder holder)
{
camera.setPreviewDisplay(holder);
setCameraParameters();
camera.startPreview();
}

private void setCameraParameters()
{
Camera.Parameters parameters = camera.getParameters();
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);

DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int rotation = windowManager.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 rotate = (info.orientation - degrees + 360) % 360;
parameters.setRotation(rotate);

// Save Parameters
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
}
}

为什么这段代码适用于三星设备以外的其他设备?

我试图在以下 SO 帖子中找到答案,但到目前为止没有任何帮助: this onethis other one .

编辑

执行 Joey Chong 的回答不会改变任何东西:

public void onPictureTaken(final byte[] data, Camera camera)
{
try
{
File pictureFile = new File(...);
Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
FileOutputStream fos = new FileOutputStream(pictureFile);
realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);

int orientation = -1;
ExifInterface exif = new ExifInterface(pictureFile.toString());
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (exifOrientation)
{
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
break;
default:
break;
}

fos.close();
}

以下是我获得的工作设备的 EXIF 结果:

  • 方向:0

这里是 S4 的结果:

  • 方向:0

最佳答案

因为手机还是横向保存,元数据是90度的。您可以尝试检查 exif,在放入 ImageView 之前旋转位图。要检查 exif,请使用如下内容:

    int orientation = -1;

ExifInterface exif = new ExifInterface(imagePath);

int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;

break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;

break;
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;

break;

case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;

break;
default:
break;
}

关于三星 Galaxy S3、S4、S5 的 Android 相机/图片方向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27515487/

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