我有原始灰度图像像素由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