Convert many single-page TIF into one multi-page TIF Tag(s): AWT


This HowTo takes a list of single-page TIF and convert them into one multi-page TIF.

This is done with the JAI (Java Advance Image) package.

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.awt.image.BufferedImage;

import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import javax.media.jai.PlanarImage;

import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.TIFFEncodeParam;

public class TiffUtils {

  public static void main(String[] args) throws Exception {

    // 2 single page TIF to be in a multipage
    String [] tifs = {
        "C:/temp/test01.tif",
        "C:/temp/test02.tif"
    };
    int numTifs = tifs.length;  // 2 pages

    BufferedImage image[] = new BufferedImage[numTifs];
    for (int i = 0; i < numTifs; i++) {
        SeekableStream ss = new FileSeekableStream(tifs[i]);
        ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", ss, null);
        PlanarImage pi = new NullOpImage
            (decoder.decodeAsRenderedImage(0),null,null,OpImage.OP_IO_BOUND);
        image[i] = pi.getAsBufferedImage();
        ss.close();
    }

    TIFFEncodeParam params = new TIFFEncodeParam();
    params.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
    OutputStream out = new FileOutputStream("C:/temp/multipage.tif");
    ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
    List <BufferedImage>list = new ArrayList<BufferedImage>(image.length);
    for (int i = 1; i < image.length; i++) {
        list.add(image[i]);
    }
    params.setExtraImages(list.iterator());
    encoder.encode(image[0]);
    out.close();

    System.out.println("Done.");
  }
}

See also : Convert a multi-page TIF into single-page TIF


blog comments powered by Disqus