如何从Tiff获取总页数

时间:2019-02-01 17:06:49

标签: java tiff icafe

我已经开始在我们的项目中创建新方法以返回总页数。我们正在使用TIFFTweaker,可从以下URL-https://github.com/dragon66/icafe/blob/master/src/com/icafe4j/image/tiff/TIFFTweaker.java

进行引用

在此类中,我发现了一个方法TIFFTweaker.getPageCount(),看起来像它的RandomAccessInputStream想要一个getPageCount()对象。

我一直在尝试从文件对象中获取他们想要的东西。

解决这个问题并从tiff返回总页数的最佳方法是什么?

我查看了一些Java文档,stackOverflow和一些随机博客,但似乎无法弄清楚如何从文件对象访问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;

}

我期望返回一个数值,该数值代表我正在传递的TIFF文件中的总页数。

2 个答案:

答案 0 :(得分:2)

这就是我要做的工作。 @roeygol,谢谢您的回答。我曾尝试过将Maven导入依赖项,但是该版本中有些问题。以下是我想出了。

    @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;

    }

答案 1 :(得分:1)

尝试使用此代码:

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);
        }
    }
}

此代码的先决条件是使用Jai编解码器: https://mvnrepository.com/artifact/com.sun.media/jai-codec/1.1.3

要使用的主要功能是getNumPages()

相关问题