2012-11-03 44 views
3

我從我的肖像相機應用拍攝照片併發送到服務器。在發送之前,如果需要,我需要旋轉圖像。因此,我將圖像保存在SD卡中,並嘗試得到的Exif或Cursor.I通過最相關this.But什麼崗位的工作去爲me.Here是代碼..使用EXIF或光標圖像的方向始終爲0

  1. ExifInterface

    File imageFile = new File(uri.toString()); 
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
    

    這裏的方位是對於任何圖像始終爲0

  2. Cusor

    int orientation = 0; 
    final String[] projection = new String[] { MediaStore.Images.Media.ORIENTATION }; 
    final Cursor cursor = getApplicationContext() 
           .getContentResolver().query(uri, projection, null, 
             null, null); 
    if (cursor != null) { 
    final int orientationColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION); 
    if (cursor.moveToFirst()) { 
        orientation = cursor.isNull(orientationColumnIndex) ? 0 : cursor.getInt(orientationColumnIndex); 
    } 
    

    這裏也像旋轉是我zero.How可以找到圖片的確切旋轉?任何幫助將不勝感激..

+0

對此有什麼好運?有類似的問題? – ono

回答

0

請從支持庫開始使用ExifInterface。它提供了一個支持InputStream的構造函數。從Uri獲得路徑是越野車。 你需要這個庫:在build.gradle中編譯'com.android.support:exifinterface:25.3.1'。 代碼

private float getOrientation (InputStream image, Uri photoURI) { 
    //ExifInterface myExif = new ExifInterface(photoURI.getPath()); 

    float photoRotation = 0; 
    boolean hasRotation = true; 

    try { 
     ExifInterface exif = new ExifInterface(image); 
     int exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_UNDEFINED); 

     switch (exifRotation) { 
      case ExifInterface.ORIENTATION_UNDEFINED: { 
       hasRotation = false; 
      } 
      case ExifInterface.ORIENTATION_ROTATE_90: { 
       photoRotation = 90.0f; 
       break; 
      } 
      case ExifInterface.ORIENTATION_ROTATE_180: { 
       photoRotation = 180.0f; 
       break; 
      } 
      case ExifInterface.ORIENTATION_ROTATE_270: { 
       photoRotation = 270.0f; 
       break; 
      } 
     } 
    } catch (IOException e) { 
      Log.d("ODDREAMS", e.getMessage()); 
    } 

    if (!hasRotation) { 
     try { 
       String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION}; 
       Cursor cursor = getActivity().getContentResolver().query(photoURI, columns, null, null, null); 

       if (cursor == null) return 0; 

       if (cursor.moveToFirst()) { 
        int orientationColumnIndex = cursor.getColumnIndex(columns[1]); 
        photoRotation = cursor.getInt(orientationColumnIndex); 
        Log.d("ODDREAMS", "CURSOR " + photoRotation); 
       } 
       cursor.close(); 
     } 
     catch (Exception e) {} 

    } 

    return photoRotation; 
}