Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He is responsible for managing the BuildVu product strategy and roadmap, and also spends a lot of his time writing code to build new features, improve functionality, fix bugs, and improve the testing for BuildVu.

Improving Image Output Performance in Java

1 min read

A common complaint from Java developers when using ImageIO in Java is that it is both slow to write out images, and that the images it produces are not well optimised and have large file sizes.

In a previous article I wrote about how to reduce image file size and recommend using PNGQuant, but how can you improve the speed at which images get written out?

Possibility 1:

One simple thing you can do if you are not already is to use a BufferedOutputStream, though the improvement is mostly negligible.

Possibility 2:

Another possibility is to write out JPEG files rather than PNG files which is faster in Java, but as JPEG does not support transparency, this becomes a bug if your images contain colors with alpha.

Possibility 3:

You could use a third party library such as PngEncoder by ObjectPlanet, which claims to be 5 times faster for a standard truecolour image and almost 7 times faster for a truecolour image with alpha (though I did not see improvements quite that large when experimenting).

My Suggestion:

Finally, a technique that I would recommend is to write images out on a separate thread, though this may not fit your use case. The key here is that ImageIO.write() is largely bound by IO and as such, whilst writing out images, your CPU is spending most of the time simply waiting for the disk to write the image out. This is time that your CPU could spend doing other tasks, such as generating the next image that needs to be written out. In our PDF to HTML5 converter we have exactly this scenario, where rather than waiting for the image to be written out we could spend that time decoding and converting the rest of the PDF file. This is a technique we have been using since our September 2014 release.

A problem with this solution is that ImageIO is not thread safe, which is why I recommend using an ExecutorService with a single thread. In order for your application to end successfully, you will need to wait for any images to finish being output.

If you have any suggestions for how to improve the performance of ImageIO.write(), please feel free to share it in the comments.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He is responsible for managing the BuildVu product strategy and roadmap, and also spends a lot of his time writing code to build new features, improve functionality, fix bugs, and improve the testing for BuildVu.

One Reply to “Improving Image Output Performance in Java”

  1. Stop using ImageIO and start using ImageMagick. I used to use ImageIO, but it has serious gaps in functionality and performance issues as your article outlines. If you instead use something like this: http://im4java.sourceforge.net/ then your capability and performance stop being an issue at all. Granted this requires ImageMagick to be installed on the machine, but if you’re working in a server environment that’s not a big deal.

Leave a Reply

Your email address will not be published. Required fields are marked *