Write Multiple Images Tiff File (JAI vs. ImageIO)

Posted on Updated on

package other;

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedImageAdapter;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;

public class MultiPageRead {

public static void main(String[] args) {
String filepath = “D:\\Users\\hp\\Desktop\\test\\”;
PlanarImage[] images = new PlanarImage[7];
// images[0]=
RenderedImage renderedImage;
try {

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\CCITT_1.TIF”));
images[0] = (PlanarImage) new RenderedImageAdapter(renderedImage);

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\CCITT_2.TIF”));
images[1] = (PlanarImage) new RenderedImageAdapter(renderedImage);

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\CCITT_3.TIF”));
images[2] = (PlanarImage) new RenderedImageAdapter(renderedImage);

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\CCITT_4.TIF”));
images[3] = (PlanarImage) new RenderedImageAdapter(renderedImage);

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\CCITT_5.TIF”));
images[4] = (PlanarImage) new RenderedImageAdapter(renderedImage);

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\islam.png”));
images[5] = (PlanarImage) new RenderedImageAdapter(renderedImage);

renderedImage = javax.imageio.ImageIO.read(new File(filepath
+ “\\omar.tmp”));
images[6] = (PlanarImage) new RenderedImageAdapter(renderedImage);

} catch (IOException e) {

e.printStackTrace();
}

try {
saveTiffImageIO(images);
} catch (IOException e) {

e.printStackTrace();
}

}

private static void saveTiffImageIO(PlanarImage[] images)
throws IOException {
Iterator<ImageWriter> writers = ImageIO
.getImageWritersByFormatName(“tiff”);
ImageWriter imageWriter = writers.next();

ImageOutputStream ios = ImageIO.createImageOutputStream(new File(
“D:\\Users\\hp\\Desktop\\t.tiff”));
imageWriter.setOutput(ios);

imageWriter.prepareWriteSequence(null);
for (PlanarImage image : images) {
imageWriter.writeToSequence(new IIOImage(image, null, null), null);
}
imageWriter.endWriteSequence();

imageWriter.dispose();
ios.flush();
ios.close();
}

private static void saveTiffJAI(OutputStream out, PlanarImage[] images)
throws IOException {
TIFFEncodeParam param = new TIFFEncodeParam();
ImageEncoder encoder = ImageCodec
.createImageEncoder(“TIFF”, out, param);
List<PlanarImage> list = new ArrayList<PlanarImage>();
for (int i = 1; i < images.length; i++) {
list.add(images[i]);
}
param.setExtraImages(list.iterator());
encoder.encode(images[0]);
}
}

About these ads

3 thoughts on “Write Multiple Images Tiff File (JAI vs. ImageIO)

    mohtashims said:
    June 26, 2012 at 7:42 am

    I am getting the bew error executing your code.

    *******************************************************
    C:\Informatica\PowerCenter8.6.1\java\bin>java other.MultiPageRead
    Exception in thread “main” java.util.NoSuchElementException
    at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:808)
    at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:820)
    at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:805)
    at other.MultiPageRead.saveTiffImageIO(MultiPageRead.java:67)
    at other.MultiPageRead.main(MultiPageRead.java:55)
    *********************************************************

    Below is how my code looks…

    ***************************************************************

    package other;

    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.imageio.IIOImage;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriter;
    import javax.imageio.stream.ImageOutputStream;
    import javax.media.jai.PlanarImage;
    import javax.media.jai.RenderedImageAdapter;
    import com.sun.media.jai.codec.ImageCodec;
    import com.sun.media.jai.codec.ImageEncoder;
    import com.sun.media.jai.codec.TIFFEncodeParam;

    public class MultiPageRead {

    public static void main(String[] args) {
    String filepath = “C:\\Documents and Settings\\MS00109310\\Desktop”;
    PlanarImage[] images = new PlanarImage[5];
    // images[0]=
    RenderedImage renderedImage;
    try {

    renderedImage = javax.imageio.ImageIO.read(new File(filepath
    + “\\wudhu.jpg”));
    images[0] = (PlanarImage) new RenderedImageAdapter(renderedImage);

    renderedImage = javax.imageio.ImageIO.read(new File(filepath
    + “\\pj0072_1339141528734.JPG”));
    images[1] = (PlanarImage) new RenderedImageAdapter(renderedImage);

    renderedImage = javax.imageio.ImageIO.read(new File(filepath
    + “\\Angry Birds.JPG”));
    images[2] = (PlanarImage) new RenderedImageAdapter(renderedImage);

    renderedImage = javax.imageio.ImageIO.read(new File(filepath
    + “\\ISGTDrill.JPG”));
    images[3] = (PlanarImage) new RenderedImageAdapter(renderedImage);

    renderedImage = javax.imageio.ImageIO.read(new File(filepath
    + “\\outlooksearchissue.JPG”));
    images[4] = (PlanarImage) new RenderedImageAdapter(renderedImage);

    } catch (IOException e) {

    e.printStackTrace();
    }

    try {
    saveTiffImageIO(images);
    } catch (IOException e) {

    e.printStackTrace();
    }

    }

    private static void saveTiffImageIO(PlanarImage[] images)
    throws IOException {
    Iterator writers = ImageIO
    .getImageWritersByFormatName(“tiff”);
    ImageWriter imageWriter = writers.next();

    ImageOutputStream ios = ImageIO.createImageOutputStream(new File(
    “C:\\Documents and Settings\\MS00109310\\Desktop\\t.tiff”));
    imageWriter.setOutput(ios);

    imageWriter.prepareWriteSequence(null);
    for (PlanarImage image : images) {
    imageWriter.writeToSequence(new IIOImage(image, null, null), null);
    }
    imageWriter.endWriteSequence();

    imageWriter.dispose();
    ios.flush();
    ios.close();
    }

    private static void saveTiffJAI(OutputStream out, PlanarImage[] images)
    throws IOException {
    TIFFEncodeParam param = new TIFFEncodeParam();
    ImageEncoder encoder = ImageCodec
    .createImageEncoder(“TIFF”, out, param);
    List list = new ArrayList();
    for (int i = 1; i < images.length; i++) {
    list.add(images[i]);
    }
    param.setExtraImages(list.iterator());
    encoder.encode(images[0]);
    }
    }

    ******************************************************8

    omar said:
    October 14, 2012 at 3:37 am

    Excelente, muchas gracias.

    Denzoe said:
    January 16, 2014 at 11:19 pm

    excellent, this function works for read OLS STYLE JPEG (TIFF), thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s