用Java显示TIFF图像

时间:2009-10-14 16:18:23

标签: java swing tiff

有人可以告诉我如何在Java中加载多页TIFF图像并在JScrollPane中显示它吗?我可以使用哪个班级?

2 个答案:

答案 0 :(得分:4)

AFAIK,你不能用Java的标准API做到这一点。

然而,

JAI可以:

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

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

示例来自:http://java.sun.com/products/java-media/jai/forDevelopers/samples/MultiPageRead.java

答案 1 :(得分:0)

Sun的此示例代码使用1.0构造。特别是,javax.media.jai.widget.ScrollingImagePanel在1.1中已弃用。该文档解释说它无法显示某些图像,但没有给另一个类使用。

也行         TIFFDecodeParam param = null; 必须改为         ImageDecodeParam param = null; 为了获得编译代码。