开发者

How to resize an image without loading into memory?

开发者 https://www.devze.com 2023-03-06 03:39 出处:网络
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

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 passthr开发者_开发百科ough 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.


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!


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消