2010-03-30 25 views

回答

10

它實際上是非常簡單的事情。使用

Bitmap yourBitmap = Bitmap.createBitmap(sourceBitmap, x to start from, y to start from, width, height)

更新:使用BitmapRegionDecoder

+3

感謝史蒂夫,但這需要我首先加載sourceBitmap,我試圖避免這樣做,因爲它太大而不適合內存,我想使用未經過下采樣的位圖。 – Schermvlieger 2010-04-09 10:49:48

+0

糟糕,你是對的,我沒有正確地閱讀你的問題的一部分。我不確定如何在沒有縮減採樣的情況下做到這一點。事實上,一個非常類似的問題也出現在我自己的數字化工作列表中! – 2010-04-09 11:10:23

9

試試這個

InputStream istream = null; 
try { 
    istream = this.getContentResolver().openInputStream(yourBitmapUri); 
} catch (FileNotFoundException e1) { 
    e1.printStackTrace(); 
} 

BitmapRegionDecoder decoder  = null; 
try { 
    decoder = BitmapRegionDecoder.newInstance(istream, false); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
Bitmap bMap = decoder.decodeRegion(new Rect(istream, x to start from, y to start from, x to end with, y to end with), null);  
imageView.setImageBitmap(bMap); 
+0

oops ..sorry ..此外,還有一件事是爲sdk版本2.3或更高版本... – Renjith 2012-07-27 10:59:23

0

@RKN

你的方法也可以拋出的OutOfMemoryError異常 - 如果裁剪位圖超過VM。

我的方法將你和防範這種exeption: (L,T,R,B - 圖像%)

Bitmap cropBitmap(ContentResolver cr, String file, float l, float t, float r, float b) 
{ 
    try 
    { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 

     // First decode with inJustDecodeBounds=true to check dimensions 
     BitmapFactory.decodeFile(file, options); 
     int oWidth = options.outWidth; 
     int oHeight = options.outHeight; 

     InputStream istream = cr.openInputStream(Uri.fromFile(new File(file))); 

     BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(istream, false); 
     if (decoder != null) 
     { 
      options = new BitmapFactory.Options(); 

      int startingSize = 1; 
      if ((r - l) * oWidth * (b - t) * oHeight > 2073600) 
       startingSize = (int) ((r - l) * oWidth * (b - t) * oHeight/2073600) + 1; 

      for (options.inSampleSize = startingSize; options.inSampleSize <= 32; options.inSampleSize++) 
      { 
       try 
       { 
        return decoder.decodeRegion(new Rect((int) (l * oWidth), (int) (t * oHeight), (int) (r * oWidth), (int) (b * oHeight)), options);  
       } 
       catch (OutOfMemoryError e) 
       { 
        Continue with for loop if OutOfMemoryError occurs 
       } 
      } 
     } 
     else 
      return null; 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    return null; 
} 

,並返回最大可用的位圖或空

+0

如果'startingSize'> 32最初是因爲輸入圖像太大? – TWiStErRob 2014-07-25 10:15:52

0

使用RapidDecoder

,簡單地做到這一點

import rapid.decoder.BitmapDecoder; 

Rect bounds = new Rect(left, top, right, bottom); 
Bitmap bitmap = BitmapDecoder.from(getResources(), R.drawable.image) 
     .region(bounds).decode(); 

它需要Android 2.2或以上。

0

您可以使用以下算法

  • 計算最大可能inSampleSize仍然產生一個 圖像比你的目標較大的出完全加載位圖加載位圖的縮放版本。
  • 使用 BitmapFactory.decodeFile(文件,選項)加載圖像,作爲 選項傳入inSampleSize。
  • 使用 Bitmap.createScaledBitmap()調整到所需的尺寸。

查看以下帖子 Android: Resize a large bitmap file to scaled output file瞭解更多詳情。