为什么这个方法会抛出索引越界错误?尝试从我自己生成的数据中创建图像,我希望这会起作用。
private BufferedImage getImageFromFloatArray(float[] data, int w, int h) {
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
System.out.println("Image pixel array size: "
+ ((DataBufferInt) img.getRaster().getDataBuffer())
.getData().length);
System.out.println("Datasize: " + data.length);
WritableRaster raster = img.getRaster();
raster.setPixels(0, 0, w, h, data);
return img;
}
堆栈跟踪
Image pixel array size: 800000
Datasize: 800000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 800000
at java.awt.image.SampleModel.setPixels(Unknown Source)
at java.awt.image.WritableRaster.setPixels(Unknown Source)
at image.PixelAraryToImageTest.getImageFromFloatArray(PixelAraryToImageTest.java:36)