Java – how to combine multiple multi page TIF files into one TIF
I tried to take multiple pages TIF files and combine them into a multi - Page TIF file
I found some code in this question, but it seems to take only each individual The first page of the TIF file and creates a new multi - page with these first pages tif.
Whether there is a small change that I don't see will lead to the same code from the source Get each page from the TIF file and put them all into the combined tif?
For clarification, I want the source file:
> SourceA. TIF (page 3) > sourceb TIF (page 4) > sourcec TIF (1 page)
Merge into
> combined. TIF (8 pages)
I also hope to be able to specify TIF resolution and compression, but I'm not sure if Jai supports it, and it's not a necessary condition for the correct answer
I modified the code in the reference problem to load all the files in the directory TIF file to answer:
public static void main(String[] args) {
String inputDir = "C:\\tifSources";
File sourceDirectory = new File(inputDir);
File file[] = sourceDirectory.listFiles();
int numImages = file.length;
BufferedImage image[] = new BufferedImage[numImages];
try
{
for (int i = 0; i < numImages; i++)
{
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",ss,null);
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(0),null,OpImage.OP_IO_BOUND);
image[i] = op.getAsBufferedImage();
}
TIFFEncodeParam params = new TIFFEncodeparam();
OutputStream out = new FileOutputStream(inputDir + "\\combined.tif");
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff",out,params);
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 0; i < numImages; i++)
{
imageList.add(image[i]);
}
params.setExtraImages(imageList.iterator());
encoder.encode(image[0]);
out.close();
}
catch (Exception e)
{
System.out.println("Exception " + e);
}
}
Solution
I know I'm just missing out on something in a single A small part of the iteration page in TIF, I'm just not sure where it is
More Internet searches let me discover, not do:
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(0),OpImage.OP_IO_BOUND);
I want to iterate over each page in the current document with the following:
int numPages = decoder.getNumPages();
for(int j = 0; j < numPages; j++)
{
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(j),OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
This will take everyone Each page of TIF is added to the image list The last trap is the last call
encoder.encode(images.get(0));
Will result in the first page in the new TIF twice, so I added an intermediate loop and list filling, which will not add the first page in the call:
params.setExtraImages(imageList.iterator());
This keeps the first page out of "extraimages" and adds a call to the encoding
The final updated code is:
public static void main(String[] args) {
String inputDir = "C:\\tifSources";
File faxSource = new File(inputDir);
File file[] = faxSource.listFiles();
System.out.println("files are " + Arrays.toString(file));
int numImages = file.length;
List<BufferedImage> images = new ArrayList<BufferedImage>();
try
{
for (int i = 0; i < numImages; i++)
{
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",null);
int numPages = decoder.getNumPages();
for(int j = 0; j < numPages; j++)
{
PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(j),OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
}
TIFFEncodeParam params = new TIFFEncodeparam();
OutputStream out = new FileOutputStream(inputDir + "\\combined.tif");
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff",params);
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 1; i < images.size(); i++)
{
imageList.add(images.get(i));
}
params.setExtraImages(imageList.iterator());
encoder.encode(images.get(0));
out.close();
}
catch (Exception e)
{
System.out.println("Exception " + e);
}
}
