首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chain 2 Renderscript Intrinsics : Blur & ColorMatrix

Chain 2 Renderscript Intrinsics : Blur & ColorMatrix
EN

Stack Overflow用户
提问于 2014-07-11 17:08:12
回答 1查看 1.1K关注 0票数 0

我试图链接两个Renderscript: ScriptIntrinsicBlur和ScriptIntrinsicColorMatrix。

我想要模糊和图像,然后应用一个颜色过滤器。

下面是我当前的代码(我尝试了许多不同的实现,包括ScriptGroups,我无法工作):

代码语言:javascript
复制
final ScriptIntrinsicBlur scriptBlur = ScriptIntrinsicBlur.create(
        mRenderScript,
        Element.U8_4(mRenderScript));
final Allocation input = Allocation.createFromBitmap(mRenderScript,
        bmp);
Bitmap blurOutBitmap = bmp.copy(bmp.getConfig(), true);
final Allocation output = Allocation.createFromBitmap(
        mRenderScript, blurOutBitmap);

scriptBlur.setRadius(mBlur_Radius);
scriptBlur.setInput(input);
scriptBlur.forEach(output);
bmp.recycle();
output.copyTo(blurOutBitmap);

mRenderScript.finish();

final ScriptIntrinsicColorMatrix scriptColor = ScriptIntrinsicColorMatrix
        .create(mRenderScript, Element.U8_4(mRenderScript));

/** for a first test, I am using a simple blue filter **/
Matrix3f mat = new Matrix3f(new float[] {
        1, 0, 1,
        0, 1, 1,
        0, 0, 1
});
scriptColor.setColorMatrix(mat);

final Allocation colorInput = Allocation.createFromBitmap(mRenderScript,
        blurOutBitmap);
Bitmap outBitmap = bmp.copy(blurOutBitmap.getConfig(), true);
final Allocation colorOutput = Allocation.createFromBitmap(
        mRenderScript, outBitmap);

scriptColor.forEach(colorInput, colorOutput);
blurOutBitmap.recycle();
colorOutput.copyTo(outBitmap);
displayBitmap(outBitmap);

这段代码在图像上产生非常难看的工件(并行红线),如果我尝试使用ScriptGroup,它就会崩溃。

有过Renderscript经验的人能帮我破译为什么吗?因为很少有关于这个主题的样本或文档,所以我不得不尝试随机修改。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-17 09:41:49

下面是正确的方法(或者至少一种可行的方法,这种情况有点错误):

代码语言:javascript
复制
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lenna);

ScriptIntrinsicBlur scriptBlur = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
scriptBlur.setRadius(5f);

ScriptIntrinsicColorMatrix scriptColor = ScriptIntrinsicColorMatrix.create(mRenderScript, Element.U8_4(mRenderScript));

final Allocation input = Allocation.createFromBitmap(mRenderScript, bitmap,
        Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT | Allocation.USAGE_SHARED);
scriptBlur.setInput(input);
Bitmap outBitmap = bitmap.copy(bitmap.getConfig(), true);
final Allocation output = Allocation.createTyped(mRenderScript, input.getType());


scriptColor.setColorMatrix(new Matrix4f(
        new float[]{1, 0f, 0f,   0,
                    1, 1,  0f,   0,
                    1, 0f, 1,    0,
                    0, 0,  0,    1}
));


ScriptGroup.Builder b = new ScriptGroup.Builder(mRenderScript);
b.addKernel(scriptBlur.getKernelID());
b.addKernel(scriptColor.getKernelID());
b.addConnection(input.getType(), scriptBlur.getKernelID(), scriptColor.getKernelID());
ScriptGroup group = b.create();

// group.setInput(scriptBlur.getKernelID(),input);
group.setOutput(scriptColor.getKernelID(), output);


group.execute();
output.copyTo(outBitmap);
return outBitmap;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24703035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档