Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read region from very large image file in Java

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.

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

like image 826
f3lix Avatar asked Aug 27 '13 13:08

f3lix


1 Answers

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
}
like image 175
Harald K Avatar answered Sep 20 '22 02:09

Harald K