Search code examples
androidbitmapimageviewrenderscript

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


Using the glide android library I get the image as a bitmap (see glide documentation) and then I try to blur the bitmap, using renderscript and ScriptIntrinsicBlur, which is a gaussian blur. (Taken from this stackoverflow 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); 
        }
    });

The problem is that this is the output, rather than a blurred image: enter image description here

Any help would be much appreciated thanks. :)


Solution

  • Is it possible that the input image is not a U8_4 (i.e. RGBA8888)? Can you switch from using "Element.U8_4(rs)" to instead use "output.getElement()"? That would probably do the right thing. If it turns out that the image is not RGBA8888, you might at least get a Java exception describing what the underlying format is (if it isn't something supported with our Blur).