在没有 ImageObserver 的情况下在 Java 中获取图像的高度和宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3336558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:22:45  来源:igfitidea点击:

Getting Height and Width of Image in Java without an ImageObserver

javaimage-processingawtdimensions

提问by Bilzac

I am trying to get the heightand widthof images (via a url) in Java without an ImageObserver. My current code is:

我试图在没有 ImageObserver 的情况下在 Java 中获取图像的heightwidth(通过 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();
}

When i go into debug mode I am able to see that the image was loaded and I can see the heightand the widthof 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.

当我进入调试模式时,我能够看到图像已加载,并且可以看到图像的heightwidth,但我似乎无法返回它们。该getHeight()getWidth()方法需要的图像观察者,我没有。先感谢您。

回答by mdma

You can use ImageIconto handle the loading of the image for you.

您可以使用ImageIcon为您处理图像的加载。

Change

改变

Image img = Toolkit.getDefaultToolkit().getImage(url);
sb.append(line + ","+ img.getHeight(null) + "," + img.getWidth(Null) + newline);

to

ImageIcon img = new ImageIcon(url);
sb.append(line + ","+ img.getIconHeight(null) + "," + img.getIconWidth(Null) + newline);

The main change is to use ImageIcon, and the getIconWidth, getIconHeightmethods.

主要的变化是使用ImageIcon, 和getIconWidth,getIconHeight方法。

回答by YoK

Following should work

以下应该工作

   Image image = Toolkit.getDefaultToolkit().getImage(image_url);
   ImageIcon icon = new ImageIcon(image);
   int height = icon.getIconHeight();
   int width = icon.getIconWidth();
   sb.append(line + ","+ height + "," + width + newline);

回答by NightFury

One can get width and height using following code if there is difficulty in retrieving URL.

如果检索 URL 有困难,可以使用以下代码获取宽度和高度。

try {

File f = new File(yourclassname.class.getResource("data/buildings.jpg").getPath());
BufferedImage image = ImageIO.read(f);
int height = image.getHeight();
int width = image.getWidth();
System.out.println("Height : "+ height);
System.out.println("Width : "+ width);
              } 
catch (IOException io) {
    io.printStackTrace();
  }

Note:datais folder in /src containing images.

注意:data是 /src 中包含图像的文件夹。

Credit goes to Getting height and width of image in Java

归功于在 Java 中获取图像的高度和宽度