如果圖像(照片)是由您製作的程序拍攝的,則必須使用正確的旋轉值設置Parameters.setRotation。
這取決於相機驅動器在保存前旋轉圖像或將旋轉值保存爲exif TAG_ORIENTATION。
因此,如果TAG_ORIENTATION爲null或零,圖像的方向是正確的,否則您必須根據TAG_ORIENTATION中的值旋轉圖像。
CODE
得到EXIF方向:
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
獲取位圖旋轉:
Bitmap bmRotated = rotateBitmap(bitmap, orientation);
方法旋轉位圖:
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
}
catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
我有答案。 http://stackoverflow.com/questions/29971319/image-orientation-android/32747566#32747566 –
這是我遇到過的一個很棒的單線解決方案:> https://stackoverflow.com/a/34241250/8033090它可以花一秒鐘來加載,但我只是在圖片視圖的後面加入了一些文字,上面寫着「加載圖片」,圖片加載時會覆蓋文字。 –