3

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.

dragon66
  • 2,645
  • 2
  • 21
  • 43
Joe Z
  • 349
  • 3
  • 13
  • 1
    You can check the test for the `TIFFTweaker` for examples: https://github.com/dragon66/icafe/blob/master/src/com/icafe4j/test/TestTIFFTweaker.java – yegodm Feb 01 '19 at 19:48

2 Answers2

2

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;

    }
Joe Z
  • 349
  • 3
  • 13
0

Try to use this code:

import java.io.File;
import java.io.IOException;
import java.awt.Frame;
import java.awt.image.RenderedImage;
import javax.media.jai.widget.ScrollingImagePanel;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;

public class MultiPageRead extends Frame {

    ScrollingImagePanel panel;

    public MultiPageRead(String filename) throws IOException {

        setTitle("Multi page TIFF Reader");

        File file = new File(filename);
        SeekableStream s = new FileSeekableStream(file);

        TIFFDecodeParam param = null;

        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);

        System.out.println("Number of images in this TIFF: " +
                           dec.getNumPages()); //<< use this function to get the number of pages of your TIFF

        // Which of the multiple images in the TIFF file do we want to load
        // 0 refers to the first, 1 to the second and so on.
        int imageToLoad = 0;

        RenderedImage op =
            new NullOpImage(dec.decodeAsRenderedImage(imageToLoad),
                            null,
                            OpImage.OP_IO_BOUND,
                            null);

        // Display the original in a 800x800 scrolling window
        panel = new ScrollingImagePanel(op, 800, 800);
        add(panel);
    }

    public static void main(String [] args) {

        String filename = args[0];

        try {
            MultiPageRead window = new MultiPageRead(filename);
            window.pack();
            window.show();
        } catch (java.io.IOException ioe) {
            System.out.println(ioe);
        }
    }
}

Prerequisites for this code is to use jai-codec: https://mvnrepository.com/artifact/com.sun.media/jai-codec/1.1.3

The main function to be used for is getNumPages()

roeygol
  • 4,908
  • 9
  • 51
  • 88