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

Did you know...

IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?

It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page

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 HEIC image files in Java with…

In this article, I will explain how to read HEIC files into Java as a BufferedImage. ImageIO does not read HEIC file types so...
Mark Stephens
1 min read

How to convert WMF files to SVG in java…

This article will show you how to convert WMF files into SVG files using our JDeli Java Image library. What is WMF? WMF is...
Amy Pearson
1 min read

How to write WebP images in Java

In this article, I will walk you through how to write out images as WebP images in Java. ImageIO does not support WebP images...
Mark Stephens
1 min read

Leave a Reply

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

IDRsolutions Ltd 2020. All rights reserved.