在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;

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐