首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Tiff获取总页数

如何从Tiff获取总页数
EN

Stack Overflow用户
提问于 2019-02-02 01:06:49
回答 2查看 1.6K关注 0票数 3

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

在这个类中,我发现了一个方法TIFFTweaker.getPageCount(),它看起来需要一个RandomAccessInputStream对象作为他们的getPageCount()

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

实现这一点并从tiff返回总页数的最佳方法是什么?

我看过一些java文档,stackOverflow和一些随机的博客,但似乎不知道如何从一个文件对象到一个randomaccessinputstream

代码语言:javascript
复制
@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文件中的总页数。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-02 03:46:09

这就是我要做的。@roeygol,谢谢你的回答。我曾尝试Maven导入依赖项,但该版本中的某些内容被破坏了。这是我想出来的。

代码语言:javascript
复制
    @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;

    }
票数 2
EN

Stack Overflow用户

发布于 2019-02-02 02:12:05

尝试使用以下代码:

代码语言:javascript
复制
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-codec:https://mvnrepository.com/artifact/com.sun.media/jai-codec/1.1.3

要使用的主要函数是getNumPages()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54484055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档