最近,我发现渲染脚本是在 Android 上进行图像处理的更好选择。表演很精彩。但是关于它的文件并不多。我想知道是否可以通过渲染脚本将多张照片合并成一张结果照片。

http://developer.android.com/guide/topics/renderscript/compute.html 说:



是否有针对此问题的代码示例?

最佳答案

对于具有多个输入的内核,您必须手动处理其他输入。

假设您想要 2 个输入。

例子.rs:

rs_allocation extra_alloc;

uchar4 __attribute__((kernel)) kernel(uchar4 i1, uint32_t x, uint32_t y)
{
    // Manually getting current element from the extra input
    uchar4 i2 = rsGetElementAt_uchar4(extra_alloc, x, y);
    // Now process i1 and i2 and generate out
    uchar4 out = ...;
    return out;
}

java :
Bitmap bitmapIn = ...;
Bitmap bitmapInExtra = ...;
Bitmap bitmapOut = Bitmap.createBitmap(bitmapIn.getWidth(),
                    bitmapIn.getHeight(), bitmapIn.getConfig());

RenderScript rs = RenderScript.create(this);
ScriptC_example script = new ScriptC_example(rs);

Allocation inAllocation = Allocation.createFromBitmap(rs, bitmapIn);
Allocation inAllocationExtra = Allocation.createFromBitmap(rs, bitmapInExtra);
Allocation outAllocation = Allocation.createFromBitmap(rs, bitmapOut);

// Execute this kernel on two inputs
script.set_extra_alloc(inAllocationExtra);
script.forEach_kernel(inAllocation, outAllocation);

// Get the data back into bitmap
outAllocation.copyTo(bitmapOut);

10-08 09:18