2010-03-30 9 views

Respuesta

10

En realidad es muy sencillo de hacer. Utilice

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

Actualización: El uso BitmapRegionDecoder

+3

Gracias Steve, pero eso requeriría que cargue en el mapa de origen en primer lugar, y estoy tratando de evitar hacerlo, ya que es demasiado grande para caber en la memoria, y quiero trabajar con un mapa de bits que no haya sido muestreado . – Schermvlieger

+0

Vaya, tiene razón, no leí esa parte de su pregunta correctamente. No estoy seguro de cómo hacer esto sin reducir la resolución. De hecho, ¡también hay un problema muy similar en mi propia lista de cómo hacer las cosas! –

9

probar esto

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 ... una cosa más que este método está destinado para la versión sdk 2.3 o superior ... – Renjith

0

@RKN

Su método también puede lanzar una excepción OutOfMemoryError - si excede de mapa de bits recortado VM.

Mi método combina el suyo y la protección contra este excepcion: (l, t, r, b -% de imagen)

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

y devuelve mapa de bits disponible max o nula

+0

¿Qué pasa si 'startingSize'> 32 inicialmente porque la imagen de entrada es tan grande? – TWiStErRob

0

Uso RapidDecoder.

y simplemente hacer esto

import rapid.decoder.BitmapDecoder; 

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

Requiere Android 2.2 o superior.

0

que es posible cargar la versión a escala de mapa de bits con la carga a cabo plenamente el mapa de bits utilizando algoritmo siguiente

  • Calcular la inSampleSize máxima posible que todavía se obtiene una imagen más grande que su objetivo.
  • Cargue la imagen con BitmapFactory.decodeFile (archivo, opciones), pasando inSampleSize como una opción .
  • Cambie el tamaño de las dimensiones deseadas utilizando Bitmap.createScaledBitmap().

Comprobar el siguiente post Android: Resize a large bitmap file to scaled output file para más detalles.

Cuestiones relacionadas