为什么Java无法解析这个图像?

时间:2012-04-11 14:15:56

标签: java image-processing

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

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库来加载图像?

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

不确定你要做什么,你的代码中究竟是什么,以及哪一行抛出NullPointerException,但以下是Java 6中的fins:

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中测试它...