问题描述
为了获得图像的高度,我们可以使用 ImageIO.read(new URL("...")).getHeight()
.
In order to get image's height we can use ImageIO.read(new URL("…")).getHeight()
.
我的问题:
- 我是否正确理解此方法是先将图像下载到本地计算机,然后再进行大小计算?
- 如果是,那么映像的确切下载位置是下载到硬盘上的某些 JVM 缓存还是直接下载到 RAM?
- 有没有办法不用传输或下载就可以获取图像的高度?但是对服务器有某种请求?
推荐答案
首先,您的问题:
有点.首先,
ImageIO.read(...)
方法是使用所有默认参数解码文件中的第一个 图像的便捷方法.由于所有ImageIO.read(...)
操作都委托给格式化特定插件或ImageReader
实例(如JPEGImageReader
),这个 可能是特定于插件的.但一般情况下,图像数据是即时"传输和解码的.请注意,此处没有大小计算",在这种情况下,整个图像 被解码为BufferedImage
.必须传输解码文件中的第一个图像所需的尽可能多的数据,但不一定存储在计算机上的任何位置,除了解码的像素值.
Kind of. First of all, the
ImageIO.read(...)
methods, are convenience methods that will decode the first image in a file, using all default parameters for decoding. As allImageIO.read(...)
operations are delegated to format specific plugins orImageReader
instances (likeJPEGImageReader
), this may be plugin specific. But the general case, is that the image data is transferred and decoded "on the fly". Note that there's no "size calculation" here, rather the entire image is decoded to aBufferedImage
in this case. As much data as is needed to decode the first image in the file has to be transferred, but not necessarily stored anywhere on the computer, other than the decoded pixel values.
这取决于.有一个设置ImageIO.setUseCache(boolean)
,它控制数据是缓存在磁盘上还是内存中.
It depends. There's a setting ImageIO.setUseCache(boolean)
, that controls whether the data is cached on disk or in RAM.
- 如果设置为
true
,则数据临时存储在磁盘上,通常在默认临时目录中(请参阅java.io.tempdir
系统属性). - 如果设置为
false
,则在解码图像时,数据会临时存储在内存中.
- If the setting is
true
, the data is temporarily stored on disk, typically in the default temp directory (seejava.io.tempdir
System property). - If the setting is
false
the data is temporarily stored in memory while the image is decoded.
不,除非您的服务器有一个特殊的 API 来为您提供此数据,并且您针对此 API 执行特定请求.但是,ImageIO
API 确实有很多更精细的方法,可以让您更快地获得图像尺寸,而无需预先下载/解码整个图像.
No, not unless your server has a special API to give you this data, and you perform specific requests against this API. However, the ImageIO
API does have a lot more granular methods that allows you to get the image dimensions a lot faster, ans without downloading/decoding the entire image up front.
获取图像尺寸的更快方法是:
The faster way of obtaining the image dimensions is:
try (InputStream stream = url.openStream()) {
// The "useCache" setting will decide whether "input" below
// will be disk or memory cached
try (ImageInputStream input = ImageIO.createImageInputStream(stream)) {
ImageReader reader = ImageIO.getImageReaders(input).next(); // TODO: Handle no reader
try {
reader.setInput(input);
// Get dimensions of first image in the stream, without decoding pixel values
int width = reader.getWidth(0);
int height = reader.getHeight(0);
}
finally {
reader.dispose();
}
}
}
同样,根据图像格式,上面的代码应该只读取确定图像尺寸所需的标题/元数据.大多数 ImageReader
实现会为此读取所有标头数据,但与解码整个图像相比,它仍然要快得多,并且涉及的数据(和内存)要少得多.
Again, depending on image format, the above code should only read as much of the header/meta data as is needed to determine the image dimensions. Most ImageReader
implementations will read all header data for this, but still, it's much faster and involves a lot less data (and memory) than decoding the entire image.
很难对需要下载或需要下载多少数据做出任何假设,因为不同的格式具有不同大小的标头,底层传输(即 HTTP)可能会以块"等形式传输数据.
It's hard to make any assumptions as to how much data is or needs to be downloaded, because different formats have headers of varying size, the underlying transport (ie. HTTP) may transfer data in "chunks" etc.
这篇关于Java 中有没有什么方法可以在不传输或下载的情况下获取图像的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!