简体   繁体   English

Android模糊与RenderScript

[英]Android Blur with RenderScript

I try to make a gaussian blur on an android Bitmap but I get this error: 我尝试在Android Bitmap上进行高斯模糊,但是我收到此错误:

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

Here is my code : 这是我的代码:

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

Note that I used android.support.v8.renderscript to ensure compatibility with android lower versions. 请注意,我使用android.support.v8.renderscript来确保与Android较低版本的兼容性。

Someone would have an idea to fix it? 有人会有想法解决它吗?

Thanks. 谢谢。

Martin 马丁

The Element arguments of ScriptIntrinsicBlur should be the same of Allocation's element, so you should use Allocation.getElement() , rather than direct Element.U8_4(rs) . ScriptIntrinsicBlurElement参数应该与Allocation的元素相同,因此您应该使用Allocation.getElement() ,而不是直接使用Element.U8_4(rs)

final ScriptIntrinsicBlur script =
    ScriptIntrinsicBlur.create(rs, input.getElement());

And you might also want move those final to class private member , since some of them might be different every time your bitmap is different. 而且您可能还希望将这些final class private member移动到class private member ,因为每当您的位图不同时,其中一些可能会有所不同。

And FYI, script.setRadius(25f) is way too high, that will cause slow calculation. 而且,FYI, script.setRadius(25f)太高了,这将导致计算缓慢。 If you need such heavy blur, you might consider to scale down the original bitmap at certain level, and blur it, then scale up to your canvas, which will be much faster than heavy blur for a huge image. 如果你需要如此沉重的模糊,你可能会考虑在某个级别缩小原始位图,并将其模糊,然后向上扩展到画布,这将比巨大图像的重度模糊快得多。

One more thing, if you don't care to keep the original bitmap, the allocation of input and output can be the same one, which might save some memory. 还有一件事,如果你不关心保留原始位图,输入和输出的分配可以是相同的,这可能会节省一些内存。

While there isn't a good way to increase blur radius, for very large images you can essentially fake it by scaling down (ex .5 original size), blurring, then scaling up to original size. 虽然没有一种增加模糊半径的好方法,但对于非常大的图像,您可以通过缩小(例如.5原始大小),模糊,然后缩放到原始大小来伪造它。 Be careful not to scale too far down as the resulting bitmap will become pixelated. 注意不要缩小太远,因为生成的位图将变为像素化。

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

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