相信大家在项目中经常要掉系统的拍照功能吧,但是我发现了一个问题,三星手机在调用系统摄像机的时候拍出的图片居然呗旋转过!遂网上找了一大把资料,但是众说纷坛,很多都没有时间解决问题。最后自己不断结合摸索,终于解决了旋转问题不多说,直接上代码:
int rotate = change.readPictureDegree(mOutPutFileUri); //三星手机前置为8,旋转270度。后置为6旋转90度
if (rotate == 90 || rotate == 270) {
System.out.println("三星手机");
Bitmap bitmap = BitmapFactory.decodeFile(mOutPutFileUri);//根据Path读取资源图片
int width = bitmap.getWidth()/2;
int height = bitmap.getHeight()/2;
System.out.println("图片的宽为:"+bitmap.getWidth()+"\n"+"图片的高为:"+bitmap.getHeight());
if (rotate == 90) {
m.setRotate(90); // 旋转angle度
}else{
m.setRotate(270); // 旋转angle度
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true);// 从新生成图片
/**
* 读取图片属性:旋转的角度
* @param path 图片绝对路径
* @return degree旋转的角度
* 三星手机前置为8,旋转270度。后置为6旋转90度
*/
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);
System.out.println("图片的旋转角度是=="+orientation);
switch (orientation) {
case 6:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case 8:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}