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



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

你能分享一下代码吗?你必须注意,如果你想在图像的一部分设置一个矩形(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);

再一次,我们不是魔术师,请分享代码好友:)这个例子也将帮助您

最新更新