用Java获取图像的宽度和高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18209887/
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-08-11 23:20:26  来源:igfitidea点击:

Get Image Width and Height in Java

javaimageimage-processing

提问by Dannika Rodriguez

I just want to ask how to get the width and height of an image, because this returns -1 for width and height:

我只是想问如何获取图像的宽度和高度,因为这将返回-1的宽度和高度:

private void resizeImage(Image image){
    JLabel imageLabel = new JLabel();

    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    System.out.println("Width:" + imageWidth);
    System.out.println("Height:" + imageHeight);
}

回答by Magnilex

Exactly why this happens in your case is unclear, you don't specify exactly what imageactually is.

在您的情况下发生这种情况的确切原因尚不清楚,您没有具体说明image实际情况。

Anyway, the answer can be found in the JavaDoc:

无论如何,答案可以在JavaDoc 中找到:

public abstract int getWidth(ImageObserver observer)

Determines the width of the image. If the width is not yet known, this method returns -1 and the specified ImageObserver object is notified later.

确定图像的宽度。如果宽度未知,则此方法返回 -1 并稍后通知指定的 ImageObserver 对象。

The width and height obiously cannot be immediately determined for the image in question. You need to pass an ImageObserverinstance which will have thismethod called when height and width can be resolved.

显然不能立即确定所讨论图像的宽度和高度。您需要传递一个ImageObserver实例,当可以解析高度和宽度时,该实例将调用方法。

回答by Josh M

    public static BufferedImage resize(final Image image, final int width, final int height){
    assert image != null;
    final BufferedImage bi = new BufferedImage(width, height, image instanceof BufferedImage ? ((BufferedImage)image).getType() : BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = bi.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();
    return bi;
}

The code posted above is one way of resizing an image. Generally to get the width and height of an image, you may do:

上面发布的代码是调整图像大小的一种方法。通常要获取图像的宽度和高度,您可以执行以下操作:

image.getWidth(null);
image.getHeight(null);

This is all under the assumption that the image is not null.

这都是在图像不为空的假设下进行的。

回答by Abstract

You should do something like this :

你应该做这样的事情:

BufferedImage bimg = ImageIO.read(new File(filename));
int width          = bimg.getWidth();
int height         = bimg.getHeight(); 

as this postsays

正如这篇文章所说

回答by Hendy Irawan

With Apache Commons Imaging, you can get image width and height with better performance, without reading entire image to memory.

使用Apache Commons Imaging,您可以获得具有更好性能的图像宽度和高度,而无需将整个图像读入内存。

Sample code below is using Sanselan 0.97-incubator (Commons Imaging is still SNAPSHOT as I write this):

下面的示例代码使用 Sanselan 0.97-incubator(在我写这篇文章时,Commons Imaging 仍然是 SNAPSHOT):

final ImageInfo imageInfo = Sanselan.getImageInfo(imageData);
int imgWidth = imageInfo.getWidth();
int imgHeight = imageInfo.getHeight();