Share this page 

Display a TIFTag(s): AWT

The regular JDK can only deal with JPG, GIF, BMP or PNG file with the package ImageIO, see this HowTo.

To deal with TIF file, you must use the JAI (Java Advanced Imaging) package.

This example will display a given TIF file. It will also display other types (JPG,...) by detecting the type.

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.awt.Image;
import java.awt.image.RenderedImage;

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class ImageViewer  {

  static Image load(byte[] data) throws Exception{
    Image image = null;
    SeekableStream stream = new ByteArraySeekableStream(data);
    String[] names = ImageCodec.getDecoderNames(stream);
    ImageDecoder dec = 
      ImageCodec.createImageDecoder(names[0], stream, null);
    RenderedImage im = dec.decodeAsRenderedImage();
    image = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
    return image;

  public static void main(String[] args) throws Exception{
    String path;
    if (args.length==0) {
      path = JOptionPane.showInputDialog(null, "Image Path",
    else {
      path = args[0];
    FileInputStream in = new FileInputStream(path);
    FileChannel channel = in.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate((int)channel.size());;
    Image image = load(buffer.array());
    // make sure that the image is not too big
    //  scale with a width of 500 
    Image imageScaled = 
      image.getScaledInstance(500, -1,  Image.SCALE_SMOOTH);
    System.out.println("image: " + path + "\n" + image);
    JOptionPane.showMessageDialog(null, new JLabel(
        new ImageIcon( imageScaled )) );

The JAI package is composed of 2 jars : jai_core.jar and jai_codec.jar

To write a TIF file, see this HowTo