我正在嘗試適應微軟的projectOxford EmotionApi的圖像自動旋轉代碼。分析設備攝像機拍攝的每個圖像的角度,然後旋轉到正確的橫向視圖,以通過情緒API進行分析。旋轉位圖而不使用ImageURI/ContentResolver?
我的問題是:我將如何調整下面的代碼以將位圖作爲參數?在這種情況下,我也完全失去了內容解析器和ExitInterface的角色。任何幫助,非常感謝。
private static int getImageRotationAngle(
Uri imageUri, ContentResolver contentResolver) throws IOException {
int angle = 0;
Cursor cursor = contentResolver.query(imageUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor != null) {
if (cursor.getCount() == 1) {
cursor.moveToFirst();
angle = cursor.getInt(0);
}
cursor.close();
} else {
ExifInterface exif = new ExifInterface(imageUri.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
default:
break;
}
}
return angle;
}
// Rotate the original bitmap according to the given orientation angle
private static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
// If the rotate angle is 0, then return the original image, else return the rotated image
if (angle != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(
bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} else {
return bitmap;
}
}
瞭解,謝謝你的詳細解釋。任何關於旋轉位圖的建議? –
@ A.Xu:嗯,你的問題中的'rotateBitmap()'方法將旋轉位圖。整個EXIF標題背後的要點是確定是否應該旋轉JPEG,如果是,則確定是否應該旋轉多少。 – CommonsWare