由于我是使用 JAI 处理 TIFF 的新手,因此我正在尝试使用浮点数据创建 6000*6000 的 RGB TIFF 图像。实际上该代码适用于 5000*5000 图像,但是当我增加大小时,我在行出现 Java 堆空间错误tiledImage.setData(pattern);
请告诉我用平铺概念创建 RGB/多波段 RIFF 图像是否是正确的方法。或者有什么方法可以创建?
错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferFloat.<init>(DataBufferFloat.java:53)
at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:409)
at javax.media.jai.RecyclingTileFactory.createTile(RecyclingTileFactory.java:397)
at javax.media.jai.PlanarImage.createWritableRaster(PlanarImage.java:1995)
at javax.media.jai.TiledImage.createTile(TiledImage.java:998)
at javax.media.jai.TiledImage.getWritableTile(TiledImage.java:1118)
at javax.media.jai.TiledImage.setData(TiledImage.java:1241)
at javaapplication17.Test3.main(Test3.java:69)
Java Result: 1
编码:
public class Test3 {
public static void main(String a[]) {
int imageHeight = 6000;
int imageWidth = 6000;
WritableRaster raster;
int[] bandOffsets = new int[3];
bandOffsets[0] = 2;
bandOffsets[1] = 1;
bandOffsets[2] = 0;
PixelInterleavedSampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, imageWidth, imageHeight, 3, 3 * 6000, bandOffsets);
// Origin is 0,0.
WritableRaster pattern = Raster.createWritableRaster(sm, new Point(0, 0));
float[] bandValues = new float[3];
bandValues[0] = 90;
bandValues[1] = 45;
bandValues[2] = 45;
// Set values for the pattern raster.
for (int y = 0; y < pattern.getHeight(); y++) {
for (int x = 0; x < pattern.getWidth(); x++) {
pattern.setPixel(x, y, bandValues);
bandValues[1] = (bandValues[1] + 1) % 255;
bandValues[2] = (bandValues[2] + 1) % 255;
}
}
ColorModel colorModel = PlanarImage.createColorModel(sm);
// Create a TiledImage using the SampleModel.
TiledImage tiledImage = new TiledImage(0, 0, imageWidth, imageHeight, 0, 0, sm, colorModel);
// Set the data of the tiled image to be the raster.
tiledImage.setData(pattern);
// Save the image on a file.
JAI.create("filestore", tiledImage, "rgbpattern_test4.tif", "TIFF");
}
}