6

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. :)

PhilLab
  • 4,777
  • 1
  • 25
  • 77
HaloMediaz
  • 1,251
  • 2
  • 13
  • 31

2 Answers2

8

As it's support only U8_4 and U8 format. You'll have to convert your bitmap to ARGB_8888 before you send it to RenderScript by this example.

        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;
zinuzoid
  • 337
  • 8
  • 14
4

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).

Stephen Hines
  • 2,612
  • 1
  • 13
  • 12
  • This is the error I get: 'android.support.v8.renderscript.RSIllegalArgumentException: Unsuported element type.' Thanks for trying to help :) – HaloMediaz Jul 29 '15 at 00:22
  • Ok. I figured out the problem. Glide loads bitmaps in RGB_565. Glides has an option to use ARGB_8888, but this still gives me the same problem. Thanks for the help. – HaloMediaz Jul 29 '15 at 00:59
  • ScriptIntrinsicBlur doesn't support non-RGBA_8888 bitmap configs. But @zinuzoid had a great solution that works for me: sentBitmap.copy(Bitmap.Config.ARGB_8888, true); – goRGon Jan 14 '16 at 00:35