2014-03-03 47 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'不是一回事(迷惑,對嗎?) –

相關問題