如何获取总页数从TIFF文件

问题描述 投票:3回答:2

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

在这个类中,我发现一个方法TIFFTweaker.getPageCount()它看起来像它想为他们RandomAccessInputStream一个getPageCount()对象。

我一直在玩弄试图从我的文件对象到他们正在寻找的东西得到。

什么是解决这个并返回从TIFF的总页数的最佳方式?

我已经看过了一些Java文档,计算器和一些随机的博客,但似乎无法弄清楚如何从一个文件对象的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文件的总页数。

java tiff icafe
2个回答
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
投票

尝试使用此代码:

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()

© www.soinside.com 2019 - 2024. All rights reserved.