在 Java 中显示 TIFF 图像

发布于 2024-08-08 05:36:55 字数 65 浏览 2 评论 0原文

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

Someone can tell me how to load a multipage TIFF image in Java and show it in a JScrollPane? Which class can I use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思碎 2024-08-15 05:36:55

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

示例来自:< a href="http://java.sun.com/products/java-media/jai/forDevelopers/samples/MultiPageRead.java" rel="nofollow noreferrer">http://java.sun.com/products/java -media/jai/forDevelopers/samples/MultiPageRead.java

AFAIK, you can't do that with Java's standard API.

JAI can however:

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

Example from: http://java.sun.com/products/java-media/jai/forDevelopers/samples/MultiPageRead.java

眼眸 2024-08-15 05:36:55

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

还有线
TIFFDecodeParam 参数 = null;
必须改为
ImageDecodeParam 参数 = null;
为了让代码编译。

This example code from Sun uses 1.0 constructs. In particular, the javax.media.jai.widget.ScrollingImagePanel was deprecated in 1.1. The documentation explains that it was unable to show some images but doesn't give another class to use.

Also the line
TIFFDecodeParam param = null;
must be changed to
ImageDecodeParam param = null;
in order to get the code to compile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文