I’ve seen a few questions regarding multipage tiffs and a few questions about compression, but none (that I’ve seen) linking the two. This question is as close as I’ve seen and puts me incredibly close, so I hope. I went into the Oracle forum thread mentioned (it is talking about a multipage PDF to TIFF with compression) and I think I’m closish to having the code finished to do this. Can anyone assist? I’m going to remove the try/catches to try and shorten this down a hair (basically all they did was output a message in console and return false).
public static boolean CompressedTiff(List<BufferedImage> images, File path)
{
if (!path.getParentFile().exists())
path.getParentFile().mkdirs();
path.createNewFile();
ImageOutputStream ios;
ios = ImageIO.createImageOutputStream(path);
Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("TIFF");
ImageWriter writer = (ImageWriter)imageWriters.next();
writer.setOutput(ios);
TIFFImageWriteParam writeParam = (TIFFImageWriteParam)writer.getDefaultWriteParam();
writeParam.setCompressionMode(2);
writeParam.setCompressionType("LZW");
writer.prepareWriteSequence(null);
for(int i = 0; i < images.size(); i++)
{
ImageTypeSpecifier spec = ImageTypeSpecifier.createFromRenderedImage(images.get(i));
javax.imageio.metadata.IIOMetadata metadata = writer.getDefaultImageMetadata(spec, writeParam);
IIOImage iioImage = new IIOImage(images.get(i), null, metadata);
writer.writeToSequence(iioImage, writeParam);
images.get(i).flush();//modified after release.
images.get(i).flush();
writer.endWriteSequence();
ios.flush();
writer.dispose();
ios.close();
}
return true;
}
It failed out on the next pass at writer.writeToSequence saying I needed to call prepareWriteSequence. I changed it to
writer.prepareWriteSequence(metadata);
writer.writeToSequence(iioImage, writeParam);
also removed the earlier writer.prepareWriteSequence(null);
and it appears to be navigating the files properly, however, the output isn’t of any type of renderable tif. Multipage or otherwise.
I have JAI installed so if it is possible to use that in some way to achieve the compressed image, that’d be fantastic. The code I am using that generates a TIFF is using this, but I haven’t seen anything that worked as far as adding compression to the pages.
edit: I added a bunch of ios.flush(); ios.close(); calls in the catch blocks and it prevents the non-renderable TIFF issue. However, it isn’t adding any page beyond the first.
If it helps, this is code that I use for modifying a TiffImageWriteParam to set the compression:
here is getTiffCompressor():
TiffCompression is my own enum that models the compressions that I offer for TIFF files. Finally, here is getCompressionType():
Now, I can’t show you everything because my code is built to encode an arbitrary number of images and yours is not, so our code structures differ wildly. In my case, I set up the encoder for using a sequence writer with a much more open architecture. I pull in an image, fire an event to optionally change the default compression, create the writer and write param, set metadata/image tags, fire progress events then write the sequence. Then I have to go in and patch the last written ifd because the tiff encoder writes them broken, so they need to be patched.