本文介绍了用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.
推荐答案
Standard 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
}
这篇关于用Java从非常大的图像文件中读取区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!