項目開發中,須要用一個ImageView顯示拍照的圖片。
在使用三星手機進行測試的時候發現圖片角度發生了旋轉,經資料查詢,這是由於三星手機拍照的圖片旋轉角度是90度,而其它手機是0度。這樣思路就出來了:先查詢被旋轉了多少度,然后再旋轉回來。ok。以下上代碼。
首先是讀取圖片被旋轉的角度。
/**
* 讀取照片exif信息中的旋轉角度
* @param path 照片路徑
* @return 角度
*/
public int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
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;
}
再接着是將圖片旋轉回0度。
public Bitmap toturn(Bitmap img, String path) {
Matrix matrix = new Matrix();
matrix.postRotate(readPictureDegree(path));
int width = img.getWidth();
int height = img.getHeight();
img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
return img;
}
