2014-09-21 17 views
1

旋轉了哎每一個我試圖加載大圖像,以我的應用程序的背景下使用此代碼:解碼圖像有時被90degree

 final Drawable drawable =new BitmapDrawable(colorResource,decodeFile(new File(LMApplication.sharedpreferences.getString(path,""))));  

       new Handler(Looper.getMainLooper()).post(new Runnable() { 
        @Override 
        public void run() { 
          if(LMApplication.CurrentSDK < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
           view.setBackgroundDrawable(drawable); 
          }else{ 
           view.setBackground(drawable); 
          } 
         } 
       }); 


public Bitmap decodeFile(File input){ 
      Bitmap bmpCompressed = null; 
      try { 
       //Decode image size 

       BitmapFactory.Options o11 = new BitmapFactory.Options(); 
       o11.inJustDecodeBounds = true; 

       BitmapFactory.decodeStream(new FileInputStream(input),null,o11); 

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

       //Find the correct scale value. It should be the power of 2. 
       int scale=1; 
       while(o11.outWidth/scale/2>=REQUIRED_SIZE && o11.outHeight/scale/2>=REQUIRED_SIZE) 
        scale*=2; 

       //Decode with inSampleSize 
       BitmapFactory.Options o22 = new BitmapFactory.Options(); 
       o22.inSampleSize=scale; 
       BitmapFactory.decodeStream(new FileInputStream(input), null, o22); 
       bmpCompressed = BitmapFactory.decodeFile(input.toString(), o22); 

       // FileOutputStream out = null; 
       try { 
      //  out = new FileOutputStream(file); 
       //  bmpCompressed.compress(CompressFormat.JPEG, 100, out); 

       } catch (Exception e) { 
       e.printStackTrace(); 
       } finally { 
        try{ 
       //  out.close(); 
        } catch(Throwable ignore) {} 
       } 

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

但有些圖像旋轉90度,左側是解碼後,有沒有問題在這裏,或者有什麼辦法可以防止這種情況?

UPDATE:

改變了這樣的代碼,現在準備每一件事:

public Bitmap decodeFile(File input){ 
      Bitmap bmpCompressed = null; 
      try { 
       //Decode image size 

       BitmapFactory.Options o11 = new BitmapFactory.Options(); 
       o11.inJustDecodeBounds = true; 

       BitmapFactory.decodeStream(new FileInputStream(input),null,o11); 

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

       int width = getDimensions.WIDTH; 

       int height = getDimensions.HEIGHT; 
       //Find the correct scale value. It should be the power of 2. 
       int scale=1; 
       while(o11.outWidth/scale/2>=width && o11.outHeight/scale/2>=height) 
        scale*=2; 

       //Decode with inSampleSize 
       BitmapFactory.Options o22 = new BitmapFactory.Options(); 
       o22.inSampleSize=scale; 
       BitmapFactory.decodeStream(new FileInputStream(input), null, o22); 
       bmpCompressed = BitmapFactory.decodeFile(input.toString(), o22); 


       ExifInterface exif = new ExifInterface(input.getAbsolutePath()); 

       int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 

       if(orientation != ExifInterface.ORIENTATION_NORMAL){ 

        int rotatesize; 

        switch (orientation) { 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        rotatesize = -90; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        rotatesize = -180; 
        break; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        rotatesize = -270; 
        break; 

       default: 
        break; 
       } 

       Matrix matrix = new Matrix(); 
       matrix.postRotate(90); 
       bmpCompressed = Bitmap.createBitmap(bmpCompressed, 0, 0, bmpCompressed.getWidth(), bmpCompressed.getHeight(), matrix, true); 

       } 
       // FileOutputStream out = null; 
       try { 
      //  out = new FileOutputStream(file); 
       //  bmpCompressed.compress(CompressFormat.JPEG, 100, out); 

       } catch (Exception e) { 
       e.printStackTrace(); 
       } finally { 
        try{ 
       //  out.close(); 
        } catch(Throwable ignore) {} 
       } 

       } catch (IOException e) {} 
       return bmpCompressed; 
       } 

回答

0

使用ExifInterface的旋轉這樣

picturePath = getIntent().getStringExtra("path"); 
Bitmap bitmap = BitmapFactory.decodeFile(picturePath); 
ExifInterface exif = new ExifInterface(picturePath); 
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 

Matrix matrix = new Matrix(); 
switch (orientation) { 
case ExifInterface.ORIENTATION_ROTATE_90: 
    matrix.postRotate(90); 
    break; 
case ExifInterface.ORIENTATION_ROTATE_180: 
    matrix.postRotate(180); 
    break; 
case ExifInterface.ORIENTATION_ROTATE_270: 
    matrix.postRotate(270); 
    break; 
default: 
    break; 
} 

myImageView.setImageBitmap(bitmap); 
bitmap.recycle(); 

注:這裏picturePath選擇圖片的路徑從Gallery

+0

是的但問題exifinterface從轉換中創建1位圖+位圖,所以OOM被拋出,任何方式ty – fosreza 2014-09-27 14:13:09

+0

不適用於'ExifInterface'。它用於旋轉矩陣。嘗試使用後回收所有位圖,如我在答案中顯示你。 – kId 2014-09-27 18:05:15

+0

如果出現OOM,則嘗試「inSample」這些圖像。 – kId 2014-09-27 18:08:54