2017-03-03 7 views
0

私はMicrosoftの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

どのように私は、引数としてビットマップを取るために以下のコードを適応させるのでしょうか?

できません。

私はまた、完全にあなたの質問のコードが適用されなければならない方向を決定するためにEXIF Orientationタグを使用しています。この場合、コンテンツリゾルバとExitInterfaceの役割

へと失われています写真を撮影したカメラ(またはそのタグを設定したもの)によって報告された画像。 ExifInterfaceは、EXIFタグを読み取るコードです。 ExifInterfaceは、実際のJPEGデータで動作する必要があります。復号化されていません。BitmapBitmapにはEXIFタグがありません。

コード内にContentResolverのコードはバグがあり、使用しないでください。 com.android.support:exifinterfaceライブラリに入っているExifInterfaceには、InputStreamのコンストラクタがあり、そのコンストラクタにはJPEGが読み込まれます。 Uriを使用する正しい方法は、ContentResolveropenInputStream()にそれを渡し、そのストリームをExifInterfaceコンストラクタに渡すことです。

+0

ご了承ください。徹底した説明をお願いします。ビットマップの回転に関する推奨事項はありますか? –

+0

@ A.Xu:質問の 'rotateBitmap()'メソッドは、ビットマップを回転させます。 EXIFヘッダー全体の背後にあるポイントは、JPEGを回転するかどうかを決定することです。 – CommonsWare

関連する問題