本文介绍了如何在不加载到内存的情况下调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查图像的尺寸,然后在宽度超过给定阈值时将其缩小.当我使用 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.

推荐答案

查看 im4javaJMagick.两者都使用名为 ImageMagick 的极其高效的外部工具进行图像处理.Im4Java 调用命令行应用程序(在单独的进程中工作),而 JMagick 通过 JNI 在同一进程中调用它.

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.

如果我在做简单的图像处理(调整大小、缩放、旋转等),我宁愿让 ImageMagick 为我做.与您在类似时间内实施的任何方案相比,它会更快、更可靠且消耗更少的内存.

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.

希望这有帮助!

这篇关于如何在不加载到内存的情况下调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:15