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 and write images in Java

1 min read

In this post, I will show you how to read and write image files in Java. I will demonstrate 2 different ways of doing this. The first is with the ImageIO, and the second is with our JDeli image library.

image formats java

 

How to read and write images in Java

Java provides a single type of object called a BufferedImage for images in Java.

A BufferedImage can be read from lots of different image types (ie BMP, HEIC, etc). Not all of these are supported by ImageIO itself but there are plugins to extend ImageIO, and other libraries such as Apache Imaging and JDeli.

In Java itself all the complexity of different image types is hidden – you work on a BufferedImage. Java allows direct access to the image pixels and colour information as well as allowing transformations and image processing.

Here is a list of how to read and write common Image file formats in Java.

How to read an image in Java with ImageIO

Step 1 Create a File handle, InputStream or URL pointing to the raw image.
Step 2 ImageIO will now be able to read an image file into a BufferedImage. This syntax is like so:

BufferedImage image = ImageIO.read(fileOrInputStreamOrURL)

How to read an image in Java with JDeli

Step 1 Add JDeli to your class or module path. (download the trial jar).
Step 2 Create a File, InputStream pointing to the raw image. You can also use a byte[] containing the image data.
Step 3 Read the image into a BufferedImage

BufferedImage image = JDeli.read(file);

How to write out an image in ImageIO

This example shows the exact settings for a PNG image.
Step 1 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 2 Pass image, PNG type and File (or OutputStream) object into write method

ImageIO.write(bufferedImage, "PNG", file);

How to write out an image with JDeli

This example shows the exact settings for a PNG image.
Step 1 Add JDeli to your class or module path. (download link to the trial jar).

Step 2 Create a File (or OutputStream) object

File file = new File("/path/to/outputFile.png"));

Step 3 Pass image, PNG type and File (or OutputStream) object into write method

JDeli.write(bufferedImage, "PNG", file);

In JDeli you can also use a typesafe version

JDeli.write(bufferedImage, OutputFormat.PNG, file);

or pass in a PngEncoderOptions object for more control over image output.

PngEncoderOptions options = new PngEncoderOptions();
JDeli.write(bufferedImage, options, file);

Why use JDeli to read and write Images in Java?

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

  • prevent heap related JVM crashes
  • support for additional image formats such as Heic
  • reduce output file size
  • improve read/write performance
  • create smaller files
  • control over output
  • support threading
  • superior image scaling algorithms



Tired of bad quality image libraries?

Write and Read Images in Java Find out why our customers trust JDeli with reading/writing images in Java

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.

2 Replies to “How to read and write images in Java”

Leave a Reply

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

IDRsolutions Ltd 2021. All rights reserved.