当我从图库中选择图像以将其设置在 android 应用程序的 imageview 上时,我看到它反转了 180 或 270 或 90 度。
因此,为了检查/解决这个问题,我使用EXIF
了方向,但它总是给我“0”。
我无法解决问题所在。
代码:
Uri selectedImageUri = data.getData();
absolutePath = selectedImageUri.getPath();
exifMedia = new ExifInterface(absolutePath);
String exifOrint = exifMedia.getAttribute(ExifInterface.TAG_ORIENTATION);
exifOrientation = Integer.parseInt(exifOrint);
System.out.println("Orientation Tag is:"+exifOrientation);
/** Convert URI into byte */
ContentResolver cr = getBaseContext()
.getContentResolver();
InputStream inputStream = cr
.openInputStream(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
rotatedBMP = getResizedBitmapImage(
bitmap, 100, 100,exifOrientation);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100,
stream);
byteArray = stream.toByteArray();
mimProfileImageBrowse.setImageBitmap(rotatedBMP);
方法:
public Bitmap getResizedBitmapImage(Bitmap bm, int newHeight, int newWidth, int exifOrientation) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
/**
* create a matrix for the manipulation
*/
Matrix matrix = new Matrix();
/**
* resize the bit map
*/
/*
1 = Horizontal (normal)
2 = Mirror horizontal
3 = Rotate 180
4 = Mirror vertical
5 = Mirror horizontal and rotate 270 CW
6 = Rotate 90 CW
7 = Mirror horizontal and rotate 90 CW
8 = Rotate 270 CW
*/
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
rotate = 45;
break;
default:
break;
}
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(rotate);
/**
* recreate the new Bitmap
*/
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true);
return resizedBitmap;
}
请帮忙。也不matrix.postROTATE
旋转位图。我不知道为什么。
谢谢