在Android多媒体开发中,ExifInterface(exif exchangeable image file) ,这个接口提供了图片文件的旋转,gps,时间等信息。
/**
* 获取正常角度的图片
* @param path
* @param srcBitmap
* @return
*/
public static Bitmap rotateBitmapInNeeded(String path, Bitmap srcBitmap) {
if (TextUtils.isEmpty(path) || srcBitmap == null) {
return null;
}
ExifInterface localExifInterface;
try {
localExifInterface = new ExifInterface(path);
int rotateInt = localExifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
float rotate = getImageRotate(rotateInt);
if (rotate != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0,
srcBitmap.getWidth(), srcBitmap.getHeight(), matrix,
false);
if (dstBitmap == null) {
return srcBitmap;
} else {
if (srcBitmap != null && !srcBitmap.isRecycled()) {
srcBitmap.recycle();
}
return dstBitmap;
}
} else {
return srcBitmap;
}
} catch (IOException e) {
e.printStackTrace();
return srcBitmap;
}
}
/**
* 获得旋转角度
*
* @param rotate
* @return
*/
public static float getImageRotate(int rotate) {
float f;
if (rotate == 6) {
f = 90.0F;
} else if (rotate == 3) {
f = 180.0F;
} else if (rotate == 8) {
f = 270.0F;
} else {
f = 0.0F;
}
return f;
}
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦