简体   繁体   English

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

[英]Getting Height and Width of Image in Java without an ImageObserver

I am trying to get the height and width of images (via a url) in Java without an ImageObserver. 我试图在没有ImageObserver的情况下在Java中获取图像的heightwidth (通过URL)。 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();
}

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. 当我进入调试模式时,我能够看到图像已加载,我可以看到图像的heightwidth ,但我似乎无法返回它们。 The getHeight() and getWidth() methods require an Image Observer, which i don't have. getHeight()getWidth()方法需要一个我没有的Image Observer。 Thank you in advance. 先感谢您。

You can use ImageIcon to 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 , getIconHeight methods. 主要的变化是使用ImageIcongetIconWidthgetIconHeight方法。

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);

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: data is folder in /src containing images. 注意: data是/ src中包含图像的文件夹。

Credit goes to Getting height and width of image in Java 归功于Java中获取图像的高度和宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取图像的高度和宽度时,ImageObserver字段的作用是什么? - What is the purpose of the ImageObserver field when getting the height and width of an image? 在Java Applet中绘制Image和ImageObserver - Draw Image and ImageObserver in Java Applet 在没有传输或下载的情况下,Java中是否有任何方法可以获取图像宽度和高度? - Is there any way in Java to take image width and height without transfer or download? 通过Java缩小JPEG图像大小而不缩放其宽度/高度 - Reduce JPEG image size without scale its width/height by Java 获取Java中的图像宽度和高度 - Get Image Width and Height in Java 如何在不影响Java中的图像实际纵横比的情况下,为图像计算新的高度和宽度以适合预定义的高度和宽度? - How to calculate new height and width for an image to fit with in predefined height and width, without affecting the image real aspect ratio in java? 获取图像宽度和高度时发生NullPointerException - NullPointerException when getting image width and height 通过Java获取视频的高度和宽度 - Getting video height and width via Java 用Java将图像缩小到其高度和宽度的一半 - Shrink an image to half its height and width in java java函数确定具有给定宽度的图像的高度? - java function to determine height of an image with the given width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM