2012-08-06 197 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();