[英]Rotating image to Mobile's Orientation when capturing with camera :Android
我正在構建一個Android應用程序,從手機的相機中單擊照片並將其顯示在ImageView中,它會自動逆時針旋轉90度。我想以手機單擊的方向而不是手機的方向顯示它讓它旋轉。
誰能提供相關的代碼片段或一些相關文檔的指針?
ExifInterface exif = new ExifInterface(imageFilePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
// rotate the image
break;
//....
}
使用這種方法將幫助您。
/*
* 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
*/
public static Bitmap getRotateBitmapImage(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;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
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;
case 0:
rotate = 360;
break;
default:
break;
}
matrix.postRotate(rotate);
// resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.