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)

1 min read

image formats java

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…

In this example, my original file was a jpg image, and I used JDeli to write it out as a png image:



Are you a Java Developer working with Image files?

Why do developers choose JDeli over free alternatives?

  1. Works with newer image formats such as AVIF, HEIC, JPEG XL, WEBP
  2. Better support than alternatives for JPEG, PNG, TIFF.
  3. Prevent JVM crashes caused by native code in other image libraries
  4. Better performance than other popular Java image libraries
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.