RenderScript 未正确渲染 ScriptIntrinsicBlur,导致 ScriptIntrinsicBlur 渲染彩虹色

RenderScript not rendering ScriptIntrinsicBlur correctly, causing the ScriptIntrinsicBlur to render a rainbow of colors

使用 glide android 库,我将图像作为位图 (see glide documentation) and then I try to blur the bitmap, using renderscript and ScriptIntrinsicBlur, which is a gaussian blur. (Taken from this Whosebug post)

 Glide.with(getApplicationContext())
    .load(ImageUrl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(300,200) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

            RenderScript rs = RenderScript.create(mContext); // context = this. this referring to the activity

            final Allocation input = Allocation.createFromBitmap( rs, resource, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT );
            final Allocation output = Allocation.createTyped( rs, input.getType() );
            final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create( rs, Element.U8_4( rs ) );
            script.setRadius(8f);
            script.setInput(input);
            script.forEach(output);
            output.copyTo(resource);

            mImageView.setImageBitmap(resource); 
        }
    });

问题是这是输出,而不是模糊图像:

非常感谢任何帮助。 :)

是否有可能输入图像不是 U8_4(即 RGBA8888)?您可以从使用 "Element.U8_4(rs)" 切换为使用 "output.getElement()" 吗?那可能会做正确的事。如果事实证明图像不是 RGBA8888,您可能至少会得到一个 Java 异常来描述底层格式是什么(如果我们的 Blur 不支持它)。

因为它只支持 U8_4 和 U8 格式。在通过此示例将位图发送到 RenderScript 之前,您必须将位图转换为 ARGB_8888。

        Bitmap U8_4Bitmap;
        if(sentBitmap.getConfig() == Bitmap.Config.ARGB_8888) {
            U8_4Bitmap = sentBitmap;
        } else {
            U8_4Bitmap = sentBitmap.copy(Bitmap.Config.ARGB_8888, true);
        }

        //==============================

        Bitmap bitmap = Bitmap.createBitmap(U8_4Bitmap.getWidth(), U8_4Bitmap.getHeight(), U8_4Bitmap.getConfig());

        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs,
                U8_4Bitmap,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);

        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, output.getElement());
        script.setRadius(radius);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);
        rs.destroy();
        return bitmap;