我查看了一下,但似乎没有一个确切的答案或解决方案来解决这个非常恼人的问题。
我以纵向方向拍摄照片,当我点击保存/放弃时,按钮也处于正确的方向。问题是,当我稍后检索图像时,它处于横向方向(图片已逆时针旋转90度)
我不想强制用户在特定方向上使用相机。
是否有一种方法可以检测照片是否以纵向模式拍摄,然后解码位图并将其翻转到正确的方向?
我查看了一下,但似乎没有一个确切的答案或解决方案来解决这个非常恼人的问题。
我以纵向方向拍摄照片,当我点击保存/放弃时,按钮也处于正确的方向。问题是,当我稍后检索图像时,它处于横向方向(图片已逆时针旋转90度)
我不想强制用户在特定方向上使用相机。
是否有一种方法可以检测照片是否以纵向模式拍摄,然后解码位图并将其翻转到正确的方向?
照片总是以设备内置摄像头的方向拍摄。要正确旋转图像,您需要读取存储在图片中的方向信息(EXIF元数据)。它记录了拍摄照片时设备的方向。
以下是一些读取EXIF数据并相应旋转图像的代码:
file
是图像文件的名称。
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
更新于2017-01-16
随着25.1.0支持库的发布,引入了一个ExifInterface支持库,这应该可以使访问Exif属性更容易。请参阅Android开发者博客中的一篇文章了解详情。
https://dev59.com/_mUo5IYBdhLWcg3wvBeX#18915443
对于那些不想点击的人,相关的技巧是使用CameraInfo而不是依赖于EXIF或媒体文件上的光标。Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(mCurrentCameraId, info);
Bitmap bitmap = rotate(realImage, info.orientation);
在链接中查看完整的代码。
mCurrentCameraId
是从哪里来的? - User
opts
和resizedBitmap
? - lschlessinger