照片始终以相机内置在设备中的方向拍摄。为了使图像正确旋转,您必须读取存储在图片中的方向信息(EXIF元数据)。在那里存储了拍摄图像时设备的方向。
这是一些读取EXIF数据并相应旋转图像的代码:
file是图像文件的名称。
BitmapFactory.Options bounds = new BitmapFactory.Options();bounds.inJustDepreBounds = true;BitmapFactory.depreFile(file, bounds);BitmapFactory.Options opts = new BitmapFactory.Options();Bitmap bm = BitmapFactory.depreFile(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属性的访问更容易。



