How to combine two or many tiff image files in to one multipage tiff image in JAVA
Asked Answered
L

1

8

I have 5 single page tiff images. I want to combine all these 5 tiff images in to one multipage tiff image. I am using Java Advanced Imaging API. I have read the JAI API documentation and tutorials given by SUN. I am new to JAI. I know the basic core java. I dont understand those documentation and turorial by SUN. So friends Please tell me how to combine 5 tiff image file in to one multipage tiff image. Please give me some guidence on above topic. I have been searching internet for above topic but not getting any single clue.

Luz answered 2/7, 2010 at 12:53 Comment(1)
Using this Java image library - github.com/dragon66/icafe, you can easily merge two or any number of TIFF images together. Plus you DON'T need to decode the images and you can even merge multipage TIFFs as well.Sunset
S
12

I hope you have the computer memory to do this. TIFF image files are large.

You're correct in that you need to use the Java Advanced Imaging (JAI) API to do this.

First, you have to convert the TIFF images to a java.awt.image.BufferedImage. Here's some code that will probably work. I haven't tested this code.

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

Then, you convert the BufferedImage array back into a multiple TIFF image. I haven't tested this code either.

TIFFEncodeParam params = new TIFFEncodeParam();
OutputStream out = new FileOutputStream(output_dir + image_name + ".tif"); 
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
Vector vector = new Vector();   
for (int i = 0; i < numImages; i++) {
    vector.add(image[i]); 
}
params.setExtraImages(vector.listIterator(1)); // this may need a check to avoid IndexOutOfBoundsException when vector is empty
encoder.encode(image[0]); 
out.close(); 
Selma answered 2/7, 2010 at 13:40 Comment(4)
You're welcome. Now that you know about BufferedImage, you can do other photo transformations.Selma
@GilbertLeBlanc I hate to dig up an old thread like this, but I wanted to ask Gilbert a question. Using this code you provided above will result in a multi-page tiff image where the first place is duplicated (3 separate images becomes page 1,1,2,3). I believe it is because of the vector iterator that you initialize at the end going over the first image even though it is already set to be written. Is there any way around this?Caruthers
@user1806716 I have updated the answer to fix duplicated first page issue. This is a nice answer, works pretty well +1Avlona
I suspect if memory is at issue, you could implement a lazy iterator that converts the images as they are encoded?Mcmillon

© 2022 - 2024 — McMap. All rights reserved.