我有原始灰度圖像像素由short[]
表示。我想創建BufferedImage
並將其保存爲PNG。 由於沒有TYPE_SHORT_GRAY
定義BufferedImage
我創建一個自己這樣說:保存自定義BufferedImage
short[] myRawImageData;
// Create signed 16 bit data buffer, and compatible sample model
DataBuffer dataBuffer = new DataBufferShort(myRawImageData, w * h);
SampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_SHORT, w, h, 1, w, new int[] {0});
// Create a raster from sample model and data buffer
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, null);
// Create a 16 bit signed gray color model
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);
// Finally create the signed 16 bit image
BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
try (FileOutputStream fos = new FileOutputStream("/tmp/tst.png")) {
ImageIO.write(image, "png", fos);// <--- Here goes the exception
} catch (Exception ex) {
ex.printStackTrace();
}
到目前爲止好,但是當我試圖用ImageIO.write
將它保存爲PNG
我越來越ArrayIndexOutOfBoundsException
。
您應該a)將演示您的問題的代碼發佈爲[MCVE],並且b)描述發生的事情,您希望發生的事情,它們之間的差異以及遇到的具體錯誤。 – pvg