Search code examples
androidimage-compression

Compressing image in android is loosing the quality when image is taken from phone camera


I am using this method for compressing the image https://gist.github.com/ManzzBaria/c3af85b708fee49d55f7

but my images are loosing quality after compression. it is worse when an image is take from phone and compressed.

Please let me know where I am doing wrong.


Solution

  • Reducing the size of the image takes pixels away, effectively reducing the resolution of the image. Of course, the file also gets smaller. If you save as a high-fidelity JPEG (say 95%), this may be all the file size reduction you need.

    Reducing the size of the file with a lossless method (eg PNG) will achieve a reduction in file size with no loss of quality — hence the name. The reduction depends on the image: a picture with many identical pixels, eg a screenshot of this page, will compress substantially.

    Using a lossy method (eg normal JPEG compression) will result in an even smaller file, but you will pay for this reduction with a loss of quality. Most methods of saving JPEGs allow you to choose a fidelity, often 80% by default, which controls the quality of the resulting image; this is the line that does it in the code you posted:

    scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
    

    Again, the reduction you achieve is inversely proportional to the complexity of the image, as well as to this fidelity parameter.

    Here's a blog post I wrote on choosing image formats: How to choose an image format. This one might also be worth reading.

    There are more good links on lossy vs lossless compression in @Gavriel's answer.