apk图像旋转问题
问题:
有的手机拍摄的图片旋转90度,有的图片旋转了180度,有的手机是正常的,现在要求的是正的。
解决措施:
获得不同手机,图像被旋转的角度,逆旋转回去。
参考:
https://www.jb51.net/article/93261.htm
https://www.jianshu.com/p/a360b32ec9b3
思路:
1、EXIF是图像文件的信息
2、先读图 根据EXIF 判断角度 然后旋转
参考代码:
// Rotate according to EXIF
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(resolver.openInputStream(imageUri));
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch(IOException e) {
Log.e("MainActivity", "ExifInterface IOException");
}
Log.d("MainActivity", "rotate = " + rotate);
Bitmap returnBm = null;
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
try {
returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch(OutOfMemoryError e) {
e.printStackTrace();
}
if(returnBm == null) {
returnBm = bitmap;
}
if(bitmap!=returnBm) {
bitmap.recycle();
}
return returnBm;