Wednesday, January 27, 2010

TIFF page extraction using JAI (Java Advanced Imaging)

     One of the most important parts in TIFF image development is multi-page tiff image extraction.  For image processing work, there are number of 3rd party software available in today’s market.  But when things comes to the online image processing work that time, either you write to a code for image operations as per your requirements or use a 3rd party software which support your applications.  Use of 3rd party software is not a good way to develop your application.  This is the only reason I went for our own implementations.
     Working with the DMS (Document Management System) / Document Image Processing, Digital Photography, Defense or Intelligent etc. are always required the image operations.  Multi-page TIFF image extraction is not an important or required part of my DMS project.  But the knowledge of tiff image extraction is a good for future prospects.  There are some parts of the codes available for tiff extraction on web world (just need to do good Google search).  To implement imaging part I decided to go with JAI (Java  Advanced Imaging).  Next topic we will see the advantages of JAI over other imaging APIs.

    At the time of TIFF page extraction, the main thing which we need to care is output image quality.  For me, it’s not important work to extract the pages from tiff image.  Quality is hardly matters as per my requirements.  That’s why I only need to extract the pages in other image type like .jpeg or .png etc.  I had set and used both types for extraction, JPEG for color images and PNG for black and white (binary/bilevel) images.  To maintain the quality of output image means to set the metadata for that image.  e.g. setting DPI of image, bit depth, horizontal and vertical resolutions etc.  I used default metadata for output image which gives 96 dpi horizontal and vertical resolutions. 

JAI (Java Advanced Imaging):
Implements a set of core image processing capabilities.
Include image tiling, regions of interest, and deferred execution.
Offer a set of core image processing operators.
Support image processing using the Java programming Language.

Why JAI?:
JAI offers lots of advantages for developers.
Platform Independent:  JAI applications will run on any machine where Java Virtual Machine is available.  It follows the Java run time library model which provides the platform independence. 

Object-oriented programming:  The API of JAI is object oriented.  It follows the object instantiation, flow of process data, concept of subclasses and parent classes, etc.

High Performance:  Using device independency we can do different types of implementations.  Its possible using JAI API.

Distributed Imaging:  JAI is also well suited as a client-server imaging programming.  Without affecting an object, remote method invocation (RMI) allows java code on a client to invoke method calls which placed in another machine.

Need to Import Packages:

(SUN Microsystems offers detailed API of above mentioned classes.  Use Google to search related API on SUN site for detailed information.)

Source Code:
 * Method for extraction
 * tiffFilePath: path of input tiff image
 * outputFileType: ouput image type (for color
 * image “jpeg” and for bilevel image “png”)
public static void extractMultiPageTiff(String tiffFilePath,
    String outputFileType) throws IOException {

     * create object of RenderedIamge to produce
     * image data in form of Rasters
    RenderedImage renderedImage[], page;
    File file = new File(tiffFilePath);
     * SeekabaleStream is use for taking input from file.
     * FileSeekableStream is not committed part of JAI API.
    SeekableStream seekableStream = new FileSeekableStream(file);
    ImageDecoder imageDecoder = ImageCodec.createImageDecoder("tiff",
            seekableStream, null);
    renderedImage = new RenderedImage[imageDecoder.getNumPages()];

    /* count no. of pages available inside input tiff file */
    int count = 0;
    for (int i = 0; i < imageDecoder.getNumPages(); i++) {
        renderedImage[i] = imageDecoder.decodeAsRenderedImage(i);

    /* set output folder path */
    String outputFolderName;
    String[] temp = null;
    temp = tiffFilePath.split("\\.");
    outputFolderName = temp[0];
     * create file object of output folder
     * and make a directory
    File fileObjForOPFolder = new File(outputFolderName);

     * extract no. of image available inside
     * the input tiff file
    for (int i = 0; i < count; i++) {
        page = imageDecoder.decodeAsRenderedImage(i);
        File fileObj = new File(outputFolderName
                + "/" + (i + 1) + ".jpg");
         * ParameterBlock create a generic
         * interface for parameter passing
        ParameterBlock parameterBlock = new ParameterBlock();
        /* add source of page */
        /* add o/p file path */
        /* add o/p file type */
        /* create output image using JAI filestore */
        RenderedOp renderedOp = JAI.create("filestore",

Java Advanced Imaging API:
Java Advanced Imaging Website:
Programming in JAI (Guide for developers):

Note:  Detailed information regarding JAI topic is available on SUN site. If you have any better idea or knew any simple and better solution than this, please share it with the blog topic.  Suggestions from your side are always welcome.


  1. you can try this free online image converter to convert tiff image.

  2. hi when i tried above code it worked in Windows but directory not gettng created in linux please help on this