如何在 JAVA 中将两个或多个 tiff 图像文件合并为一个多页 tiff 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3165792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine two or many tiff image files in to one multipage tiff image in JAVA
提问by Param-Ganak
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.
我有 5 个单页 tiff 图像。我想将所有这 5 张 tiff 图像合并为一张多页 tiff 图像。我正在使用 Java 高级成像 API。我已阅读 SUN 提供的 JAI API 文档和教程。我是 JAI 的新手。我知道基本的核心java。我不明白 SUN 的那些文档和教程。所以朋友们请告诉我如何将5个tiff图像文件组合成一个多页tiff图像。请给我一些关于上述主题的指导。我一直在互联网上搜索上述主题,但没有得到任何线索。所以请朋友们指导我。
Thanks in advance.
提前致谢。
采纳答案by Gilbert Le Blanc
I hope you have the computer memory to do this. TIFF image files are large.
我希望你有足够的计算机内存来做到这一点。TIFF 图像文件很大。
You're correct in that you need to use the Java Advanced Imaging (JAI)API to do this.
您是正确的,您需要使用Java Advanced Imaging (JAI)API 来执行此操作。
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.
首先,您必须将 TIFF 图像转换为java.awt.image.BufferedImage。这是一些可能会起作用的代码。我还没有测试过这段代码。
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.
然后,您将 BufferedImage 数组转换回多 TIFF 图像。我也没有测试过这段代码。
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.
祝你好运。