控制缓存在ImageIO读取中意味着什么

时间:2017-04-14 07:45:20

标签: 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;
        }

1 个答案:

答案 0 :(得分:3)

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

ImageIO中的缓存并不是很壮观,可能只是处理不可搜索的流。例如,当ImageIO需要确定图像的大小时,可能需要读取流的重要部分。然后可能需要再次重新读取该部分流以进行实际解码。

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

使用哪种类型的流留给ImageIO类,它根据缓存设置和底层媒体动态决定这一点。

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