0

I want to reduce the size of an image on disk without losing quality. I don't want to change the width and height of original image.

I want to do something like https://tinyjpg.com/

It reduces the size of image without changing the width and height of image.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Raghav
  • 8,772
  • 6
  • 82
  • 106
  • "It reduces the size of image without changing the width and height of image" -- it reduces the disk space taken up by a JPEG. The resulting `Bitmap` would use exactly the same amount of heap space as it would before the conversion, as the heap space is solely dictated by height, width, and bit depth. "I want to reduce the size of image (bitmap)" -- are you referring to the size on disk (i.e., a file) or the size of a `Bitmap` in memory? – CommonsWare Jul 10 '15 at 13:17
  • Yes I am referring to size on disk – Raghav Jul 10 '15 at 13:27
  • 2
    There are [many existing tools for this](http://addyosmani.com/blog/image-optimization-tools/). You will need to port one or two of them to work as an Android library. – CommonsWare Jul 10 '15 at 13:57

1 Answers1

0

You need to compress with jpeg, if you want to reduce size without change measurements. Try this;

String file = ...
OutputStream s = new FileOutputStream(file);
BitmapFactory.decodeFile(file).compress(CompressFormat.JPEG, 80, s);
s.close();

80 is compression factor which between 0-100 (0 lowest quality, 100 highest quality). Try to find best for your situation.

Hyperion
  • 382
  • 8
  • 16
  • But JPEG compression is **lossy**, which the OP **wants to avoid**. `I want to reduce the size of an image on disk without losing quality.` – Phantômaxx Jul 10 '15 at 14:10
  • 1
    You want to compress any image without loss, and without change resolution ? AFAIK the only option is remove metadata from your image – Hyperion Jul 10 '15 at 15:15
  • OptiPNG does something similar with PNG files, which have a **lossless** compression. It also works on the color table, AFAIK. – Phantômaxx Jul 10 '15 at 15:51
  • PNG and JPEG are very different formats. Check [this infographic](http://lifehacker.com/learn-when-to-use-jpeg-gif-or-png-with-this-graphic-1669336151). If you try convert images from JPEG to PNG, file size will increase. – Hyperion Jul 10 '15 at 16:10
  • Which is... **obvious**, I would say. I work with images since 30+ years. Anyway, thank you for your expertise. It may be useful for someone. – Phantômaxx Jul 10 '15 at 17:27