//读取图片旋转角度
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);
LogW.out("readPictureDegree : orientation = " + orientation);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
degree = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
degree = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
degree = 270;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
//旋转图片
public static Bitmap rotateBitmap(int angle, Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotation = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
return rotation;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if(width < height) {
bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getWidth());
}else {
Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getHeight(),bitmap.getHeight(),matrix,true);
}
万般无奈之下, 我无意之间使用方法一中的 ExifInterface 打印了拍照后压缩前角度信息, toast 打印结果是 6 , 对应常量 ExifInterface.ORIENTATION_ROTATE_90
, 可我记得之前获取到的旋转角度是0 , 仔细一想发现获取角度为0那次操作的是压缩后的图片, 我怀疑是不是压缩将图片旋转信息抹掉了.
于是我在压缩前获取图片旋转信息:
ExifInterface exifInterface = new ExifInterface(finalUserIcon.getAbsolutePath());//finalUserIcon为压缩前图片
final int degree = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
UIUtil.debugToast(String.valueOf(degree));
压缩后将原图旋转信息保存替换现有旋转信息:
try {
ExifInterface endEI = new ExifInterface(imgPath);//imgPath为压缩后图片路径
endEI.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(degree));
endEI.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
然后采用方法一的方式获取旋转信息并纠正旋转角度:
int degree = readPictureDegree(imgPath);
Bitmap bitmap = rotateBitmap(degree, BitmapFactory.decodeFile(imgPath));
saveBitmap(imgPath);
/** 保存方法 */
public static void saveBitmap(String path, Bitmap bm) {
File f = new File(path);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
至此问题得到解决, 无论横着拍照还是竖着拍照都能正确展示.