简体   繁体   English

andorid - 将组渲染脚本输出分配转换为位图

[英]andorid - Convert group renderscript output allocation to bitmap

Below group renderscript input is bitmap, output should be bitmap as well.下面组渲染脚本输入是位图,输出也应该是位图。 but when convert output allocation to bitmap, error happen:但是当将输出分配转换为位图时,会发生错误:

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)

code:代码:

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;
}

How to convert a Allocation object to bitmap?如何将分配对象转换为位图?

I am new in android .我是 android 新手。 I think renderscript documentation is very poor .I know you might be solved the problem though I have solution which is not better because it is flickering sometime .If you have best solution then and if u will share then it will very help full for me :) .我认为 renderscript 文档很差。我知道你可能会解决这个问题,尽管我有解决方案,但它不是更好,因为它有时会闪烁。如果你有最好的解决方案,如果你愿意分享,那么它对我来说非常有帮助: )。

val outallocation = group.execute(inAlloc)[0] as Allocation; // (in Kotlin)
outallocation.copyTo(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM