/**
* 旋转图片
*
* @param angle 被旋转角度
* @param bitmap 图片对象
* @return 旋转后的图片
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
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;
}
/**
* 读取照片exif信息中的旋转角度
*
* @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);
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;
}
先吐槽下:三星在国内没人要是不是没有原因的!
因为一直没有注意,发现三星的手机拍出来的图片竟是斜的。然后使用了UtilsCode的读取图片偏移角度的API,竟然读出来还是错的。我真搞不明白这种主流的框架的api竟然还有错的。
最后查了下原因,发现utilsCode 1.6.4版本的api里面少了一个break,真是坑死人了。注意看switch第一行那个default.调用 这个api最少都是90度。巨神坑!!!
/**
* 获取图片旋转角度
*
* @param filePath 文件路径
* @return 旋转角度
*/
public static int getRotateDegree(String filePath) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(filePath);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
default:
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;
}
还好最后使用网上提供的api得出了正确的结果。