图像压缩java
我想通过网络套接字(java.net)传输图像。在哪里可以找到用于在java中压缩缓冲图像的示例代码。
例如,如果我的图像文件约为200kb,我希望
将其转换为至少100kb。 .这样传输会有点快..
谢谢..
I want to transfer Images via network socket(java.net).Where Can I find a sample Code for
compressing Buffered Images in java..Say for example If my Image file is around 200kb,I want
it to be converted to atleat 100kb..So that the transfer will be lil bit fast..
Thank you..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,最好的无损图像压缩是用 PNG 完成的,Java 中的图像库可以编写它。
如果您需要比这更小的图像,则需要使用有损压缩。一种简单的方法是使用 JPEG,您可以降低质量,直到获得所需的速率。
您还可以首先将图像缩小到原始尺寸的 1/2 或 1/3,给出更小的图片并传输它,然后在另一端放大它。这将可用于快速传输,然后您可以在运动稳定下来后传输完整图像。
To my knowledge the best compression without loss on images is done with PNG which the image libraries in Java can write.
If you need smaller images than that you need to use lossy compression. A simple way to do that is with JPEG, where you can turn down the quality until you get the rate you want.
You could also downscale the images first to 1/2 or 1/3 of the original dimensions givning a much smaller picture and transfer that, and upscale it in the other end. That will be usable for quick transfers, and you can then transfer a full image when the movements have settled down.
做到这一点的方法实际上取决于您想要做什么。
如果您想传输图像作为一些质量损失的代价,您可以将它们保存为 JPEG(在内存流中),传输这些字节数据并在另一侧再次读取图像。典型的 200KB 图像可以减少到 20-50K,并获得良好的结果质量。
阅读 Java 图像 I/O API< /a> 可以为此提供帮助。
如果您绝对想保留原始字节,或者想要更通用的解决方案,只需使用一些压缩库(如 Gzip)来压缩/解压缩您的数据。要开始使用,您可以使用使用 Java API 压缩和解压缩数据作为好的开始。
The way to do that really depend of what you want to do.
If you want to transfer image as the expend of some loss of quality, you can save them to JPEG (in a memory stream), transfer theses byte data and read the image again on the other side. A typical 200KB image could be reduced to 20-50K with a good resulting quality.
Reading The Java Image I/O API could help for that.
If you absolutely want to preserve the original bytes, or want a more generic solution, just use some compression library like Gzip to compress/decompress your data. To get you started you can use Compressing and Decompressing Data Using Java APIs as a good start.
您可以使用
ImageIO.write()
以受支持的压缩图像格式传输BufferedImage
,例如jpg
(有损压缩)或png
(无损压缩)。You could use
ImageIO.write()
to stream aBufferedImage
in a supported image format that's compressed, e.g.jpg
(lossy compression) orpng
(lossless compression).