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

百般思念 提交于 2020-01-10 02:00:22

问题


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. So please guide me friends.

Thanks in advance.


回答1:


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(); 

Good luck.



来源:https://stackoverflow.com/questions/3165792/how-to-combine-two-or-many-tiff-image-files-in-to-one-multipage-tiff-image-in-ja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!