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 previously used ImageIO to read PNG files, but over time became increasingly dissatisfied as we discovered more and more issues. Eventually we wrote our own PNG Decoder which fixes those issues and is now available as part of JDeli.

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

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);

Why use JDeli?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • implements unsupported image formats
  • reduce output file size
  • improve read/write performance
  • supports threading
  • superior image scaling algorithms

Learn more about JDeli, or download to try it yourself.

This article is part of our series on reading and writing image 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 reasons Java developers switch to JDeli from ImageIO

ImageIO is build into the JDK and provides basic image support in Java. JDeli is a commercial image library for Java Developers from IDRsolutions....
Mark Stephens
1 min read

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.