0

I am working on an android app, which involves loading an jpeg image form the internal storage and the saving it back to the internal storage.

I load the bitmap of the image and save the image using the bitmap. imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)

But in saving the image, it gets compressed (even if I did not change anything in the image).

How can I load and save the same jpeg image without changing anything on the pixel level?

I'm looking for a way to write pixel by pixel (probably this is the only solution). I found a similiar way - link but this not only does not maintain the filetype as jpeg but also increases the file size 6 times. Although it maintains the exact pixel data.

Lupin
  • 19
  • `But in saving the image, it gets compressed (even if I did not change anything in the image).` You mean: If i compress that bitmap to a jpg file i get a different jpg file. Indeed. Now repeat with the new jpg file. Load it and compress to yet a new jpg file. All the jpg files have different file size. Do it ten times and look what happens. You know that jpg is a lossy compression algorithm? – blackapps Mar 07 '21 at 10:10
  • Use png files instead. Also do the ten times test. – blackapps Mar 07 '21 at 10:16
  • `not only does not maintain the filetype as jpeg but also increases the file size 6 times. ` Yes of course as then all pixels of the bitmap are saved in a classic .bmp file. And bitmaps are known for taking up a lot of memory. Hence the cry for compression which resulted in .jpg. – blackapps Mar 07 '21 at 10:20
  • A question: `AndroidBitmapUtil Android Bitmap Object To .bmp image file` It does not tell that it can read .bmp files to a bitmap. Can it? I suppose you tried it out. – blackapps Mar 07 '21 at 10:27
  • Yes, png does not cause any compression. But I need a `jpeg` image as output. Even though `jpeg` is a lossy compression, my point is that after a fix number of compressions, it should not be able to compress at all. I have observed that after the first compression(while saving the bitmap) the size decreases significantly, but after that the size remains almost constant on each compression. But still the pixels( and DCT coefficents) are scrambled. There should be a way to reverse the jpeg to bitmap compression to get the original image. – Lupin Mar 08 '21 at 02:55
  • No that is never possible as there is no info about the lost bits to restore them. Irreversable. And why didnt you answer my last question? – blackapps Mar 08 '21 at 09:12
  • `that after a fix number of compressions, it should not be able to compress at all.` You have a bitmap and the bitmap is compressed to a jpg image. The bitmap keeps the same resolution and amount of pixels and memory size. And that bitmap is allways compressed again to jpg. – blackapps Mar 08 '21 at 09:17
  • Further you say that after the first compression the resulting jpg file is smaller. That is my experience too. But it does not have to be so. If the compression algorithm of the Camera app would have been better then that of the Bitmap class you would have ended up with bigger files. – blackapps Mar 08 '21 at 09:21

0 Answers0