2012-03-12 101 views
1

我正在尋找一種解碼旋轉文件(0,90,180,270度)的位圖的方法。我需要的,因爲圖像是大的,如果我解碼位圖,然後使用類似解碼旋轉的位圖

Bitmap rotatedBitmap=Bitmap.createBitmap(source, x, y, width, height, m, filter) 

有一個時刻,當這兩個位都在內存中,有運行內存不足的風險。

任何想法?謝謝。

回答

1

也許這是你在找什麼?

//decodes image and scales it to reduce memory consumption 
    //NOTE: if the image has dimensions which exceed int width and int height 
    //its dimensions will be altered. 
    private Bitmap decodeToLowResImage(byte [] b, int width, int height) { 
     try { 
      //Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o); 

      //The new size we want to scale to 
      final int REQUIRED_SIZE_WIDTH=(int)(width*0.7); 
      final int REQUIRED_SIZE_HEIGHT=(int)(height*0.7); 

      //Find the correct scale value. It should be the power of 2. 
      int width_tmp=o.outWidth, height_tmp=o.outHeight; 
      int scale=1; 
      while(true){ 
       if(width_tmp/2<REQUIRED_SIZE_WIDTH || height_tmp/2<REQUIRED_SIZE_HEIGHT) 
        break; 
       width_tmp/=2; 
       height_tmp/=2; 
       scale*=2; 
      } 

      //Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize=scale; 
      return BitmapFactory.decodeStream(new ByteArrayInputStream(b), null, o2); 
     } catch (OutOfMemoryError e) { 
      //handle this 
     } 
     return null; 
    } 
+0

感謝您的回答,我不想縮小它的大小,只是旋轉它 – Addev 2012-03-12 15:23:49

+0

如果你想旋轉一個圖像,你將不得不擴展ImageView類,重寫它的onDraw方法,獲取畫布,應用rotate方法畫布,最後使用drawable上的繪製方法。 – 2012-03-13 04:33:25

0

如果你想使用基於NDK的解決方案,我創建了一個here,和我做了一個GitHub的項目here

這將通過將數據放入本機C「世界」來避免OOM,回收舊數據並在旋轉後返回結果。

它不需要任何下采樣。