我想找到 MyImage 的平均红色 RGB 值(扩展 BufferedImage)。我将图像中每个像素的红色值保存到数组 red[] 中。最后,我想将这些 ut 相加以找到红色的平均值。但是当我在 MyImage 上运行这段代码时,它只打印 0。我怎样才能让这个方法正常工作?(此处未包含的代码可以正常工作)
public class MyImage extends BufferedImage {
public MyImage(int width, int height, int imageType) {
super(width, height, imageType);
}
public void findAverageRedValue(){
int height = this.getHeight();
int width = this.getWidth();
int[] red = new int[height*width];
Map m = new HashMap();
for(int i=0; i < width ; i++)
{
for(int j=0; j < height ; j++)
{
ColorModel colorModel = this.getColorModel();
int rCol = colorModel.getRed(j*i);
red[red.length - 1] = rCol;
}
}
int sum = 0;
for(int i = 0; i < red.length; i++)
sum += red[i];
int averageRed = sum/red.length;
System.out.println(averageRed);
}
}