tiff 图像显示不正确

tiff image is not showing properly

我想加载一个非常大的 tiff 文件。我将堆大小更改为 800m 左右,因此图像已加载但未正确显示:

  1. 这是我的真实形象:

  2. 这是我加载后得到的截图:

这是我加载 tiff 文件的代码:

private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser fc = new JFileChooser(Database.basePath);

    FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("TIF Files", "tif");
    fc.setFileFilter(extensionFilter);
    int res = fc.showOpenDialog(null);
    File file = null;
    // We have an image!
    try {
        if (res == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            System.out.println(file.getAbsolutePath().toString().substring(0,
                    file.getAbsolutePath().toString().lastIndexOf("/")));
            path = file.getAbsolutePath().toString().substring(0,
                    file.getAbsolutePath().toString().lastIndexOf("/"));
            basepath = file.getAbsolutePath().toString().replace(".tif", ".xml");
            System.out.println("path:  " + basepath);
            setTarget(file);

            // System.out.println("canoic"+file.getCanonicalPath());
        } // Oops!

        else {
            JOptionPane.showMessageDialog(null, "You must select one image to be the reference.", "Aborting...",
                    JOptionPane.WARNING_MESSAGE);
        }

    } catch (Exception iOException) {
    }
public void setTarget(File reference) {
    try {
        targetImg = null;
        System.out.println("in try" + reference.getAbsolutePath());
        targetFile = reference;
        byte[] data = Files.readAllBytes(Paths.get(reference.getAbsolutePath()));
        targetImg = rescale(data);

    } catch (IOException ex) {
        System.out.println("in Catch");
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }

    panel_1.setLayout(new BorderLayout(0, 0));
    label = new JLabel(new ImageIcon(targetImg));
    JScrollPane scroll = new JScrollPane(label);
    panel_1.add(scroll);
    scroll.addMouseListener(this);
    scroll.addMouseMotionListener(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout(0, 0));

    setVisible(true);
}
public BufferedImage rescale(byte[] originalImage) {

    TiffDecoder decoder;
    BufferedImage decodedImage = null;
    try {
        decoder = new TiffDecoder(originalImage);
        decodedImage = decoder.read();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decodedImage;
}`

您可以使用 ImageIO#read

阅读您的图片
BufferedImage image = ImageIO.read(file);

然后将这张图片设置为ImageIcon

 label = new JLabel(new ImageIcon(image));

并且您需要将 jai_imageio.jar 放入您的类路径中