问题
Let's say I have a very large TIFF image as input. I am not able to load this image entirely because of memory specification I must comply with. So the following is not an option :
BufferedImage data = ImageIO.read(image);
Is there any Java library allowing to read a specific part of the image without buffering the whole image ? Or some ways to get TIFF tiles from a stream ?
回答1:
ImageIO can provide you an ImageReader for Tiff, and then you can use readTile.
ImageIO has several getImageReadersBy...
methods.
I am not aware whether tiff is supported by ImageIO, but ImageIO uses java SPI so one may plug-in ImageReaders and ImageWriters.
In fact this is a short-cut for read
with ImageReadParam
configured for tiles.
Never used tiles, but seeing the prior answer, I wanted to point this option out.
回答2:
There is no way in the native Java libraries that's able to read a Tiff file in it's components.... so, you are stuck with either using an external library, or building your own.
@Joop has provided a link in to the native Java library I was not aware of (know your tools!) If you cannot find the full support you need there, you may find the following useful:
The specification for Tiff files is not hugely complicated. I would consider writing my own file reader for it.
- Wiki page
- File specification (pdf)
- Java Advanced Imaging API (JAI)
The Java JAI looks like it has signifciantly extended support for reading and parsing TIFF files. Consider:
- Documentation index
- Javadoc
- TiffDescriptor
回答3:
As a complement to @Joop's answer:
All ImageIO ImageReader
implementations support an ImageReadParam
with source region specified (ImageReadParam.setSourceRegion(rect)
), so that you can extract only a specific region of the entire image. This will work for any reader, even if the underlying format doesn't support tiling, and also regardless of the tile size for a tiled TIFF (or other format supporting tiles).
Example:
ImageReader reader = ImageIO.getImageReaders(input).next();
reader.setInput(input);
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(x, y, w, h));
BufferedImage aoi = reader.read(0, param);
That said, reading the image tile by tile is probably the most efficient way to achieve what you asked for, using the TIFF format. :-)
来源:https://stackoverflow.com/questions/26139065/how-to-read-a-tiff-file-by-tiles-with-java