下面的组渲染脚本输入是位图,输出也应该是位图。但是将输出分配转换为位图时,会发生错误:
07-16 20:24:36.650 9640 9724 E RenderScript_jni: non fatal RS error, Failed to launch kernel; dimensions of input and output allocations do not match.
07-16 20:24:36.651 9640 9640 W System.err: androidx.renderscript.RSIllegalArgumentException: Object passed is not an array of primitives.
07-16 20:24:36.652 9640 9640 W System.err: at androidx.renderscript.Allocation.validateObjectIsPrimitiveArray(Allocation.java:87)
07-16 20:24:36.652 9640 9640 W System.err: at androidx.renderscript.Allocation.copyFrom(Allocation.java:834)
07-16 20:24:36.652 9640 9640 W System.err: at com.sample.groupScriptDemo(MainActivity.java:380)
代码:
private void groupScriptDemo() {
Bitmap src = BitmapFactory.decodeResource(getResources(),R.drawable.lena);
Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
RenderScript rs = RenderScript.create(this);
Allocation inAlloc = Allocation.createFromBitmap(rs, src,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT |
Allocation.USAGE_GRAPHICS_TEXTURE|
Allocation.USAGE_SHARED
);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
blur.setRadius(20.f);
ScriptIntrinsicColorMatrix gray = ScriptIntrinsicColorMatrix.create(rs, Element.U8_4(rs));
gray.setGreyscale();
ScriptGroup.Builder2 builder = new ScriptGroup.Builder2(rs);
ScriptGroup.Input unbound = builder.addInput();
ScriptGroup.Closure cblur = builder.addKernel(
blur.getKernelID(),
Type.createX(rs,Element.U8_4(rs),src.getWidth()*src.getHeight()),unbound);
ScriptGroup.Closure cgray = builder.addKernel(
gray.getKernelID(),
Type.createX(rs,Element.U8_4(rs),src.getWidth()*src.getHeight()), cblur.getReturn());
ScriptGroup group = builder.create("groupscript_demo",cgray.getReturn());
Object outObj = group.execute(inAlloc)[0];
Allocation outAlloc = Allocation.createTyped(rs, inAlloc.getType());
outAlloc.copyFrom(outObj);
outAlloc.copyTo(result);
inAlloc.destroy();
outAlloc.destroy();
group.destroy();
rs.destroy();
//showBitmap(this,src,ivInput);
//showBitmap(this,result,ivOutput);
return;
}
如何将分配对象转换为位图?