2013-06-06 8 views
7

Ich möchte die Größe einer Bitmap genau auf 200kb reduzieren. Ich bekomme ein Bild von der SD-Karte, komprimiere es und speichere es erneut unter einem anderen Namen auf der SD-Karte in ein anderes Verzeichnis. Die Komprimierung funktioniert gut (3 mb wie das Bild wird auf etwa 100 kb komprimiert). Ich schrieb die folgenden Zeilen von Codes für diese:Reduzieren Sie die Größe einer Bitmap auf eine angegebene Größe 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

ändern Breite und Höhe des Bildes .. – Riser

+0

Sie meinen, das Bild 200x200 zu machen? – TharakaNirmana

+0

ja, wie Sie wollen 200kb .. versuchen Sie, bis Sie Ihr Ergebnis erhalten .. – Riser

Antwort

16

ich eine Antwort gefunden, die perfekt funktioniert für mich:

/** 
    * 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); 
    } 

Aufruf der Methode:

Bitmap converetdImage = getResizedBitmap(photo, 500); 
  • Foto ist Ihr Bitmap
+0

Diese Funktion reduziert die Bitmap-Größe, wenn sie größer ist, aber was ist mit den kleineren Bitmaps. Werden sie gemäß der Spezifikation vergrößert? – NarendraJi

+0

Warum gibt es schwarze Bitmap? – Sermilion

+1

@TharakaNirmana was ist maxSize? in Ihrer Methode ist das Anrufen 500, 500 kb oder 500 mb? – TheQ

Verwandte Themen