问题
今天刚把索尼手机升级到Android N, 调用系统相机拍照完之后,在系统相册查看图片正常。但是在自己的应用中裁剪图片时图片是横的,经过查资料发现有的系统拍完照片后把图片旋转了90度。
解决办法
很简单把就是把图片反向旋转90度就好,但是会遇到问题是低版本或其他手机没旋转的就会出问题。所以首先要判断哪些图片旋转了,然后进行旋转处理。
1. 获取图片信息
ExifInterface exif = null;
try {
exif = new ExifInterface(filepath);
Log.i("tag", "exif》》》》》》》》》》》》》》》" + exif);
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
digree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
digree = 270;
break;
default:
digree = 0;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
exif = null;
}
2. 根据图片信息旋转Bitmap
//如果图片不为0
if (digree != 0) {
// 旋转图片
Matrix m = new Matrix();
m.postRotate(digree);
orc_bitmap = Bitmap.createBitmap(orc_bitmap, 0, 0, orc_bitmap.getWidth(),
orc_bitmap.getHeight(), m, true);
}
3. 设置图片
if (orc_bitmap != null) {
imageView.setImageBitmap(orc_bitmap);
}