java中如何压缩字符串?
我想将任何图像作为转换为文本的消息发送。但那个文字太长了。我想尽可能地压缩该文本。我应该怎么办?如果您提供有关代码的一些帮助,我将非常感谢您。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您通过获取现有文件(例如 JPG)并对其进行 base64 编码将图像转换为文本,则没有太多可用的压缩 - 原始文件可能已经被相当程度地压缩了。
(我希望您没有使用
new String(bytes)
或任何类似的可怕的东西来转换任意二进制数据。)请注意,如果您的输出格式必须是文本,这使得事情变得更加困难 - 一个明显的解决方案是将现有文本转换为字节数组并压缩它...但是如果您需要转换回文本,您需要再次使用 base64,这会将其膨胀一个因子4/3。
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.
它实际上取决于输入图像的格式,但如果图像已经经过 JPEG 或 PNG(DEFLATE 压缩)等图像压缩算法处理,则无法再对其进行压缩。
不过,您无论如何都可以尝试压缩字符串。只需将其转换为字节数组并使用 java.util.zip 包。
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.
如果您正在处理 jpeg,那么根据定义,它不能进一步压缩,但它可能会编码得更好。
你是如何编码的?如果您使用的是 base-64 编码(将其编码为 64 个字母),那么您就做得很好。您也许能够将其编码为 128 个字符,这将是消息大小的一半,但是您需要找到 128 个唯一的 ASCII 字符,这些字符将全部通过您使用的任何介质传输(例如,0x08 可能通过一种介质传输,如一个独特的字符,但在另一种介质中它可能只是删除前一个字符)。
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).