通常情況厂抽,調(diào)用照相機(jī)拍照之后產(chǎn)生的圖片默認(rèn)旋轉(zhuǎn)角度為0,此信息可以通過讀取圖片的EXIF信息來獲取到丁眼。對于某些手機(jī)拍照之后旋轉(zhuǎn)角度被改變了筷凤,造成照片的現(xiàn)實(shí)也改變了我們可以通過android.graphics.Matrix將照片角度在旋轉(zhuǎn)回去即可。
1. 使用ExifInterface對象獲取圖片的EXIF信息苞七。
/**
* 讀取圖片的旋轉(zhuǎn)的角度
* ExifInterface支持3中傳參數(shù)的方式藐守,
* 1.指定文件路徑
* 2.通過FileDescriptor對象
* 3.從原始的輸入流
* @param path 圖片絕對路徑
* @return
*/
private int getBitmapDegree(String path) {
int degree = 0;
try {
// 從指定路徑下讀取圖片,并獲取其EXIF信息
ExifInterface exifInterface = new ExifInterface(path);
// 獲取圖片的旋轉(zhuǎn)信息
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
之后使用矩陣將圖片的角度旋轉(zhuǎn)回去蹂风。
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null;
// 根據(jù)旋轉(zhuǎn)角度卢厂,生成旋轉(zhuǎn)矩陣
Matrix matrix = new Matrix();
matrix.postRotate(degree);
try {
// 將原始圖片按照旋轉(zhuǎn)矩陣進(jìn)行旋轉(zhuǎn),并得到新的圖片
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return returnBm;
}