在获取Java图像的高度和宽度没有的ImageObserver [英] Getting Height and Width of Image in Java without an ImageObserver
问题描述
我试图让高度
和宽度
Java中的图像(通过URL)中没有的ImageObserver 。
我现在的code是:
I am trying to get the height
and width
of images (via a url) in Java without an ImageObserver.
My current code is:
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();
}
当我进入调试模式下我能看到图像加载,我可以看到高度
和宽度
的形象,但我似乎无法归还。在的getHeight()
和的getWidth()
方法需要的图像观察者,我没有。谢谢你在前进。
When i go into debug mode I am able to see that the image was loaded and I can see the height
and the width
of the image, but i can't seem to return them. The getHeight()
and getWidth()
methods require an Image Observer, which i don't have. Thank you in advance.
推荐答案
您可以使用<一个href=\"http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/swing/ImageIcon.html\">ImageIcon处理图像的加载适合你。
You can use ImageIcon to handle the loading of the image for you.
修改
Image img = Toolkit.getDefaultToolkit().getImage(url);
sb.append(line + ","+ img.getHeight(null) + "," + img.getWidth(Null) + newline);
到
ImageIcon img = new ImageIcon(url);
sb.append(line + ","+ img.getIconHeight(null) + "," + img.getIconWidth(Null) + newline);
主要的变化是使用的ImageIcon
和 getIconWidth
, getIconHeight
方法。
这篇关于在获取Java图像的高度和宽度没有的ImageObserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!