且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在没有传输或下载的情况下,Java中是否有任何方法可以获取图像宽度和高度?

更新时间:2023-11-02 23:35:58

首先,你的问题:


  1. 种类。首先, ImageIO.read(...)方法是使用所有方法解码文件中第一个图像的便捷方法用于解码的默认参数。由于所有 ImageIO.read(...)操作都被委托给格式化特定插件或 ImageReader 实例(如 JPEGImageReader ),此可能是特定于插件的。但一般情况是,图像数据是在运行中传输和解码的。请注意,这里没有大小计算,而在这种情况下,整个图像被解码为 BufferedImage 。除了解码的像素值之外,解码文件中第一个图像所需的数据必须传输,但不一定存储计算机上的任何位置。

  1. 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 all ImageIO.read(...) operations are delegated to format specific plugins or ImageReader instances (like JPEGImageReader), 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 a BufferedImage 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),它控制数据是缓存在磁盘上还是缓存在RAM中。

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 (see java.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.