Android模糊与RenderScript

时间:2014-01-31 17:43:29

标签: android renderscript

我尝试在Android Bitmap上进行高斯模糊,但是我收到了这个错误:

rsAssert失败:!mTypes.size()和 rsAssert失败:!mElements.size()

这是我的代码:

public Bitmap blurBitmap(Bitmap src) {
    Bitmap outBitmap = src.copy(src.getConfig(), true);

    final RenderScript rs = RenderScript.create(this);
    final Allocation input = Allocation.createFromBitmap(rs, src);
    final Allocation output = Allocation.createFromBitmap(rs, outBitmap);

    final ScriptIntrinsicBlur script =
            ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(25f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(outBitmap);

    rs.destroy();

    return outBitmap;
}

请注意,我使用android.support.v8.renderscript来确保与Android较低版本的兼容性。

有人会有想法解决它吗?

感谢。

马丁

3 个答案:

答案 0 :(得分:2)

Element的{​​{1}}参数应与Allocation的元素相同,因此您应使用Allocation.getElement(),而不是直接ScriptIntrinsicBlur

Element.U8_4(rs)

您可能还希望将final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, input.getElement()); 移到final,因为每次位图不同时,其中一些可能会有所不同。

而且仅供参考,class private member过高,会导致计算缓慢。如果你需要如此沉重的模糊,你可能会考虑在某个级别缩小原始位图,并将其模糊,然后向上扩展到画布,这将比巨大图像的重度模糊快得多。

还有一件事,如果你不关心保留原始位图,输入和输出的分配可以是相同的,这可能会节省一些内存。

答案 1 :(得分:0)

该错误无害且实际上不会影响正在运行的代码。模糊不起作用吗?如果它不起作用,你可以共享logcat的其余部分(以及你正在运行的设备/版本)吗?

答案 2 :(得分:0)

虽然没有一种增加模糊半径的好方法,但对于非常大的图像,您可以通过缩小(例如.5原始大小),模糊,然后缩放到原始大小来伪造它。注意不要缩小太远,因为生成的位图将变为像素化。