androi studio bitmap 压缩后 图片方向异常
Android 使用 Camera2拍照后,获得 byte[] data,随后使用 BitmapFactory.decodeByteArray 压缩
导致图片方向异常
/*
* 避免出现方向异常
* */
public static Matrix getMatrix(byte[] data) {
if (Build.VERSION.SDK_INT >= 24) {
ExifInterface exif = null;
try {
exif = new ExifInterface(new ByteArrayInputStream(data));
} catch (IOException e) {
e.printStackTrace();
}
int orientation = ExifInterface.ORIENTATION_UNDEFINED;
if (exif != null) {
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
}
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
break;
default:
break;
}
return matrix;
} else {
return null;
}
}
....
Matrix matrix = getMatrix(data);
if (matrix != null) {
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} else {
return bitmap;
}

浙公网安备 33010602011771号