1

I want to send any image as a message that is converted into text. But that text is too long . I want to compress that text as much as possible. What should I do? I would be very thankful to you if you provide some help regarding the code.

3 Answers 3

4

If you converted the image into text by taking an existing file (e.g. a JPG) and base64-encoding it, then there's not a lot of compression available - the original file is likely to be pretty heavily compressed already.

(I hope you weren't converting arbitrary binary data using new String(bytes) or anything similarly ghastly.)

Note that if your output format has to be text, that makes things even harder - one obvious solution would be to convert the existing text to a byte array and compress that... but if you need to convert back to text, you'd want to use base64 again, which will inflate it by a factor of 4/3.

Sign up to request clarification or add additional context in comments.

1 Comment

+1: The only step up is to use some sort of base 96 encoding (or a high based than 64) There is no standard way to do this I am aware of.
1

It actually depends on the format of input image, but you cannot compress it any more if the image is already processed by image compressing algorithms like JPEG or PNG (DEFLATE compressed).

You can however try to compress a string anyway. Just convert it to bytes array and use java.util.zip package.

Comments

1

If you are dealing with a jpeg then--pretty much by definition it can't be compressed much further, but it might be encoded better.

How are you encoding it? If you are using base-64 encoding (you are encoding it to 64 letters) you're doing pretty well. You MIGHT be able to encode it to 128 characters which will half the size of your message, but you need to find 128 unique ASCII characters that will all be transmitted across whatever medium you are using (for instance, 0x08 might transmit through one medium as a unique character but in another medium it might just delete the previous character).

2 Comments

"If you are dealing with a jpeg then--pretty much by definition it can't be compressed much further, but it might be encoded better." I am not quite sure whether 'encoded better' includes a higher compression level, but note that a JPEG with no compression (yes, they do exist) is a great deal larger in file size than another that uses high compression.
He means that a JPEG file can't be further losslessly compressed. That is, just compressing the bytes without regard for its content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.