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 (Tutorial)

2 min read

image formats java

image formats

Why do Images cause problems for Java Developers?

Images cause problems for Java Developers because many of the more common Image formats are not fully supported by Java’s default ImageIO library. Support for some image formats (such as Tiff) has actually deteriorated in releases since Java 8.

In addition many third party alternatives are just Java wrappers around non-Java code (leading to memory and security issues) or require extensive code changes.

What options are there for supporting Image files in Java?

Java does have some support for Images files in ImageIO. There are also Open Source and Commercial options available, including Java wrappers on non-Java solutions.

The best Open Source solution is probably Apache Imaging which supports lots of formats.

ImageIO supports plugins which allows ImageIO code to work with other Image library without having to make code changes. The best Open Source plugin for ImageIO is TweleveMonkeys.

We recommend JDeli because it is a pure, complete Java implementation with no known security issues. It can be used with existing ImageIO code as a plugin and also has an API to write new code. It supports several other complex Image formats such as AVIF, HEIC and WEBP.

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.

If you are just looking for an Image Viewer, JDeli includes a built-in Image Viewer.

how to read and write images in 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 various 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 file in Java with ImageIO

  1. Create a File handle, InputStream or URL pointing to the raw image.
  2. ImageIO will now be able to read an image file into a BufferedImage.

and the Java code for reading an image in ImageIO…

File file = new File(“/path/to/image.png”);
BufferedImage image = ImageIO.read(file);

Reading image in Java with JDeli

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

and the Java code for reading an image in JDeli…

How to write out an image in ImageIO

This example shows the exact settings for a PNG image.

  1. Create a File (or OutputStream) object
  2. Pass image, PNG type and File (or OutputStream) object into write method

and the Java code for writing an image in ImageIO…

File file = new File(“/path/to/image.png”);
ImageIO.write(bufferedImage, "PNG", file);

How to write out an image with JDeli

This example shows the exact settings for a PNG image.

  1. Add JDeli to your class or module path. (download link to the trial jar).
  2. Create a File (or OutputStream) object
  3. Pass image, PNG type and File (or OutputStream) object into write method

and the Java code for writing an image in JDeli…

Other useful Image links

How can JDeli help?

JDeli is the best enterprise-level Java image library for image manipulation. You can use JDeli to read, write and convert your images as it has Image support. Visit our documentation to learn more about JDeli’s Image support.



Are you a Java Developer working with Image files?

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.

6 Replies to “How to read and write images in Java (Tutorial)”

    1. You can process the image in lots of ways. We provide lots of processing options or you can write your own in Java. I am not clear what you mean by fields – this is an image. Can you explain further?

Comments are closed.