調用系統相機拍照完之後,在系統相冊查看圖片正常。但是在自己的應用中從文件夾中讀取圖片裁剪時圖片被旋轉了,經過查資料發現有的系統拍完照片後把圖片旋轉了90度。(遇到的手機是小米6)
解決辦法:圖片的角度在圖片的信息中,所以讀取圖片的旋轉角度,然後給他旋轉回去
/**
* 讀取照片旋轉角度
*
* @param path 照片路徑
* @return 角度
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.e("TAG", "原圖被旋轉角度: ========== " + orientation );
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;
}
/**
* 旋轉圖片
* @param angle 被旋轉角度
* @param bitmap 圖片對象
* @return 旋轉後的圖片
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Log.e("TAG","angle==="+angle);
Bitmap returnBm = null;
// 根據旋轉角度,生成旋轉矩陣
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// 將原始圖片按照旋轉矩陣進行旋轉,並得到新的圖片
returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bitmap;
}
if (bitmap != returnBm) {
bitmap.recycle();
}
return returnBm;
}
調用:
int angle = readPictureDegree(originalFile.getPath());
Log.e("TAG","degree===="+angle);
Bitmap bitmapori = BitmapFactory.decodeFile(originalFile.getPath());
// 修復圖片被旋轉的角度
Bitmap bitmap = rotaingImageView(angle, bitmapori);