RenderScript无法正确渲染ScriptIntrinsicBlur,导致ScriptIntrinsicBlur呈现彩虹色

时间:2015-07-28 00:57:20

标签: android bitmap imageview renderscript

使用滑动android库我将图像作为位图(see glide documentation),然后我尝试使用renderscript和ScriptIntrinsicBlur来模糊位图,这是一种高斯模糊。 (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); 
        }
    });

问题在于这是输出,而不是模糊的图像: enter image description here

非常感谢任何帮助。 :)

2 个答案:

答案 0 :(得分:6)

因为它只支持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;

答案 1 :(得分:3)

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