搜索类似问题后,我想问以下问题:
我试图从 Google 静态 map 加载图片,但收到此错误:线程“main”java.lang.IllegalArgumentException 中出现异常:输入 == null!
在 javax.imageio.ImageIO.read(来源未知)
在 MapPicturePanel.main(MapPicturePanel.java:18)
以下是该类的完整代码:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MapPicturePanel {
private JScrollPane getContent(BufferedImage image) {
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return new JScrollPane(label);
}
public static void main(String[] args) throws IOException {
String path = "http://maps.googleapis.com/maps/api/staticmap?center=37.426616,-122.176380&zoom=14&size=800x600&scale=2&format=jpg&maptype=hybrid&sensor=false";
URL url = MapPicturePanel.class.getResource(path);
BufferedImage image = ImageIO.read(url);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new MapPicturePanel().getContent(image));
f.setSize(800,800);
f.setLocation(200,200);
f.setVisible(true);
}
}
最佳答案
您正在尝试在类路径中查找资源(这就是 Class.getResource 所做的)。但看起来您想从外部 url 加载图像。
就这样做
BufferedImage image - ImageIO.read(new URL(path));
关于java - 尝试使用 ImageIO.read 加载图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8460764/