2014-10-20 85 views
0

我需要在我的應用程序中將照片從相機上傳到服務器。它適用於大多數設備。但是有一些設備會導致圖像旋轉90度,這不是我想要的行爲。經過研究,我開始知道它的EXIF數據附有圖像。爲了去除圖像位圖中的EXIF數據,我嘗試了各種各樣的東西,比如重新調整圖像等,但沒有一個爲我工作。任何人都請提出一種方法來執行此任務。如何從安卓相機圖像中刪除EXIF數據

回答

2

試試這個,

public static Bitmap getImage(Context context, Uri uri) 
      throws FileNotFoundException, IOException { 

     InputStream input = context.getContentResolver().openInputStream(uri); 

     BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); 
     onlyBoundsOptions.inJustDecodeBounds = true; 
     onlyBoundsOptions.inDither = true;// optional 
     onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional 
     BitmapFactory.decodeStream(input, null, onlyBoundsOptions); 
     input.close(); 

     if ((onlyBoundsOptions.outWidth == -1) 
       || (onlyBoundsOptions.outHeight == -1)) 
      return null; 

     BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
     bitmapOptions.inJustDecodeBounds = false; 
     bitmapOptions.inDither = true; 
     bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional 
     input = context.getContentResolver().openInputStream(uri); 
     Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); 
     input.close(); 

     ExifInterface ei = new ExifInterface(uri.getPath()); 
     int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      bitmap = rotateImage(bitmap, 90); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      bitmap = rotateImage(bitmap, 180); 
      break; 
     } 

     return bitmap; 
    } 

這裏的URI是從相機拍攝圖像的URI。

對於理解EXIF orienatation:轉到http://www.impulseadventure.com/photo/exif-orientation.html

+0

而且哪裏的位圖從何而來? – greenapps 2014-10-20 08:25:25

+0

查看編輯答案 – 2014-10-20 08:27:41

+0

但是你的代碼是做什麼的?總是旋轉?請澄清。 – greenapps 2014-10-20 08:31:55

0

這裏我們使用ExifInterface對象的JPEG文件中讀取標籤和讀取例如圖像的方向屬性

// Variable to store the corrected bitmap. 
Bitmap correctedBitMap = null; 

ExifInterface exifInterface = new ExifInterface(<PATH OF YOUR PHOTO>); 
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

現在整數變量取向具有關於信息有多少是由旋轉的圖像,然後將其分別對更改後ExifInterface恆定值&檢查。

switch(orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     correctedBitMap = rotateImage(<YOUR BITMAP OBJECT>, 90); 
     break; 

    case ExifInterface.ORIENTATION_ROTATE_180: 
     correctedBitMap = rotateImage(<YOUR BITMAP OBJECT>, 180); 
     break; 
} 

下面是rotateImage方法的代碼:

private Bitmap rotateImage(Bitmap source, float angle) { 

    Bitmap bitmap = null; 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    try { 
     bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), 
      matrix, true); 
    } catch (OutOfMemoryError e) { 
     e.printStackTrace(); 
    } 
    return bitmap; 
} 
+0

欣賞你的努力。其實我已經在請求'URI'來加載位圖。而問題在於,由於相機在應用程序中拍攝的照片暫時存儲。所以當我再次請求'URI'時,它會導致崩潰。任何其他建議?有沒有辦法在'Bitmap'對象上執行這個函數? – Ammar 2014-10-20 09:49:10

+0

爲此,你可以看看我的這個答案http://stackoverflow.com/a/26463347/2330675。這裏檢查最後一塊代碼。 – Tushski 2014-10-20 10:22:56