脚本全局变量肯定会帮助您解决问题。本质上,它们是 Java/Kotlin 端可以访问以设置或获取的脚本变量。如果您只能使用 Android 7+,则可以将其作为缩减内核(而不是映射内核)来执行。基本上,没有“输出”分配。
这是通过映射内核执行此操作的快速记录。我没有尝试或编译过这个,所以你可能需要调整它。假设您有两个Bitmap
对象并且它们的大小/格式相同(此处没有错误处理以保持简短),并且正在工作Activity
,您可以像这样设置它:
// You should only create the Rendescript context and script one
// time in the lifetime of your Activity. It is an expensive op.
Renderscript rs = Renderscript.create(this);
ScriptC_diffBitmap script = new ScriptC_diffBitmap(rs);
Allocation inAlloc1 =
Allocation.createFromBitmap(rs,
bitmap1,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
Allocation inAlloc2 =
Allocation.createFromBitmap(rs,
bitmap2,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
Allocation outAlloc = Allocation.createTyped(rs,
inAlloc2.getType());
script.set_maxX(0);
script.set_maxY(0);
script.set_minX(Int.MAX_VALUE);
script.set_minY(Int.MAX_VALUE);
script.set_inBitmap1(inAlloc1);
script.foreach_root(inAlloc2, outAlloc);
// Get back the min/max values and do whatever you need
minX = script.get_minX();
minY = script.get_minY();
maxX = script.get_maxX();
maxY = script.get_mayY();
支持这一点的 Rendescript 代码(同样,使用映射内核),命名为diffBitmap.rs
:
#pragma version(1)
#pragma rs java_package_name(com.example.DiffBitmap)
int32_t minX;
int32_t minY;
int32_t maxX;
int32_t maxY;
rs_allocation inBitmap1;
uchar4 RS_KERNEL root(uchar4 inBitmap2Point, uint32_t x, uint32_t y)
{
uchar4 inBitmap1Point = rsGetElementAt_uchar4(inBitmap1, x, y);
if ((inBitmap1Point.r != inBitmap2Point.r) ||
(inBitmap1Point.g != inBitmap2Point.g) ||
(inBitmap1Point.b != inBitmap2Point.b) ||
(inBitmap1Point.a != inBitmap2Point.a))
{
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
// Since we have to return an output, just return bitmap1
return inBitmap1Point;
}