2012-10-18 20 views
3

我的問題上解碼

我有一系列的位圖,我想在正確的方向加載的位圖不讀EXIF數據。

當我將圖像保存我去和使用ExifInterface

  ExifInterface exif = new ExifInterface(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX); 
      int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL)); 
      Log.v("PhotoManager", "Rotation:"+rotation); 
      if (rotation > 0) { 
       exif.setAttribute(ExifInterface.TAG_ORIENTATION,String.valueOf(0)); 

這正常設置方向屬性,如果我拉這一形象從我的設備,這將是在正確的方向。但是,當我稍後解碼我的位圖時,即使圖像是以縱向拍攝,它仍保持在相機的左側水平的默認方向?

我的問題

我怎樣才能解碼位圖,並考慮到它的EXIF信息?

我不想在每次解碼後都旋轉圖像,因爲我必須創建另一個位圖,這是我沒有的內存。

在此先感謝。

回答

1

對於那些也堅持這一點,並有處理多個位圖時的oom問題,這裏是我的解決方案。

不要改變exif數據,就像我最初在問題中想到的那樣 - 我們需要這個後面的內容。

當涉及到解碼圖像進行查看,而不是解碼全尺寸圖像只是解碼縮小到您需要的圖像。下面的代碼示例包含將位圖解碼爲設備屏幕大小,然後它還會爲您處理位圖的旋轉。

public static Bitmap decodeFileForDisplay(File f){ 

    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 
     DisplayMetrics metrics = MyApplication.getAppContext().getResources().getDisplayMetrics(); 

     //The new size we want to scale to 
     //final int REQUIRED_SIZE=180; 

     int scaleW = o.outWidth/metrics.widthPixels; 
     int scaleH = o.outHeight/metrics.heightPixels; 
     int scale = Math.max(scaleW,scaleH); 
     //Log.d("CCBitmapUtils", "Scale Factor:"+scale); 
     //Find the correct scale value. It should be the power of 2. 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     Bitmap scaledPhoto = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     try { 
      ExifInterface exif = new ExifInterface(f.getAbsolutePath()); 
      int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL)); 
      if (rotation > 0) 
       scaledPhoto = CCBitmapUtils.convertBitmapToCorrectOrientation(scaledPhoto, rotation); 

     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     return scaledPhoto; 

    } catch (FileNotFoundException e) {} 
    return null; 
    } 

public static Bitmap convertBitmapToCorrectOrientation(Bitmap photo,int rotation) { 
    int width = photo.getWidth(); 
    int height = photo.getHeight(); 


    Matrix matrix = new Matrix(); 
    matrix.preRotate(rotation); 

    return Bitmap.createBitmap(photo, 0, 0, width, height, matrix, false); 

} 

因此,位圖圖像調用多數民衆贊成是decodeFileForDisplay(File f);在正確的方向和正確的大小爲你的屏幕節省您噸的內存問題後返回。

我希望它可以幫助別人

+2

不完整的代碼。 exifToDegrees缺失。 – pilcrowpipe

+0

對不起@pilcrowpipe你想我爲你編碼嗎? – StuStirling

+0

不,對我來說沒關係,我只是覺得最好有一個完整的解決方案作爲答案。無論如何,感謝您提供信息,這是一個很好的參考。 – pilcrowpipe