我试图适应微软的projectOxford EmotionApi的图像-自动旋转代码。对设备摄像机拍摄的每一幅图像进行角度分析,然后旋转到正确的景观视图上,通过情感API进行分析。
我的问题是:我如何调整下面的代码,使其以位图为论据?在这种情况下,我也完全不知道Content和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;
}
}发布于 2017-03-03 23:33:27
我将如何调整下面的代码以位图作为参数?
你不能这么做。
在这种情况下,我也完全不知道内容解析器和ExitInterface的作用。
您问题中的代码使用EXIF Orientation标记来确定应用于图像的方向,就像拍摄照片的摄像机(或其他设置的标记)所报告的那样。ExifInterface是读取EXIF标记的代码。ExifInterface需要处理实际的Bitmap数据,而不是解码的Bitmap-- Bitmap不再有EXIF标记。
里面的ContentResolver代码是错误的,不应该使用.ExifInterface库中的com.android.support:exifinterface有一个构造函数,该构造函数接受InputStream,它将从该构造函数读取InputStream。这里使用Uri的正确方法是将它传递给ContentResolver上的openInputStream(),将该流传递给ExifInterface构造函数。
https://stackoverflow.com/questions/42589171
复制相似问题