I want find the average red RGB-value of a MyImage (extending BufferedImage). I save the red value for each pixel in the image to an array red[]. At the end I want to sum these ut to find the average value of red. But when I run this code on a MyImage it only prints 0. How can i get this method to work properly? (The code not included here works just fine)
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);
}
}