从Java中的非常大的图像文件中读取区域

从Java中的非常大的图像文件中读取区域

问题描述:

是否有一个Java库可以读取超大图像(例如JPEG)文件的区域(> 10,000 x 10,000像素),而不会将整个图像保留在内存中。

Is there a Java library that can read regions of very large image (e.g. JPEG) files (> 10,000 x 10,000 pixels) without keeping the whole image in memory.

或者,哪个Java库能够以最小的开销处理非常大的图像文件。

Or alternatively, which Java library is capable of handling very large image files with a minimum of overhead.

标准ImageIO允许您读取(大)图像的区域而不首先将整个图像读入存储器。

Standard ImageIO allows you to read regions of (large) images without reading the entire image into memory first.

Rectangle sourceRegion = new Rectangle(x, y, w, h); // The region you want to extract

ImageInputStream stream = ImageIO.createImageInputStream(input); // File or input stream
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);

if (readers.hasNext()) {
    ImageReader reader = readers.next();
    reader.setInput(stream);

    ImageReadParam param = reader.getDefaultReadParam();
    param.setSourceRegion(sourceRegion); // Set region

    BufferedImage image = reader.read(0, param); // Will read only the region specified
}