gpt4 book ai didi

创建 TIFF 图像时 Java 堆空间错误

转载 作者:行者123 更新时间:2023-12-02 07:52:29 24 4
gpt4 key购买 nike

由于我不熟悉使用 JAI 处理 TIFF,因此我尝试使用 float 据创建 6000*6000 的 RGB TIFF 图像。实际上,该代码适用于 5000*5000 图像,但当我增加大小时,我在 tiledImage.setData(pattern);

行收到 Java 堆空间错误

请告诉我使用平铺概念创建 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");
}
}

最佳答案

简单的解决方案是使用 -Xmx 选项增加堆大小。

关于创建 TIFF 图像时 Java 堆空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10070435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com