Java 支持使用 ImageIO.read()
读取整个图像,但是有没有办法只读取部分图像?
背景:
我正在开发一个地图应用程序,读取大图像,将其分割,然后使用生成的图块作为地图的背景。在某些情况下,提供的图像可能太大,以至于将图像作为 BufferedImage
读取可能已经导致 OutOfMemoryError
。
我知道我可以授予虚拟机更多的堆空间,但我仍然更喜欢一种通过仅部分读取图像(即 256*256 像素的图块)来进一步降低风险的方法,我可以将其直接保存到文件系统。
Java supports reading of whole images with ImageIO.read()
, but is there a way of reading an image only partially?
Background:
I'm working on a map application that reads large images, splits them up, then uses the generated tiles as the background of a map. In some cases the provided image can be so large that reading the Image as a BufferedImage
might already cause an OutOfMemoryError
.
I'm aware I could grant the VM more heap space, still I'd prefer a way to further reduce the risk by reading the image only in parts, i. e. in tiles of 256*256 pixels, that I could save straight away to the file system.
发布评论
评论(2)
我已经很长时间没有使用它了,但是您可以使用 TiledImage 通过 JAI(Java 高级成像)来实现它。
JAI 主页。
It's a long time since I've used it but you can do it with JAI (Java Advanced Imaging) using a TiledImage.
JAI homepage.
如果您使用 PNG 格式,此库可让您逐行加载图像,因此无需将其完全加载到内存中 http://code.google.com/p/pngj/
In case you are using PNG format, this library lets you load an image line by line, so you don't need to load it fully in memory http://code.google.com/p/pngj/