在android多媒体开发中,ExifInterface(exif exchangeable image file) ,这个接口提供了图片文件的旋转,gps,时间等信息。
Bitmap bitmap =null;
int scallType = 0;
try {
ExifInterface exifInterface = new ExifInterface(file.getPath());
int result = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
int rotate = 0;
switch(result) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
default:
break;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
// 初めのデコードはサイズ取得のみ
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
if (options.outWidth < 0 || options.outHeight < 0) {
return null;
}
scallType = genScallType(context, options);
options.inJustDecodeBounds = false;
bitmap= BitmapFactory.decodeFile(filePath, options);
if(rotate > 0) {
Matrix matrix = new Matrix();
matrix.setRotate(rotate);
Bitmap rotateBitmap = Bitmap.createBitmap(
bitmap, 0, 0, options.outWidth, options.outHeight, matrix, true);
if(rotateBitmap != null) {
bitmap.recycle();
bitmap = rotateBitmap;
}
}
} catch (IOException e) {
e.printStackTrace();
}
public int getAttributeInt (String tag, int defaultValue)
Since:
API Level 5
Returns the integer value of the specified tag. If there is no such tag in the JPEG file or the value cannot be parsed as integer, return defaultValue.
(在指定的tag后,返回一个int类型的值,这里传入的是ExifInterface.TAG_ORIENTATION,所以会返回一个角度的int类型的值,当我们用android 平板电脑,或者手机横屏拍照片时并希望把它作为背景设置在所在的应用背景,而且不希望背景会产生旋转90度得现象)