2013-06-06 28 views
7

Bir bitmapi boyutunu tam olarak 200kb'ye azaltmak istiyorum. Sdcard'dan bir görüntü alıyorum, sıkıştırıp farklı bir dizine farklı bir adla tekrar sdcard'a kaydediyorum. Sıkıştırma iyi çalışıyor (3 mb gibi görüntü 100 kb'ye sıkıştırılıyor). Bunun için kodları aşağıdaki satırları yazdı:Bir bitmapi boyutunu Android'de belirtilen boyuta küçültün

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

yöntemini çağırarak:

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

değiştir Bu resmin genişliği ve yüksekliği .. – Riser

+0

görüntü 200x200 yapmak demek? – TharakaNirmana

+0

evet, istediğiniz gibi 200kb .. sonucunu elde edinceye kadar deneyiniz .. – Riser

cevap

16

benim için mükemmel çalışıyor bir cevap buldu

Bitmap converetdImage = getResizedBitmap(photo, 500); 
  • fotoğraf aşağıdadır bitmap
+0

Bu işlev, eğer daha büyükse, ancak daha küçük bitmap'ler hakkında bitmap boyutunu azaltacaktır. Şartname uyarınca büyütülecek mi? – NarendraJi

+0

Neden siyah bitmap veriyor? – Sermilion

+1

@TharakaNirmana maxSize nedir? Metodunuzda 500, 500 kb veya 500 mb mi arıyorsunuz? – TheQ