我正在尝试适应微软的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