2010-03-30 66 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了解更多详情。