使用压缩缩小图像大小而不更改维度

时间:2012-08-06 12:03:25

标签: android compression android-image

如何在不改变图像尺寸的情况下减小图像尺寸(高度和宽度应该相同)。在android中我该怎么做?。我想降低图像的质量。我想通过网络发送它。所以大小应该更小。我需要类似于https://play.google.com/store/apps/details?id=com.shoozhoo.imageresizer&hl=en

的功能

2 个答案:

答案 0 :(得分: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,使用您选择的一个或提供正确的文件大小。

答案 1 :(得分: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();