0

I'm developing an Android app where the server sends to the app (and viceversa) different base64 encoded images. Is it possible to compress the base64 string? something like

photo --> base64 encoded string --> compressed string

Doing so, how much data can I save, more or less? And, if I can save some byte, what is the best way to do the compression in Android and in Java?

In pure Java I sow here on Stackoverflow the Deflater class (that can do the work, but I don't know how much does it permits to save in terms of data).

Johan
  • 74,508
  • 24
  • 191
  • 319
Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34
  • 1
    Images are almost always already compressed. Base64 encoding **expands** it a bit, and then you can **shrink it** some with compression. But I thin this way you could only get a larger result, not smaller. – ppeterka Oct 05 '13 at 21:40

1 Answers1

3

The ratio of compression depends on the redundancy in the message.
Most image formats are already compressed, so there is no meaningful redundancy there.

Converting an image to base64 expands it by about 37%.
Compression is never 100% effective, and it always adds a little overhead (for headers and stuff).
So you're looking at 105% of the original image.

Note however that compressing a base64 file makes no sense, because the whole point of base64ing a file is to allow it to be send as text.
If you compress it it wil be binary once more.
In that case it's better to leave it as is.

If it needs to stay an emailable string
It needs to stay base64. Because the original is already compressed you cannot compress it further.

Alternatives
If you have a lossy algorithm (like jpeg), you can shrink the picture by expanding it to bitmap and then recoding it back to jpeg using different parameters.
This will result in some quality loss, but can be a great way to convert a 8MB picture into a 100kB picture.
Then you can base64 it and send it by email.

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319