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
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
- Create a File handle, InputStream or URL pointing to the raw image.
- 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
- Add JDeli to your class or module path. (download the free trial jar).
- Create a File, InputStream pointing to the raw image. You can also use a byte[] containing the image data.
- Read the image into a BufferedImage
and the Java code for reading an image in JDeli…
File file = new File(“/path/to/image.png”);
BufferedImage image = JDeli.read(file);
How to write out an image in ImageIO
This example shows the exact settings for a PNG image.
- Create a File (or OutputStream) object
- 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.
- Add JDeli to your class or module path. (download link to the trial jar).
- Create a File (or OutputStream) object
- Pass image, PNG type and File (or OutputStream) object into write method
and the Java code for writing an image in JDeli…
File file = new File(“/path/to/image.png”);
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);
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?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
What is JDeli?
JDeli is a commercial Java Image library that is used to read, write, convert, manipulate and process many different image formats.
Why use JDeli?
To handle many well known formats such as JPEG, PNG, TIFF as well as newer formats like AVIF, HEIC and JPEG XL in java with no calls to any external system or third party library.
What licenses are available?
We have 3 licenses available:
Server for on premises and cloud servers, Distribution for use in a named end user applications, and Custom for more demanding requirements.
How does JDeli compare?
We work hard to make sure JDeli performance is better than or similar to other java image libraries. Check out our benchmarks to see just how well JDeli performs.
What if my images were JPG?
Works the same. Java will still read. We have articles on https://blog.idrsolutions.com/2017/02/how-to-read-jpeg-images-in-java/ and https://blog.idrsolutions.com/2015/04/how-to-write-jpeg-images-in-java/
Can we adjust angle of image with this library?
Yes. Have a look at https://support.idrsolutions.com/jdeli/tutorials/image-processing/rotate-an-image
Can we mask certain fields from the image?
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?