【发布时间】:2015-03-17 03:36:22
【问题描述】:
我想在 android 位图中将白色背景转换为透明背景。
我的情况:
原始图片:我无法发布图片
public Bitmap replaceColor(Bitmap src){
if(src == null)
return null;
int width = src.getWidth();
int height = src.getHeight();
int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);
for(int x = 0;x < pixels.length;++x){
pixels[x] = ~(pixels[x] << 8 & 0xFF000000) & Color.BLACK;
}
Bitmap result = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
return result;
}
处理后 它是逐个像素地检测的。 很好,但是这个位图图像不会保持原始颜色。
所以,我将代码附加到过滤器。
if (pixels[x] == Color.white)
public Bitmap replaceColor(Bitmap src){
if(src == null)
return null;
int width = src.getWidth();
int height = src.getHeight();
int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);
for(int x = 0;x < pixels.length;++x){
if(pixels[x] == Color.WHITE){
pixels[x] = ~(pixels[x] << 8 & 0xFF000000) & Color.BLACK;
}
}
Bitmap result = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
return result;
}
处理后,
但是,这张图片不能完全去除白色。 所以,它并不漂亮。
我真的想在 android 位图中删除白色背景
我的代码在stackoverflow文章下。
【问题讨论】:
-
这是因为 Color.white 是 FFFFFFFF。然而 FFFFFFFE 仍然是白的,但不会被这个算法捕捉到。该算法仅适用于精心制作的图像。
-
感谢您的评论。我不明白你的意思..?那么,我不能解决这个问题吗?
-
VVB ,您的解决方案不要使用我的情况,因为我不使用 R.imagefile 的 XML。因此,如果您知道其他解决方案,请写信至此处。
标签: android colors bitmap background transparent