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
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.
1 Comment
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
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).