Android从RGB 2D数组创建位图

时间:2016-03-16 18:04:14

标签: android bitmap rgb pixel

我正在开发一个android水印项目,我需要制作一个方法来为R,G和B生成2D数组,并在从其他人完成的WM方法中恢复之后重新创建图像。(I没有这个方法,所以我无法改变它)。

以下是我如何使用位图创建R,G,B:

Bitmap image = BitmapFactory.decodeFile("/sdcard/Watermarking/WM_20151208_183746_1282108897-1.jpg");
int height = image.getHeight();
int width = image.getWidth();
short [][] red = new short[height][width];
short [][] green = new short[height][width];
short [][] blue = new short[height][width];
int column = 0;
int row = 0;
while(row < height)
{
    while(column < width)
    {
        int pixel = image.getPixel(column, row);
        red[row][column] = (short) Color.red(pixel);
        green[row][column] = (short) Color.green(pixel);
        blue[row][column] = (short) Color.blue(pixel);
        column++;
    }
    row++;
}
displayImage(red, green, blue);

这是我正在做的方法,以测试我所做的是正确的:

public void displayImage(short [][] red, short [][] green, short [][] blue)
{
    short height = (short) red.length;
    short width = (short) red[0].length;
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    int column = 0;
    int row = 0;
    while(row < height)
    {
        while(column < width)
        {
            image.setPixel(column, row, Color.rgb(red[row][column], green[row][column], blue[row][column]));
            column++;
        }
        row++;
    }

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setImageBitmap (image);
}

出现的图像是黑色图像,即使它应该是正确的。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

通过简单地调试代码,您只是通过不重置列变量来重复填充数组的第一行

添加:

column = 0;

在两个循环之后(在填充和读取数组时);