2012-08-06 194 views
0

如何在不改變圖像尺寸的情況下縮小圖像大小(高度和寬度應該相同)。在android中我怎麼做到這一點?我想降低圖像質量。我想要所以尺寸應該更小。我需要的功能類似於https://play.google.com/store/apps/details?id=com.shoozhoo.imageresizer&hl=en使用壓縮縮小圖像大小不改變尺寸

+1

保存圖像爲JPG,它是一種壓縮格式。 http://stackoverflow.com/questions/4579647/how-to-save-a-jpeg-image-on-android-with-a-custom-quality-level – CSmith 2012-08-06 12:10:20

回答

1

請注意,鏈接的應用程序通過裁剪來縮小圖像的大小。

如果你想減少圖像的存儲字節,你需要使用較低的質量壓縮它。在這裏,您交易質量的大小。

這是一些代碼。

Bitmap bm = ... /* load your image into a bitmap object */ 

try { 
    OutputStream out = new FileOutputStream(new File("my-smaller-image.jpg") ; 
    bm.compress(Bitmap.CompressFormat.JPEG, 80 /* this is the quality parameter */, out) ; 
    out.flush() ; 
    out.close() ; 
} 
catch (Exception e) {} 

這裏的質量參數設置爲80,使用您的選擇或者給你正確的文件大小的一個。

+0

我會嘗試這個代碼.. – vmb 2012-08-06 12:54:16

0

使用本

  FileInputStream fin = new FileInputStream(file); 
      byte[] fileData = new byte[(int) file.length()]; 

      fin.read(fileData); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 16; 
      options.inPurgeable = true; 
      options.inScaled = true; 

      Bitmap bm = BitmapFactory.decodeByteArray(fileData, 0, 
        fileData.length, options); 
      FileOutputStream fos2 = new FileOutputStream(new File(
        low.getPath() + "/" + file.getName())); 

      bm.compress(CompressFormat.JPEG, 90, fos2); 

      byte[] result = xor(fileData, key); 
      fos = new FileOutputStream(file); 
      fos.write(result); 
      fos.close();