【发布时间】:2020-09-18 08:43:09
【问题描述】:
我有一个位图,我需要删除所有具有 alpha 的像素。听起来很简单,但我还是坚持了下来。 我有这个 Java 代码:
public static Bitmap overdrawAlphaBits(Bitmap image, int color) {
Bitmap coloredBitmap = image.copy(Bitmap.Config.ARGB_8888, true);
for (int y = 0; y < coloredBitmap.getHeight(); y++) {
for (int x = 0; x < coloredBitmap.getWidth(); x++) {
int pixel = coloredBitmap.getPixel(x, y);
if (pixel != 0) {
coloredBitmap.setPixel(x, y, color);
}
}
}
return coloredBitmap;
}
它工作正常,但速度很慢,处理一张位图大约需要 2 秒。 我正在尝试使用 RenderScript。它工作得很快,但不稳定。 这是我的代码:
public static Bitmap overdrawAlphaBits(Bitmap image, Context context) {
Bitmap blackbitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), image.getConfig());
RenderScript mRS = RenderScript.create(context);
ScriptC_replace_with_main_green_color script = new ScriptC_replace_with_main_green_color(mRS);
Allocation allocationRaster0 = Allocation.createFromBitmap(mRS, image, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
Allocation allocationRaster1 = Allocation.createTyped(mRS, allocationRaster0.getType());
script.forEach_root(allocationRaster0, allocationRaster1);
allocationRaster1.copyTo(blackbitmap);
allocationRaster0.destroy();
allocationRaster1.destroy();
script.destroy();
mRS.destroy();
return blackbitmap;
}
还有我的 .rs 文件:
void root(const uchar4 *v_in, uchar4 *v_out) {
uint32_t rValue = v_in->r;
uint32_t gValue = v_in->g;
uint32_t bValue = v_in->b;
uint32_t aValue = v_in->a;
if(rValue!=0 || gValue!=0 || bValue!=0 || aValue!=0){
v_out->r = 0x55;
v_out->g = 0xED;
v_out->b = 0x69;
}
}
所以我在多个位图上使用此方法 - 第一个位图工作正常,但我收到损坏的图像。顺便说一句,当我在第一个位图上再次应用此方法时,它也会损坏它。 看起来没有关闭的内存分配或共享资源,idk。
有什么想法吗? 也许有更简单的解决方案? 提前谢谢大家!
【问题讨论】:
标签: android image-processing bitmap renderscript