2013-06-06 17 views
7

Voglio ridurre esattamente la dimensione di una bitmap a 200kb. Ricevo un'immagine dalla sdcard, comprimila e la salva sulla sdcard di nuovo con un nome diverso in una directory diversa. La compressione funziona bene (3 mb come l'immagine è compressa a circa 100 kb). Ho scritto le seguenti righe di codice per questo:Ridurre la dimensione di una bitmap a una dimensione specificata in Android

String imagefile ="/sdcard/DCIM/100ANDRO/DSC_0530.jpg"; 
Bitmap bm = ShrinkBitmap(imagefile, 300, 300); 

//this method compresses the image and saves into a location in sdcard 
    Bitmap ShrinkBitmap(String file, int width, int height){ 

     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inJustDecodeBounds = true; 
      Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 

      int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); 
      int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); 

      if (heightRatio > 1 || widthRatio > 1) 
      { 
      if (heightRatio > widthRatio) 
      { 
       bmpFactoryOptions.inSampleSize = heightRatio; 
      } else { 
       bmpFactoryOptions.inSampleSize = widthRatio; 
      } 
      } 

      bmpFactoryOptions.inJustDecodeBounds = false; 
      bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 

      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
      byte[] imageInByte = stream.toByteArray(); 
      //this gives the size of the compressed image in kb 
      long lengthbmp = imageInByte.length/1024; 

      try { 
       bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/compressed_new.jpg")); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     return bitmap; 
     } 
+0

modifica larghezza e altezza di quell'immagine .. – Riser

+0

vuoi dire rendere l'immagine 200x200? – TharakaNirmana

+0

sì, come tu vuoi 200kb .. prova fino ad ottenere il risultato .. – Riser

risposta

16

ho trovato una risposta che funziona perfettamente per me:

/** 
    * reduces the size of the image 
    * @param image 
    * @param maxSize 
    * @return 
    */ 
    public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 

     float bitmapRatio = (float)width/(float) height; 
     if (bitmapRatio > 1) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 
     return Bitmap.createScaledBitmap(image, width, height, true); 
    } 

chiamando il metodo:

Bitmap converetdImage = getResizedBitmap(photo, 500); 
  • foto è tua bitmap
+0

Questa funzione riduce la dimensione della bitmap se è più grande, ma per quanto riguarda le bitmap più piccole. Saranno ingranditi secondo le specifiche? – NarendraJi

+0

Perché fornisce una bitmap nera? – Sermilion

+1

@TharakaNirmana cos'è maxSize? nel tuo metodo di chiamata è 500, 500 kb o 500 mb? – TheQ

Problemi correlati