Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut) making image size bigger than original

I want to compress the image taken from a camera to png format to make them smaller in size, so I'm using this code:

compressedPictureFile = new File(imagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
FileOutputStream fOut = new FileOutputStream(compressedPictureFile);
boolean compressed = bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
fOut.flush();
fOut.close();

The problem is that compressedPictureFile is actually bigger that the original image (from 1 Mb to 6Mb)

What am I missing? And is this the best way to reduce the size of an image?

Thanks

like image 480
Dany Y Avatar asked Apr 23 '13 19:04

Dany Y


1 Answers

The images taken with the camera are most likely stored in the jpg-format, which is lossy, but is relatively good for images with lots of colors (such as photographs).

When you compress the bitmap using your method you save it as a png. The png compression is lossless, but can achieve pretty small filesizes when there are few colors (such as in certain logotypes or other graphics). When the amount of colors and complexity in a png-file increases, so will the file size (this is why the camera saves a jpg - the quality/filesize ratio is much better than png for most photographs).

So, if you want to decrease the filesize of the photographs, use jpg compression and experiment with the quality-parameter. You might also want to decrease the resolution of the images, since this will save lots of space (a file with 50% resolution will be approx. 25% in data-size).

like image 84
Jave Avatar answered Sep 23 '22 07:09

Jave