I have been working for a couple of days now on an program that I am trying to get to separate a multi-page tiff image into single jpeg files, and then remerge multiple jpegs into a single multi-page tiff file for me. I have tried many approaches, mostly ones including the use of the JAI, ImageIO, and custom JAI/ImageIO libraries but I just cant seem to get it to merge 3 single jpeg files into a single TIFF file. For example, I have the following section of code:
private void pushTIFF(ArrayList<PlanarImage> images) throws IOException{
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("tiff");
ImageWriter imageWriter = writers.next();
ImageOutputStream pushOS = ImageIO.createImageOutputStream(new File(pathToTIFF));
imageWriter.setOutput(pushOS);
imageWriter.prepareWriteSequence(null);
for(int i = 0; i < images.size(); i ++){
imageWriter.writeToSequence(new IIOImage(images.get(i),null,null),null);
}
imageWriter.endWriteSequence();
imageWriter.dispose();
pushOS.flush();
pushOS.close();
}
Where the ArrayList images list is obtained elsewhere by decoding the source multi-page tiff image by using:
ImageCodec.createImageDecoder("tiff", tiffStream, null)
I have also tried other approaches using the ImageIO library instead, but I can't seem to wrap my brain around how to properly approach this problem. Running the method I listed above "pushTiff" seems to always lead to exceptions such as:
com.sun.media.jai.codecimpl.util.ImagingException: Unable to decode Packbits compressed data - not enough data.
As if the three pages obtained earlier are null or something, but when I check that they exist during runtime, they are there.
Basically, I am asking if anyone out there knows of a more up-to-date method of manipulating tiff files in java. In the end, I want to be able to break up a large tiff image into its smaller jpeg pages, and then remerge it into a tiff image that has a smaller overall filesize than what it started with.
Any ideas?