您能告诉我这段代码有什么问题吗?
//bmp is a bitmap of already present image
int width=bmp.getWidth();
int height=bmp.getHeight();
int rgbval[]=new int[width*height];
bmp.getPixels(rgbval, 0, width, 0, 0, width, height);
rgbval=actual(rgbval);
Bitmap bmp2=bmp.copy(Bitmap.Config.ARGB_8888,true);
bmp2.setPixels(rgbval, 0, width, 0, 0, width, height);
actual
是我创建的用于处理bmp的rgb值的函数。通过使用eclipse的调试功能,我检查了它是否正常工作,但是当我尝试恢复bmp2的rgb值时,我没有得到操纵值。 最佳答案
首先,请参见以下答案:Android Bitmap setPixel doens't work correctly? (set value, then read a different value)
因此,如果您的原始图像似乎没有Alpha通道(即hasAlpha()返回false),它将自动将rgb值转换为预乘格式。
那就是你正在经历的吗? bmp.hasAlpha()是否为假?
我刚刚使用默认的Android图标作为位图测试了您的代码,hasAlpha()返回的是true。输入和输出颜色阵列中的Alpha通道相同。但是,对于2304个像素中的15个,红色通道会关闭一到两个。我没有检查蓝色或绿色通道。我认为这是内部舍入错误?我正在发布非常冗长且仅用于测试的代码,以防万一有人要添加其他内容。
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
int width=bmp.getWidth();
int height=bmp.getHeight();
int rgbval[]=new int[width*height];
bmp.getPixels(rgbval, 0, width, 0, 0, width, height);
int rgbval2[] = actual(rgbval);
Bitmap bmp2=bmp.copy(Bitmap.Config.ARGB_8888,true);
bmp2.setPixels(rgbval2, 0, width, 0, 0, width, height);
int rgb2[] = new int[width*height];
bmp2.getPixels(rgb2, 0, width, 0, 0, width, height);
int alpha1[] = new int[width*height];
int alpha2[] = new int[width*height];
int red1[] = new int[width*height];
int red2[] = new int[width*height];
for(int i=0; i<alpha2.length; ++i) {
alpha1[i] = Color.alpha(rgbval2[i]);
alpha2[i] = Color.alpha(rgb2[i]);
red1[i] = Color.red(rgbval2[i]);
red2[i] = Color.red(rgb2[i]);
if(red1[i]!=red2[i]) {
int a1 = alpha1[i];
int a2 = alpha2[i];
int r1 = red1[i];
int r2 = red2[i];
Log.e("E", String.format("a1: %d, a2: %d, r1: %d, r2: %d", a1, a2, r1, r2));
}
}
private int[] actual(int rgb[]) {
int rgb2[] = new int[rgb.length];
for(int i=0; i<rgb.length; ++i) {
rgb2[i] = Color.argb(Color.alpha(rgb[i]), Color.red(rgb[i])/2,
Color.green(rgb[i])/2, Color.blue(rgb[i])/2);
}
return rgb2;
}
这是LogCat的输出
a1: 64, a2: 64, r1: 30, r2: 32
a1: 64, a2: 64, r1: 30, r2: 32
a1: 142, a2: 142, r1: 58, r2: 57
a1: 142, a2: 142, r1: 58, r2: 57
a1: 216, a2: 216, r1: 56, r2: 55
a1: 216, a2: 216, r1: 56, r2: 55
a1: 57, a2: 57, r1: 6, r2: 4
a1: 59, a2: 59, r1: 6, r2: 4
a1: 231, a2: 231, r1: 90, r2: 91
a1: 216, a2: 216, r1: 95, r2: 94
a1: 216, a2: 216, r1: 95, r2: 94
a1: 217, a2: 217, r1: 57, r2: 58
a1: 216, a2: 216, r1: 88, r2: 89
a1: 199, a2: 199, r1: 66, r2: 67
a1: 199, a2: 199, r1: 39, r2: 38