gpt4 book ai didi

java - 如何在不完全加载图像的情况下读取图像的高度和宽度

转载 作者:行者123 更新时间:2023-12-02 11:27:17 25 4
gpt4 key购买 nike

我正在使用一些应用程序,其中在本地系统上加载了多个图像 URL。我需要获取我使用 ImageIO.read(File) 的每个图像的高度和宽度。这完成了工作,但首先将完整的图像加载到内存中,然后让我获取数据。但是我想知道是否有任何方法可以在不将图像加载或部分加载到内存的情况下获取图像尺寸。

最佳答案

引用thisthis链接

Obtaining an ImageInputStream is straightforward. Given an input source in the form of a File or InputStream, an ImageInputStream is produced by the call:

Object source; // File or InputStream
ImageInputStream iis = ImageIO.createImageInputStream(source);

Once a source has been obtained, it may be attached to the reader by calling:

reader.setInput(iis, true);

The second parameter should be set to false if the source file contains multiple images and the application is not guaranteed to read them in order. For file formats that allow only a single image to be stored in a file, it is always legal to pass in a value of true.

Once the reader has its input source set, we can use it to obtain information about the image without necessarily causing image data to be read into memory. For example, calling reader.getImageWidth(0) and reader.getImageHeight(0) allows us to obtain the width and height of the first image stored in the file respectively. A well-written plug-in will attempt to decode only as much of the file as is necessary to determine the image width, without reading any pixels.

关于java - 如何在不完全加载图像的情况下读取图像的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49530185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com