Site logo,

Reading & writing images in Java


2012 Apr 17: this page now matches the radically restructured GRIP since version 12.1.1

Java's image reading and writing capabilities used to be very limited but since version 1.4 a new package has improved things enormously: javax.imageio (see Oracle's API documentation).

This package uses plug-ins for different image formats. The basic package reads and writes JPEG, PNG, BMP and GIF but JAR files are available to read other formats - importantly for us, including TIFF. The TIFF software is available from Oracle under Java Advanced Imaging (JAI) as Image-I/O Tools.

At a simple level there is a static method, (, which returns a java.awt.BufferedImage. However, there is more to be read from image files than simply the image. There may be metadata and there may be more than one image in the file - typically there will be a thumbnail image as well as the main one.

The imageio package defines a data structure, type IIOImage, which is able to hold all the images and metadata read from a file. This is used in the Image8 and Image16 classes in GRIP.

Another package called jrawio, from Tidal Wave, conforms to the JAI specification and enables RAW files in the formats of the major camera makers to be read. The JAR file for this is also provided with GRIP so that such files can be processed.

For reading and writing images as FITS files, GRIP has its own code, documented here (see class net.grelf.image.FITS). A third party package has not been used because it would involve copying a large amount of data from one structure to another after loading or before writing. GRIP uses a very memory-efficient structure for images deeper than 16 bits per channel, as was discussed here.

The easiest way to load and save images using GRIP's API is to use the following methods.

In net.grelf.image.ImageLoader:
  public static java.util.List <Image> loadAll (String imFilePath)
In any implementation of net.grelf.image.Image: 
  public void save (String imFilePath)

The image type will be determined by the extension on the file name at the end of the path. An image file may contain several images, typically a thumb-nail as well as the main image. So the loadAll () method returns a list of type Image.

Next page