Reduce Image Size Using Compression Without changing Dimension

孤人 提交于 2020-01-03 05:42:12

问题


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


回答1:


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.




回答2:


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();


来源:https://stackoverflow.com/questions/11827894/reduce-image-size-using-compression-without-changing-dimension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!