Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the color of the image changed when it compressed by Bitmap.CompressFormat.JPEG

I am trying to compress the photo took by the camera in Android. But the color of the image changed when it compressed by Bitmap.CompressFormat.JPEG. How can I solve this problem? Thanks

I have put some sample images which generated from my code. You can see the color of the paper on the top of the images is different.

Here is the code snippet:

Bitmap bitmap = BitmapFactory.decodeFile(Common.FOLDER_PATH + "pic.jpg");

FileOutputStream stream2 = new FileOutputStream(Common.FOLDER_PATH + "pic100.jpg");         
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream2); 

FileOutputStream stream3 = new FileOutputStream(Common.FOLDER_PATH + "pic100.png");         
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream3);

This is original image: original
This is JPEG: jpg
This is PNG: png

like image 473
mobile app Beginner Avatar asked Jun 06 '12 05:06

mobile app Beginner


People also ask

How does compression affect image quality?

Image file sizes without compression can be so large, that some computers are unable to store or process the data. Conversely, compression can make a file size so small that image quality becomes extremely poor.

How does the JPEG compression work?

The JPEG compression is a block based compression. The data reduction is done by the subsampling of the color information, the quantization of the DCT-coefficients and the Huffman-Coding (reorder and coding). The user can control the amount of image quality loss due to the data reduction by setting (or chose presets).

How do you tell if an image has been compressed?

You can pretty much determine if the file is compressed by looking at the file type. If you inspect the string toDataURL() produces, you will see a mime-type defining either a PNG or JPEG file - in some cases where browsers support other file formats you can also see BMP and ICO file formats.

Is bitmap compressed format?

BMP and PNG are both large, high-quality image file types. While BMP files are raw and uncompressed, you can compress PNG files to make them smaller and easier to manage. Read on to discover more about the main differences between the BMP and PNG formats.


1 Answers

JPEG is a lossy compression format and there may be loss of image information during the compression. The sacrifice of original image information is made for a better compression ratio (resulting in smaller file).

However, if this is not acceptable for you, you should use one of the lossless compression methods which includes the PNG.

like image 101
Rajesh Avatar answered Oct 21 '22 06:10

Rajesh