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.

Are you a Java Developer working with Image files?

Read and write images in Java with JDeli

How to read and write images in Java

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.

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

  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. This syntax is like so:
    BufferedImage image = ImageIO.read(fileOrInputStreamOrURL)

How to read an 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
    BufferedImage image = JDeli.read(file);

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
    File file = new File("/path/to/outputFile.png"));
  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.

  1. Add JDeli to your class or module path. (download link to the trial jar).
  2. Create a File (or OutputStream) object
    File file = new File("/path/to/outputFile.png"));
  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);


Find out how to read and write images files in Java with JDeli:

Read: BufferedImage image = JDeli.read(streamOrFile);

Write: JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)

Learn more >>

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”

    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.