Convert a multi-page TIF into single-page TIFTag(s): AWT


Tagged Image File Format (abbreviated TIFF) is a file format for mainly storing images, including photographs and line art. TIFF was originally created as an attempt to get desktop scanner vendors of the mid-1980's to agree on a common scanned image file format, rather than have each company promulgate its own proprietary format.

This HowTo takes a multi-page TIF (from a FAX) and convert it into many single-pages TIF.

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

http://java.sun.com/products/java-media/jai/index.jsp

>
import java.io.*;

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;

import java.awt.image.RenderedImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.JAI;
import java.awt.image.renderable.ParameterBlock;


public class TestTiff {

    public static void main(String[] args) throws IOException {
        new TestTiff().doitJAI();
    }

    public void  doitJAI() throws IOException {
        FileSeekableStream ss = new FileSeekableStream("d:/multi.tif");
        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, null);
        int count = dec.getNumPages();
        TIFFEncodeParam param = new TIFFEncodeParam();
        param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
        param.setLittleEndian(false); // Intel
        System.out.println("This TIF has " + count + " image(s)");
        for (int i = 0; i < count; i++) {
            RenderedImage page = dec.decodeAsRenderedImage(i);
            File f = new File("d:/single_" + i + ".tif");
            System.out.println("Saving " + f.getCanonicalPath());
            ParameterBlock pb = new ParameterBlock();
            pb.addSource(page);
            pb.add(f.toString());
            pb.add("tiff");
            pb.add(param);
            RenderedOp r = JAI.create("filestore",pb);
            r.dispose();
        }
    }
}

This solution is working but JAI is a slow performer.

You may want to consider to use an external utility to do this kind on conversion. A nice one is irfanview (win), a (free) multi-purpose graphic utility.

http://www.irfanview.com/

To do a TIF conversion, use this command line :

C:\IrfanView\i_view32" d:\multi.tif /extract=(d:\,tif) /killmesoftly
irfanview is an amazing software when it comes to transform a graphic format to another one. You can use it from a command line or from a GUI.

See also : Convert many single-page TIF into one multi-page TIF


blog comments powered by Disqus