2010-03-30 11 views

cevap

10

Aslında yapmak çok basittir.

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

Güncelleme kullanın: BitmapRegionDecoder

+3

Teşekkürler Steve, ancak bu önce sourceBitmap'e yüklememi gerektirir ve bunu yapmaktan kaçınmaya çalışıyorum, çünkü belleğe sığmayacak kadar büyüktür ve alt örneklemede olmayan bir bitmap ile çalışmak istiyorum. . – Schermvlieger

+0

Oops, haklısın, sorununun bu bölümünü düzgün okumadım. Bundan sonra altörneklemesiz olarak nasıl yapılacağından emin değilim. Aslında çok benzer bir problem, kendi başıma nasıl yapılacağına dair bir liste. –

9

bu

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 sürüm 2.3 veya üstü için bu yöntem bir şey daha var ... – Renjith

0

@RKN

Yöntemin ayrıca istisna OutOfMemoryError atabilir denemek kullanmak - kırpılmış bitmap VM aşarsa.

My yöntem bu exeption karşı sizin ve koruma birleştirir: (l, t, r, b - resmin%)

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; 
} 

ve maksimum kullanılabilir bitmap veya null

+0

Başlangıçta giriş görüntüsü çok büyüktür çünkü başlangıçta başlangıçta 32 ise? – TWiStErRob

0

kullanımı RapidDecoder döndürür.

Ve sadece bu

import rapid.decoder.BitmapDecoder; 

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

Yukarıda Android 2.2 veya gerektirir yapmak.

0

Dışarı tam hala hedeften daha bir görüntü daha büyük verir mümkün olan maksimum inSampleSize hesaplayın aşağıdaki algoritmayı

  • kullanarak bitmap yüklenirken ile bitmap'in ölçekli sürümünü yükleyebilirsiniz.
  • BitmapFactory.decodeFile (dosya, seçenekler) kullanarak görüntüyü seçeneği olarak InSampleSize uygulamasından geçirerek yükleyin.
  • Bitmap.createScaledBitmap() öğesini kullanarak istediğiniz boyutlara yeniden boyutlandırın.

Daha fazla bilgi için aşağıdaki iletiyi denetleyin Android: Resize a large bitmap file to scaled output file.

İlgili konular