BufferedImage像素与Color.getRGB()像素之间的差异

时间:2013-07-18 05:49:18

标签: java arrays colors bufferedimage pixels

我正在尝试从只包含灰度半透明图像(argb)的bufferedimage获取像素数据。 我从图像中获取了一个WritableRaster并使用Raster的setPixels方法设置其像素。 使用此方法,我得到ArrayIndexOutOfBounds Exceptions。在做了一些研究后,我发现BufferedImages每个像素每个像素存储一个整数,而不是每个像素有一个整数。也就是说,每个像素的每个频带为8位。 当我尝试访问和更改颜色类型时,这会给我带来很多麻烦。 我不知所措。 如果需要,我会发布任何其他细节。 谢谢, 新浪

1 个答案:

答案 0 :(得分:0)

你能分享一下代码吗?你必须注意,如果你想在图像的一部分上设置一个矩形(128x128),你必须像这样调用设置的RGB方法

//start points to draw
int blockX=128,blockY=128;
//size of the target rectangle need to be painted
int w=128,h=128;
int[] colors=new int[h*w];
//....init color array
//start painting from x value in array
int colorOffset=0;
//number of value should read from array for one row
int paintWidth=128;
//I think you get the Exception here, because of wrong number passed for drawing pixel for each row
out.setRGB(blockX,blockY, w, h, colors, colorOffset, paintWidth);

再次,我们不是魔术师,请分享代码伙伴:) this example也会帮助你