如何在不加载到内存的情况下调整图像大小?
我想检查图像的尺寸,如果宽度超过给定阈值,则缩小其尺寸。当我使用 ImageIO.read() 加载 6MB JPEG 图像时,BufferedImage 分配大约 45MB 的堆空间。有没有办法在不将所有数据加载到内存中的情况下进行直通图像调整大小?我尝试传递 ImageIO.read() 一个 File 对象,认为它会从磁盘传输数据,但这没有帮助。
I'd like to check the dimensions of an image, and then size it down if the width exceeds a given threshold. When I load a 6MB JPEG image with ImageIO.read(), the BufferedImage allocates about 45MB of heap space. Is there a way to do a passthrough image resize without loading all the data into memory? I tried passing ImageIO.read() a File object, thinking it would stream the data from disk, but it doesn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 im4java 和 JMagick。两者都使用名为 ImageMagick 的极其高效的外部工具来进行图像处理。 Im4Java 调用命令行应用程序(在单独的进程中工作),而 JMagick 通过 JNI 在同一进程中调用它。
如果我正在做简单的图像处理(调整大小、缩放、旋转等),我宁愿让 ImageMagick 为我做。与您在相似时间内实现的任何方法相比,它会更快、更可靠,并且消耗的内存更少。
希望这有帮助!
Check out im4java and JMagick. Both use an extremely efficient external tool called ImageMagick for image manipulation. Im4Java calls the command-line app (does work in separate process), and JMagick calls it within the same process via JNI.
If I'm doing simple image manipulation (resizing, scaling, rotating, etc), I'd much rather let ImageMagick do it for me. It'll be faster, more reliable, and consume less memory than anything you'd implement in a similar amount of time.
Hope this helps!
我之前使用过 jmagick 并取得了很好的效果,但是让它在不同的环境中工作可能会很繁琐(将罐子粘在正确的位置,确保您拥有正确的 imagemagick 版本等)。
您可能需要考虑使用 imgscalr (http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library)。它是一个开源的纯 java 图像处理库,可在 Apache 2.0 许可下使用。比那些可怕的 Java2D 东西更容易使用并且更快。
I've used jmagick before with excellent results, however it can be fiddly to get it working in different environments (sticking the jars in the right place, making sure you've got the correct build of imagemagick, etc).
You might want to consider using imgscalr (http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library). It's an open source, pure java image manipulation library available under the Apache 2.0 license. Much much (much..) easier to use and faster than that horrendous Java2D stuff.
您可以使用 ImageInfo 来检查图像的宽度和高度图像而不将其加载到内存中。我不知道有哪个库可以让您在不将图像加载到内存中的情况下调整大小。 45MB 确实不是那么多内存。您可以增加堆大小吗?
You can use ImageInfo to check the width and height of the image without loading it into memory. I do not know of a library that will allow you to re-size without loading the image in memory. 45MB is not all that much memory really. Can you just increase your heap size?