2012-09-25 44 views
6

我使用Android 2.2在HTC Desire上測試我的應用程序。它的工作原理與我所喜歡的完全相同。我使用Sherlock軟件包在舊設備上具有與較新設備相同的樣式。如何處理不符合EXIF方向數據的Android設備?

我的AVD設置爲使用最新的android,並且它看起來還可以。然後我把它放到三星Galaxy S2上,當我使用相機和畫廊圖片時,它們被錯誤地旋轉了。它接縫三星上的東西(相機應用程序,它自己的機器人)沒有或它確實檢查EXIF,我的圖像是錯誤的。縱向加載縱向圖像,縱向加載橫向圖像。

  1. 我想我需要檢查EXIF不知何故,並忽略它以加載圖像,因爲它們是?
  2. 更大的問題是 - 如何知道是否有其他設備(某些HTC,某些華爲無線)會出現類似問題?我認爲除了擁有4個屏幕尺寸組外,所有Android設備的行爲方式都是相同的......

Tnx。

回答

4

沒有任何代碼很難說出發生了什麼。

我發現的最簡單的方法是讀取EXIF信息並檢查圖像是否需要旋轉。要了解更多關於ExifInterface類在Android: http://developer.android.com/intl/es/reference/android/media/ExifInterface.html

這就是說,這裏是一些示例代碼:

/** An URI and a imageView */ 
public void setBitmap(ImageView mImageView, String imageURI){ 
    // Get the original bitmap dimensions 
    BitmapFactory.Options options = new BitmapFactory.Options();    
    Bitmap bitmap = BitmapFactory.decodeFile(imageURI, options); 
    float rotation = rotationForImage(getActivity(), Uri.fromFile(new File(imageURI))); 

    if(rotation!=0){ 
     //New rotation matrix 
     Matrix matrix = new Matrix(); 
     matrix.preRotate(rotation); 
     mImageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, reqHeight, reqWidth, matrix, true)); 
    } else { 
     //No need to rotate 
     mImageView.setImageBitmap(BitmapFactory.decodeFile(imageURI, options)); 
    } 
} 


/** Returns how much we have to rotate */ 
public static float rotationForImage(Context context, Uri uri) { 
     try{ 
      if (uri.getScheme().equals("content")) { 
       //From the media gallery 
       String[] projection = { Images.ImageColumns.ORIENTATION }; 
       Cursor c = context.getContentResolver().query(uri, projection, null, null, null); 
        if (c.moveToFirst()) { 
         return c.getInt(0); 
        }    
      } else if (uri.getScheme().equals("file")) { 
       //From a file saved by the camera 
        ExifInterface exif = new ExifInterface(uri.getPath()); 
        int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)); 
        return rotation; 
      } 
      return 0; 

     } catch (IOException e) { 
      Log.e(TAG, "Error checking exif", e); 
      return 0; 
     } 
} 

/** Get rotation in degrees */ 
private static float exifOrientationToDegrees(int exifOrientation) { 
     if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      return 90; 
     } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { 
      return 180; 
     } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      return 270; 
     } 
     return 0; 
} 

如果有,你會看到日誌上rotationForImage功能「錯誤檢查EXIF」錯誤。

+4

答案很好解釋如何檢查ExifInterface方向數據,但這似乎並不是原始問題。 '你如何處理那些沒有正確記錄方向場的設備?'似乎是問題的焦點。這個答案只解釋瞭如何從android文檔中明確解釋的ExifInterface中獲取方向字段。 –

+0

不適合我。 –