我正在尝试在没有 ImageObserver 的情况下在 Java 中获取图像的height
和width
(通过 url)。我目前的代码是:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File xmlImages = new File("C:\\images.xml");
BufferedReader br = new BufferedReader(new FileReader(xmlImages));
File output = new File("C:\\images.csv");
BufferedWriter bw = new BufferedWriter(new FileWriter(output));
StringBuffer sb = new StringBuffer();
String line = null;
String newline = System.getProperty("line.separator");
while((line = br.readLine()) != null){
if(line.contains("http")){
URL url = new URL(line.)
Image img = Toolkit.getDefaultToolkit().getImage(url);
sb.append(line + ","+ img.getHeight(null) + "," + img.getWidth(Null) + newline);
}
}
br.close();
bw.write(sb.toString());
bw.close();
}
当我进入调试模式时,我可以看到图像已加载并且我可以看到图像的height
和width
,但我似乎无法返回它们。getHeight()
and方法需要一个 Image Observer,而getWidth()
我没有。先感谢您。