2014-03-03 46 views
0

我想在我的应用程序中使用RenderScript框架模糊位图。我正在使用下面的代码:ScriptIntrinsicBlur产生黑色位图

public static Bitmap apply(Context context, Bitmap sentBitmap, int radius) 
{ 
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); 

    final RenderScript rs = RenderScript.create(context); 
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, 
                 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(radius); 
    script.setInput(input); 
    script.forEach(output); 
    output.copyTo(bitmap); 
    return bitmap; 
} 

不幸的是,我所得到的代码是黑色的位图。我该如何解决这个问题?

位图传递到下面的方式创建的apply方法:

Bitmap b = Bitmap.createBitmap(thisView.getWidth(), 
           thisView.getHeight(), 
           Bitmap.Config.ARGB_8888); 

这些位图的宽度和高度的4

倍数也有通过的renderScript但我报道了一些错误不知道它们是什么意思,我应该如何解决它们(ScriptIntrinsicBlur的文档很薄)。下面是这些错误:

20305-20391/com.xxx E/RenderScript﹕ rsi_ScriptIntrinsicCreate 5 
20305-20391/com.xxx E/RenderScript﹕ rsAssert failed: mUserRefCount > 0, in 
frameworks/rs/rsObjectBase.cpp at 112 

编辑:

半径是5和我运行在三星Galaxy Nexus的应用与Android 4.2.1。

+0

运行中设置的“半径”是多少?什么版本的Android?查看rsObjectBase.cpp代码,日志断言不在4.3.1或4.4.2源文件中。 –

+0

@LarrySchiefer我已经更新了我的问题。你能看一下吗? – Bobrovsky

+0

您使用支持库吗?你应该使用支持库。 –

回答

0

感谢@Tim穆雷,我解决了该问题(有两个实际上)

我转向使用支持库和现我希望带有gradle项目的Android Studio最终将学习如何处理库符号。

问题的另一个主要来源是我使用完全透明的位图作为ScriptIntrinsicBlur的输入。我的错。从三月-07-2013

的Android 0.5工作室在摇篮供电项目支持的renderScript修复问题

编辑。

0

使用此功能来模糊你的输入位图图像:

Bitmap BlurImage(Bitmap input) { 
      RenderScript rsScript = RenderScript.create(this); 
      Allocation alloc = Allocation.createFromBitmap(rsScript, input); 

      ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript, alloc.getElement()); 
      blur.setRadius(12); 
      blur.setInput(alloc); 

      Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), input.getConfig()); 
      Allocation outAlloc = Allocation.createFromBitmap(rsScript, result); 
      blur.forEach(outAlloc); 
      outAlloc.copyTo(result); 

      rsScript.destroy(); 
      return result; 
     } 
+0

我试过上面的代码,不幸的是它会抛出'android.renderscript.RSIllegalArgumentException:Unsuported元素类型'。 – Bobrovsky

+0

奇怪becouse我测试这个功能,对我来说确定,请确保你通过正确的位图,并尝试在再次运行之前清理你的项目 –

+0

这是因为'ScriptIntrinsicBlur'只允许'U8_4'的元素。我怀疑你的原始模糊代码发生了什么,是'createFromBitmap'的结果是给你''Allocation'与'Element.RGBA_8888'不是一回事(迷惑,对吗?) –

相关问题