简体   繁体   中英

How to create an array of pixel values?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM