Android 从 RGB 二维数组创建位图
Android create bitmap from RGB 2D arrays
我正在做一个 android 水印项目,我需要创建一个方法来为 R、G 和 B 生成二维数组,并在从已完成的 WM 方法中获取图像后重新创建图像由别人。(我没有这个方法,所以我不能改变它)。
这是我从位图中创建 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);
}
显示的图像是黑色图像,尽管它应该是正确的。感谢任何帮助。
通过简单地调试您的代码,您只是在不重置列变量的情况下重复填充数组的第一行
添加:
column = 0;
在两个循环之后(填充和读取数组);
我正在做一个 android 水印项目,我需要创建一个方法来为 R、G 和 B 生成二维数组,并在从已完成的 WM 方法中获取图像后重新创建图像由别人。(我没有这个方法,所以我不能改变它)。
这是我从位图中创建 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);
}
显示的图像是黑色图像,尽管它应该是正确的。感谢任何帮助。
通过简单地调试您的代码,您只是在不重置列变量的情况下重复填充数组的第一行
添加:
column = 0;
在两个循环之后(填充和读取数组);