2010-07-14 30 views
5

Tengo un mapa de bits ... y si la altura del mapa de bits es mayor que maxHeight, o el ancho es mayor que maxWidth, me gustaría cambiar proporcionalmente la imagen para que quepa en maxWidth X Altura máxima. Esto es lo que estoy tratando:Redimensionar proporcionalmente un mapa de bits

BitmapDrawable bmp = new BitmapDrawable(getResources(), PHOTO_PATH); 

    int width = bmp.getIntrinsicWidth(); 
    int height = bmp.getIntrinsicHeight(); 

    float ratio = (float)width/(float)height; 

    float scaleWidth = width; 
    float scaleHeight = height; 

    if((float)mMaxWidth/(float)mMaxHeight > ratio) { 
     scaleWidth = (float)mMaxHeight * ratio; 
    } 
    else { 
     scaleHeight = (float)mMaxWidth/ratio; 
    } 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 

    Bitmap out = Bitmap.createBitmap(bmp.getBitmap(), 
      0, 0, width, height, matrix, true); 

    try { 
     out.compress(Bitmap.CompressFormat.JPEG, 100, 
       new FileOutputStream(PHOTO_PATH)); 
    } 
    catch(FileNotFoundException fnfe) { 
     fnfe.printStackTrace(); 
    } 

consigo la siguiente excepción:

java.lang.IllegalArgumentException: bitmap size exceeds 32bits

¿qué estoy haciendo mal aquí?

+0

Puede usted más allá del código corregido aquí? Estoy recibiendo la misma excepción – Mahesh

Respuesta

8

Su escalaAncho y escalaLa altura debe ser factores de escala (por lo tanto, no muy grandes) pero su código parece pasar en el ancho y la altura reales que está buscando. Entonces terminas incrementando masivamente el tamaño de tu mapa de bits.

Creo que hay otros problemas con el código para derivar scaleWidth y scaleHeight también. Para empezar, su código siempre tiene scaleWidth = width o scaleHeight = height, y cambia solo uno de ellos, por lo que también va a distorsionar la relación de aspecto de su imagen. Si solo desea cambiar el tamaño de la imagen, solo debe tener una sola scaleFactor.

Además, ¿por qué su estado if comprueba, efectivamente, maxRatio> ratio? ¿No debería estar revisando ancho> max Ancho o altura> maxHeight?

1

Esto es porque el valor de scaleWidth o scaleHeight es demasiado grande, scaleWidth o scaleHeight es decir ampliar o reducir la tasa 's, pero no width o height, demasiado grande plomo tasa a bitmap tamaño excede 32 bits

matrix.postScale(scaleWidth, scaleHeight); 
1

esto es cómo lo hice:

public Bitmap decodeAbtoBm(byte[] b){ 
    Bitmap bm; // prepare object to return 

    // clear system and runtime of rubbish 
    System.gc(); 
    Runtime.getRuntime().gc(); 

    //Decode image size only 
    BitmapFactory.Options oo = new BitmapFactory.Options(); 
    // only decodes size, not the whole image 
    // See Android documentation for more info. 
    oo.inJustDecodeBounds = true; 
    BitmapFactory.decodeByteArray(b, 0, b.length ,oo); 

    //The new size we want to scale to 
    final int REQUIRED_SIZE=200; 

    // Important function to resize proportionally. 
    //Find the correct scale value. It should be the power of 2. 
    int scale=1; 
    while(oo.outWidth/scale/2>=REQUIRED_SIZE 
      && oo.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; // Actual scaler 

    //Decode Options: byte array image with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize=scale; // set scaler 
    o2.inPurgeable = true; // for effeciency 
    o2.inInputShareable = true; 

    // Do actual decoding, this takes up resources and could crash 
    // your app if you do not do it properly 
    bm = BitmapFactory.decodeByteArray(b, 0, b.length,o2); 

    // Just to be safe, clear system and runtime of rubbish again! 
    System.gc(); 
    Runtime.getRuntime().gc(); 

    return bm; // return Bitmap to the method that called it 
} 
Cuestiones relacionadas