1

我想获取图像的每个 rgb 值并将这些值保存到二维数组。数组应该是这样的。

int [][] rgb = { { r, g, b}, { r, g, b}, .... }

这是我的代码

public int[][] extractImg(Bitmap photo) {
         //define the array size  
        int [][] rgb = new int[photo.getWidth()][photo.getHeight()];

        for(int i=0; i < photo.getWidth(); i++)  
        {  
            for(int j=0; j < photo.getHeight(); j++)  
            {  
                //get the RGB value from each pixel and store it into the array 
                 Color c = new Color(photo.getPixel(i, j), true);
                rgb[i][j] = { c.getRed(), c.getGreen, c.getBlue};  
            } 
        }  

        return rgb;
    }

我收到一条错误消息“数组常量只能在初始化程序中使用”。有没有可能将 rgb 矩阵保存到数组的方法?

4

1 回答 1

0

您不能通过以下方式为数组赋值:

rgb[i][j] = { c.getRed(), c.getGreen, c.getBlue}; 

除了语法不正确之外,您还忘记了颜色的 getGreen 和 getBlue 函数的大括号 ()。数组不会按照您指定的方式存储 rgb 值。如果您存储了一组颜色值,如下所示:

public Color[][] extractImg(Bitmap photo) {
     //define the array size  
    Color [][] rgb = new Color[photo.getWidth()][photo.getHeight()];

    for(int i=0; i < photo.getWidth(); i++)  
    {  
        for(int j=0; j < photo.getHeight(); j++)  
        {  
            //get the RGB value from each pixel and store it into the array as Color
            rgb[i][j] = new Color(photo.getPixel(i, j), true);   
        } 
    }  

    return rgb;
}
于 2013-11-15T09:36:58.547 回答