Android:Renderscript错误 - 无法更新位图分配,大小不匹配

时间:2016-05-24 22:54:07

标签: android android-bitmap renderscript android-renderscript android-blur-effect

我尝试从网络上加载照片并对其进行模糊处理,将模糊图像输出为单独的位图。我的代码如下所示:

            URL url = new URL(myUrl);
            mNormalImage = BitmapFactory.decodeStream(url.openStream());

            final RenderScript rs = RenderScript.create( mContext );
            final Allocation input = Allocation.createFromBitmap( rs, mNormalImage, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT );
            final Allocation output = Allocation.createTyped( rs, input.getType() );
            final ScriptIntrinsicBlur script;
            script = ScriptIntrinsicBlur.create( rs, Element.U8_4( rs ) );
            script.setRadius( 3.f );
            script.setInput( input );
            script.forEach( output );
            output.copyTo( mBlurredImage );

我收到了错误:

android.renderscript.RSIllegalArgumentException: 
Cannot update allocation from bitmap, sizes mismatch

为什么会这样?

1 个答案:

答案 0 :(得分:3)

mBlurredImage在哪里创建?发生这种情况是因为该位图的大小与输入不匹配。您应该使用以下内容创建它:

Bitmap mBlurredImage = 
    Bitmap.createBitmap(
        mNormalImage.getWidth(),
        mNormalImage.getHeight(),
        mNormalImage.getConfig());