出于某种原因,我可以使用setRGB更改缓冲图像,但不能使用栅格中的实际int数组:
BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
int gray = (int) (MathUtil.noise(x, y) * 255); //I have tested the noise function, and know it works fine
img.setRGB(x, y, gray << 16 | gray << 8 | gray);
}
}
BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
int[] data = ((DataBufferInt) img.getData().getDataBuffer()).getData();
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
int gray = (int) (MathUtil.noise(x, y) * 255); //I have tested the noise function, and know it works fine
data[x + y * 32] = gray << 16 | gray << 8 | gray;
}
}
public static float noise(int x, int y) {
int n = x + y * 57;
n = (n << 13) ^ n;
return Math.abs((1.0f - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f));
}
没关系我修好了。我需要使用getRaster:P
答案 0 :(得分:3)
因为当你调用BufferedImage.getData()
时,它会返回一个副本,而不是实际的后备数组。因此,您直接对该数组所做的任何更改都不会反映在图像中。
来自JavaDoc for BufferedImage.getData():
<强>返回:强> 一个光栅,它是图像数据的副本。
编辑对Java 6 JavaDoc中的相同方法所说的内容有趣,它对副本的效果更为明确。我想知道他们为什么要改变它?
将图像作为一个大图块返回。返回的光栅是图像数据的副本,如果图像被更改,则不会更新
答案 1 :(得分:0)
答案是否就像数据数组中的更改未反映在img
对象中一样简单?