2017-03-03 63 views
0

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

回答

0

我会如何适应下面的代码,以位图作为参数?

你不行。

我也完全失去了作为内容Resolver和ExitInterface在这种情况下

在你的问题中的代码使用EXIF Orientation标签,以确定应适用于该方位的作用图像,照相机所拍摄的照片(或任何设置标签)报告。 ExifInterface是读取EXIF标签的代码。 ExifInterface需要使用实际的JPEG数据,而不是解码的Bitmap — a Bitmap不再具有EXIF标签。

ContentResolver这里的代码存在bug并且不应该使用。 com.android.support:exifinterface库中的ExifInterface具有一个构造函数,它需要一个InputStream,从中读取JPEG。在这里使用Uri的正确方法是将它传递给ContentResolver上的openInputStream(),将该流传递给ExifInterface构造函数。

+0

了解,谢谢你的详细解释。任何关于旋转位图的建议? –

+0

@ A.Xu:嗯,你的问题中的'rotateBitmap()'方法将旋转位图。整个EXIF标题背后的要点是确定是否应该旋转JPEG,如果是,则确定是否应该旋转多少。 – CommonsWare