Write Multiple Images Tiff File (JAI vs. ImageIO)

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]);
}
}

Advertisements

About lorddisk

WebCenter Content, WebCenter Portal, WebCenter Sites,Weblogic, Identity and Access Management (IAM),SSO,OAM,OIM,OAAM,OUD, OPAM,OID, OVD ,Oracle API Gateway ,OBIEE,OEDQ, Oracle ADF, Oracle SOA,J2EE, CackePHP ,PHP,J2SE,J2EE,Spring,Hibernate,JQuery,CSS,Java Script ,Joomla,Drupal,Worpress
This entry was posted in java and tagged , . Bookmark the permalink.

3 Responses to Write Multiple Images Tiff File (JAI vs. ImageIO)

  1. mohtashims says:

    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

  2. omar says:

    Excelente, muchas gracias.

  3. Denzoe says:

    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