用法:
//Environment.getExternalStorageDirectory()得到磁盘根目录
File inputImagePath = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
//得到磁盘文件里的图片旋转角度
int degrees = readPictureDegree(String.valueOf(Environment.getExternalStorageDirectory())+"/output_image.jpg");
FileInputStream file = new FileInputStream(inputImagePath);
Bitmap bitmap = BitmapFactory.decodeStream(file);
//传入Bitmap和旋转的角度返回正常角度Bitmap
Bitmap bitmap1 = rotateBitmap(bitmap,degrees);
//设置上去控件
image.setImageBitmap(bitmap1);
//传入图片在手机磁盘的路径,此方法返回该图片的旋转角度
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;
}
return degree;
}
//传入bitmap和图片的旋转角度,即可得到矫正后的图片
public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
if (degrees == 0 || null == bitmap) {
return bitmap;
}
Matrix matrix = new Matrix();
matrix.setRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
if (null != bitmap) {
bitmap.recycle();
}
return bmp;
}