Reduce Image Size Using Compression Without changi

2019-06-13 15:37发布

How to reduce Image Size without changing image dimensions(Height and width should be same).In android How can i do this?.I want to reduce the quality of image.I want to send it over the network.So size should be smaller.I need a functionality similar to https://play.google.com/store/apps/details?id=com.shoozhoo.imageresizer&hl=en

2条回答
我只想做你的唯一
2楼-- · 2019-06-13 16:02

Note the application linked reduce the size of the image by cropping it.

If you want to reduce the stored bytes of an image you need to compress it using a lower quality. Here you trade quality for size.

Here is some code.

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) {}

Here the quality parameter is set to 80, use one of your choosing or giving you the correct file size.

查看更多
欢心
3楼-- · 2019-06-13 16:14

use this

            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();
查看更多
登录 后发表回答