0

I am trying to create a double-scripted array that has all the RGB values of an image in java. Afterwards, I want to see whether the pixel is black or white. However, the following code gives me an ArrayIndexOutofBounds Error and I'm not sure why. Any help would be greatly appreciated!

int[][] imageArr = new int[image.getTileHeight()+1][image.getTileWidth()+1];
    for (int a=0; a<image.getTileHeight(); a++)
    {
        for (int b=0; b<image.getTileWidth(); b++)
        {
            imageArr[a][b] = image.getRGB(a,b);               
        }
    }

Stack Trace of error:

java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
    at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:318)
    at java.awt.image.BufferedImage.getRGB(BufferedImage.java:888)
    at image2.main(image2.java:34)
Java Devil
  • 10,629
  • 7
  • 33
  • 48
user1755178
  • 255
  • 1
  • 4
  • 9

1 Answers1

0

If you are wanting to get it for the complete image then you need to use getHeight and getWidth not getTileHeight and getTileWidth in your loops and array initialisation.

These are causing the getRBG to throw the ArrayIndexOutOfBounds as it trying to get the image of the Raster data embedding within your BufferedImage

Java Devil
  • 10,629
  • 7
  • 33
  • 48