Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize image in Java with low memory consumption

Tags:

java

image

ImageMagick's "convert" command-line tool allows you to specify a limit on memory usage while performing its operations. I'd like to find a Java image-resizing library that gives the same option. My application sometimes gets very large JPEGs, and needs to downsample them to a reasonable size. For example, a 11600 x 5152 image will consume around 227MB of memory just decompressing it; the resizing process winds up using somewhat more than that.

Is there a way, in Java, to resize a very large image to a reasonable size without decompressing the entire image in memory?

like image 210
Ben Dilts Avatar asked Nov 12 '12 18:11

Ben Dilts


1 Answers

I think it was already discussed to some extent in this topic: What is the best java image processing library/approach?

However, Java itself provides an image manipulation and processing API which features the possibility of reading (and therefore, manipulating) pixel data in chunks; you could write code around it to perform as intended, but depending on your needs, the actual improvement on performance would not be worth the trouble.

A sure way of achieving the best possible performance, according to your needs, is through JNI, by using some sort of native solution (like pure C OpenGL code) to handle the image loading and processing. But you certainly realize that this could be considered overkill and not worth the trouble at all, for the very same reasons. Again, already discussed over here: Load a PNG image into Java as BufferedImage through JNI C code

like image 170
javabeats Avatar answered Oct 05 '22 00:10

javabeats