Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

How to read PNG files in Java

1 min read

In this article I will show you how to read PNG files in Java. I will show you how to do this using ImageIO, and secondly using JDeli.

We originally used ImageIO and JAI in our software but it didn’t meet our needs and so we created JDeli as a complete ImageIO replacement. Our PNG encoder and decoder speeds up the process of reading and writing PNGs. JDeli also lets you set a compression limit, allowing you to choose the best compression or the fastest compression. JDeli can read and write a very wide range of image formats. Here is the full list on Image file conversions supported.

First a quick introduction to PNGs…

What is a PNG?

PNG stands for Portable Network Graphics. It is a lossless image format popular on the world wide web because it supports transparency in browsers.

How do I read PNG Files?

ImageIO

ImageIO is included in the JDK (Javadoc here). Here is how to read a png file with ImageIO:

File imageFile = new File("image.png");
BufferedImage image = ImageIO.read(imageFile);

JDeli

JDeli is a Java image library from IDRSolutions (Javadoc and download).

You can easily replace ImageIO (and get much better support for PNG files) by just changing ImageIO.read to JDeli.read:

//Read Image (can also be OutputStream or byte array)
File imageFile = new File("image.png"); 
BufferedImage image = JDeli.read(imageFile);

Or you can use the JDeli PngDecoder directly:

File imageFile = new File("image.png");
PngDecoder decoder = new PngDecoder();
BufferedImage image = decoder.read(imageFile);

Alternatively, you can use the raw image data:

PngDecoder decoder = new PngDecoder();
BufferedImage image = decoder.read(rawPngData);

JDeli provides a complete PNG converter as it can read and write PNG image files and many other image formats.

You can find more tutorials in our series on handling common image formats in Java, JDeli and other image libraries, including how to write out PNG files in Java.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Bethan Palmer Bethan is a Java developer and a Java Champion. She has spoken at conferences including JavaOne/Code One, DevFest and NetBeans days. She has a degree in English Literature.

3 Replies to “How to read PNG files in Java”

Leave a Reply

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

IDRsolutions Ltd 2019. All rights reserved.