我正在尝试调整Microsoft的Projectoxford Emotionapi的图像 - 自动Rotater代码。分析设备相机拍摄的每个图像的角度,然后旋转到正确的景观视图,以通过情感API分析。
我的问题是:我如何调整下面的代码以将位图作为参数?在这种情况下,我也完全失去了内容解析器和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;
}
}
我将如何调整下面的代码以将位图作为参数?
你不能。
在这种情况下,我也完全失去了内容解析器和Exitinterface的作用
您问题中的代码使用EXIF Orientation
标签来确定应应用于图像的方向,如拍摄照片的相机报道(或该标签的任何设置)所报告。ExifInterface
是读取EXIF标签的代码。ExifInterface
需要使用实际的JPEG数据,而不是解码的Bitmap
—Bitmap
不再具有EXIF标签。
ContentResolver
代码中有错误窗口,不应使用。com.android.support:exifinterface
库中的ExifInterface
具有一个构造函数,该构造函数采用InputStream
,从中读取JPEG。在此处使用Uri
的正确方法是将其传递给ContentResolver
上的openInputStream()
,将该流传递到ExifInterface
构造函数。