我正在尝试链接两个Renderscripts:ScriptIntrinsicBlur和ScriptIntrinsicColorMatrix。
我想模糊和图像,然后应用彩色滤镜。
这是我当前的代码(我尝试了许多不同的实现,包括ScriptGroups,我无法开始工作:
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 Experience的人能帮我解释原因吗?由于关于该主题的样本或文档非常少,我不得不尝试随机修改。
答案 0 :(得分:2)
这是正确的方法(或者至少有一种方法可行,这种情况有点儿麻烦):
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;