简体   繁体   English

如何创建像素值数组?

[英]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. 我正在尝试创建一个具有Java中图像的所有RGB值的双脚本数组。 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. 但是,下面的代码给我一个ArrayIndexOutofBounds错误,我不确定为什么。 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. 如果要获取完整的图像,则需要在循环和数组初始化中使用getHeightgetWidth而不是getTileHeightgetTileWidth

These are causing the getRBG to throw the ArrayIndexOutOfBounds as it trying to get the image of the Raster data embedding within your BufferedImage 这些导致getRBG在尝试获取嵌入BufferedImage中的Raster数据的图像时抛出ArrayIndexOutOfBounds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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