2

我正在尝试使用 ImageIO.read() 使用 1.7.0u JVM 加载此图像:

http://taste-for-adventure.tablespoon.com/files/2012/02/2012-02-05-poll-hotdog-275w.jpg

Chrome 对此没有任何问题,但 Java 会抛出以下异常:

java.lang.NullPointerException
    at java.awt.color.ICC_Profile.intFromBigEndian(ICC_Profile.java:1770)
    at java.awt.color.ICC_Profile.getNumComponents(ICC_Profile.java:1462)
    at sun.java2d.cmm.lcms.LCMSTransform.<init>(LCMSTransform.java:122)
    at sun.java2d.cmm.lcms.LCMS.createTransform(LCMS.java:76)
    at java.awt.color.ICC_ColorSpace.fromRGB(ICC_ColorSpace.java:222)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.setImageData(JPEGImageReader.java:635)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:550)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:295)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:427)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:543)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:986)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:966)
    at javax.imageio.ImageIO.read(ImageIO.java:1448)
    at javax.imageio.ImageIO.read(ImageIO.java:1400)

众所周知,Java 的图像阅读器很脆弱吗?是否有更强大的 Java 库来加载图像?

4

2 回答 2

2

嗨,请尝试以下方法,这在 java 1.6 中运行没有任何问题吗?它是否给出相同的例外?

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ShowImage {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Debug Frame");
        frame.setSize(200, 200);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Image image = null;
        JLabel label = null;

        try {
            image = ImageIO.read(new File("c:/scratch/hotdog.jpg"));
            label = new JLabel(new ImageIcon(image));
        } catch (IOException e) {
            label.setText("Image loading failed");
        }

        frame.add(label,BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
于 2012-04-11T14:21:34.327 回答
0

不确定您要做什么,您的代码中到底有什么,以及哪一行抛出 NullPointerException 但以下在 Java 6 中有效:

String imageUrl = "http://taste-for-adventure.tablespoon.com/files/2012/02/2012-02-05-poll-hotdog-275w.jpg";
BufferedImage bi = ImageIO.read(new URL(imageUrl));
if(bi != null)
    System.out.println("Image Loaded!");
else
    System.out.println("Something's wrong...");

抱歉,我现在无法在 Java 7 中对其进行测试...

于 2012-04-11T14:58:07.443 回答