3 回答

TA貢獻1797條經驗 獲得超6個贊
如果使用數碼相機或智能手機拍攝照片,則旋轉通常作為圖像文件的一部分存儲在照片的Exif數據中。您可以使用Android讀取圖像的Exif元數據ExifInterface。
首先,創建ExifInterface:
ExifInterface exif = new ExifInterface(uri.getPath());
接下來,找到當前旋轉:
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
將exif旋轉轉換為度數:
int rotationInDegrees = exifToDegrees(rotation);
哪里
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
然后,以圖像的實際旋轉為參考點,使用來旋轉圖像Matrix。
Matrix matrix = new Matrix();
if (rotation != 0) {matrix.preRotate(rotationInDegrees);}
使用將Bitmap.createBitmapa Matrix作為參數的方法來創建新的旋轉圖像:
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
其中Matrix m擁有新的輪換:
Bitmap adjustedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);
- 3 回答
- 0 關注
- 828 瀏覽
添加回答
舉報