javatifficafe

How to Get Total Page Count From Tiff


I have started to create a new method in our project to return total pages. We are using TIFFTweaker which can be referenced from the following URL - https://github.com/dragon66/icafe/blob/master/src/com/icafe4j/image/tiff/TIFFTweaker.java

In this class I found a method TIFFTweaker.getPageCount() which looks like it wants a RandomAccessInputStream object for their getPageCount().

I've been playing around with trying to get from my file object over to what they're looking for.

What would be the best way to approach this and return the total pages from the tiff?

I have looked over some java docs, stackOverflow and some random blogs but can't seem to figure out how to get from a file object to a randomaccessinputstream.

@Override
public Integer totalPages(File file) {

Integer numberOfPages = 0;
try{
      //TIFFTweaker.getPageCount(); - How to pass the file and get the count? Problem is type is a random access input stream and I have a file type

       FileInputStream fileInputStream = new FileInputStream(file);
       String absolutePath = file.getAbsolutePath();

       // return TIFFTweaker.getPageCount();

  }catch(IOException e){
        log.error("Error with Tiff File" + e);
   }

return null;

}

I am expecting a numeric value returned which represents the total number of pages in the TIFF file I'm passing.


Solution

  • Here is what I got to work. @roeygol, thanks for your answer. I had tried to Maven import the dependency but something was broken in that version. Here is what I came up with.

        @Override
        public Integer totalPages(File file) {
    
            try(
                InputStream fis = new FileInputStream(file);
                RandomAccessInputStream randomAccessInputStream = new 
                FileCacheRandomAccessInputStream(fis)
            ){
    
               return TIFFTweaker.getPageCount(randomAccessInputStream);
    
           }catch(IOException e){
    
    
                log.error("Error with Tiff File" + e);
            }
    
            return null;
    
        }