java - ImageIO读取中的控制缓存意味着什么

标签 java caching image-processing bufferedimage javax.imageio

我对使用 BufferedImage 对象的唯一担心是,对于非常大的图像(例如 60000x32000),它将导致 JVM 在有限的 JVM 堆空间上因 OOM 而关闭。然而, ImageIO.read 方法的 JavaDocs 说了一些关于“控制缓存”的内容。

在这种情况下,什么是控制缓存?

这是否意味着 ImageIO.read 使用磁盘上的图像缓存来处理大图像?

引用下面的JavaDocs和ImageIO.read方法:

       /**
         * Returns a <code>BufferedImage</code> as the result of decoding
         * a supplied <code>File</code> with an <code>ImageReader</code>
         * chosen automatically from among those currently registered.
         * The <code>File</code> is wrapped in an
         * <code>ImageInputStream</code>.  If no registered
         * <code>ImageReader</code> claims to be able to read the
         * resulting stream, <code>null</code> is returned.
         *
         * <p> The current cache settings from <code>getUseCache</code>and
         * <code>getCacheDirectory</code> will be used to control caching in the
         * <code>ImageInputStream</code> that is created.
         *
         * <p> Note that there is no <code>read</code> method that takes a
         * filename as a <code>String</code>; use this method instead after
         * creating a <code>File</code> from the filename.
         *
         * <p> This method does not attempt to locate
         * <code>ImageReader</code>s that can read directly from a
         * <code>File</code>; that may be accomplished using
         * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
         *
         * @param input a <code>File</code> to read from.
         *
         * @return a <code>BufferedImage</code> containing the decoded
         * contents of the input, or <code>null</code>.
         *
         * @exception IllegalArgumentException if <code>input</code> is
         * <code>null</code>.
         * @exception IOException if an error occurs during reading.
         */
        public static BufferedImage read(File input) throws IOException {
            if (input == null) {
                throw new IllegalArgumentException("input == null!");
            }
            if (!input.canRead()) {
                throw new IIOException("Can't read input file!");
            }

            ImageInputStream stream = createImageInputStream(input);
            if (stream == null) {
                throw new IIOException("Can't create an ImageInputStream!");
            }
            BufferedImage bi = read(stream);
            if (bi == null) {
                stream.close();
            }
            return bi;
        }

最佳答案

在此上下文中,它仅意味着 read 方法将使用 getUseCachegetCacheDirectory 中的设置来控制是否允许缓存( getUseCache),如果是的话,它可以存储临时文件的位置(getCacheDirectory)。

ImageIO 中的缓存并不引人注目,可能仅用于处理不可查找的流。例如,当 ImageIO 需要确定图像的大小时,它可能需要读取流的很大一部分。然后,它可能需要再次重新读取流的该部分以进行实际的解码。

对于支持查找的文件和流,这不是问题,因为您可以在开始解码时重新读取前面的部分。对于 HTTP 流来说,没有这样的选项,在这种情况下,部分流可能需要存储在某处以便稍后解码。它可以位于内存 (MemoryCacheImageInputStream) 中或临时文件 (FileCacheImageInputStream) 中。

使用哪种类型的流由 ImageIO 类决定,该类根据缓存设置和底层媒体动态决定。

所以,我认为这在处理非常大的图像时不会有帮助。您仍然需要确保虚拟机有足够的空间来解码它们。

关于java - ImageIO读取中的控制缓存意味着什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43407381/

相关文章:

Java:使用引导类路径阻止找到 H2 驱动程序

ios - 我怎么知道要使用哪个 NSURLRequest CachePolicy

python - 透视单应性问题

performance - PostgreSQL 中的计数器缓存列

python - map_block 和 map_overlap 之间的处理时间差异是由于 dask.array 到 np.array 的转换吗?

python - 使用 ORB 的 OpenCV 图像对齐

java - 没有出现 fragment 的简单 ListView

java - 使用迭代器的 ConcurrentModificationException

java - 为什么 Google 的 Multimap 的 entries() 方法不返回键/集合对?

javascript - 将数据添加到 Angular $http 缓存