Merge Tiff Files Input Streams Using Java

SOVED : Merge Tiff Input Stream list and combine as a single Tiff input stream using java Solution :
public static InputStream mergeTiffFiles(InputStream[] contentElements) throws IOException
  {
   List<BufferedImage> images = new ArrayList<BufferedImage>();
   SeekableStream ss =null;
   try
   { 
     if(contentElements.length==1)
     {
       return contentElements[0];
     }
     //Iterate over each content element positioned at 0,1,2 in Filenet
     for(InputStream contentElement:contentElements)
     {
       ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", contentElement, null);

       int numPages = decoder.getNumPages();
       //Iterate over each page of the given document and add it to list of buffered images
       for(int j = 0; j < numPages; j++)
       {
         PlanarImage op = new NullOpImage(decoder.decodeAsRenderedImage(j), null, null, OpImage.OP_IO_BOUND);
         images.add(op.getAsBufferedImage());
       }
     }
     //write the merged stream to the file combined.tif in the same folder location
     TIFFEncodeParam params = new TIFFEncodeParam();
     FileOutputStream out = new FileOutputStream("combined.tif");
     ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, 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();
     // Read the merged tif file and delete after convert to Input stream
     File mergedFile = new File("combined.tif");    
     ss = new FileSeekableStream(mergedFile);
     mergedFile.delete();
    
   }
   catch (Exception e)
   {
     throw new DMFException(Constant.ERROR_WHILE_MERGING_TIFF, e);
   }
    
   return ss;
  }

0 comments:

Post a Comment