2010-03-30 15 views

risposta

10

In realtà è molto semplice da fare. Utilizzare

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

Aggiornamento: utilizzare BitmapRegionDecoder

+3

Grazie Steve, ma per prima cosa dovrei caricarlo in sourceBitmap, e sto cercando di evitare di farlo, poiché è troppo grande per stare nella memoria, e voglio lavorare con una bitmap che non è stata sottoposta a downsampling . – Schermvlieger

+0

Oops, hai ragione, non ho letto correttamente quella parte della tua domanda. Non sono sicuro di come farlo senza sottocampionare. In effetti, un problema molto simile si trova anche nella mia lista personale-come-fare-come-fare! –

9

provare questo

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 .. un'altra cosa questo metodo è intented per sdk versione 2.3 o successiva ... – Renjith

0

@RKN

Il vostro metodo può anche lanciare OutOfMemoryError eccezione - se bitmap ritagliata supera VM.

Il mio metodo Distinti e la protezione contro questo combina exeption: (L, T, R, B -% dell'immagine)

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

e restituisce max bitmap disponibile o nullo

+0

Cosa succede se 'startingSize'> 32 inizialmente perché l'immagine di input è così grande? – TWiStErRob

0

Usa RapidDecoder.

E semplicemente fare questo

import rapid.decoder.BitmapDecoder; 

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

Richiede Android 2.2 o superiore.

0

È possibile caricare la versione ridotta di bitmap con caricare completamente il bitmap utilizzando seguente algoritmo

  • Calcolare l'inSampleSize massimo possibile che produce ancora un'immagine più grande della vostra destinazione.
  • Carica l'immagine utilizzando BitmapFactory.decodeFile (file, opzioni), passando inSampleSize come opzione .
  • Ridimensionare alle dimensioni desiderate utilizzando Bitmap.createScaledBitmap().

Controllare il seguente messaggio Android: Resize a large bitmap file to scaled output file per ulteriori dettagli.

Problemi correlati