如何在Android中创建模糊位图图像

时间:2015-10-06 11:48:51

标签: android bitmapimage

我尝试创建一个蓝色位图图像,但它会生成一些错误的渲染脚本

private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

    try {           smallBitmap = RGB565toARGB888(smallBitmap);         } catch (Exception e) {             e.printStackTrace();        }

    Bitmap bitmap = Bitmap.createBitmap(smallBitmap.getWidth(),
            smallBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    RenderScript renderScript = RenderScript.create(MainActivity.this);

    Allocation blurInput = Allocation.createFromBitmap(renderScript,
            smallBitmap);       Allocation blurOutput = Allocation.createFromBitmap(renderScript,
            bitmap);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
            Element.U8_4(renderScript));        blur.setInput(blurInput);       blur.setRadius(radius); // radius must be 0 < r <= 25       blur.forEach(blurOutput);

    blurOutput.copyTo(bitmap);      renderScript.destroy();

    return bitmap;

}

,第二部分是

private Bitmap RGB565toARGB888(Bitmap img) throws Exception {       int numPixels = img.getWidth() * img.getHeight();       int[] pixels = new int[numPixels];

    // Get JPEG pixels. Each int is the color values for one pixel.         img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(),
            img.getHeight());

    // Create a Bitmap of the appropriate format.       Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(),
            Bitmap.Config.ARGB_8888);

    // Set RGB pixels.      result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(),
            result.getHeight());        return result;  }

当我调用方法blurRenderScript(bitmap, 20)时,它会生成类似10-06 11:40:33.547: E/RenderScript(13094): rsi_ScriptIntrinsicCreate 5 10-06 11:40:33.571: A/libc(13094): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=128), thread 13107

的错误

1 个答案:

答案 0 :(得分:0)

您可以使用一些第三方工具 使用Renderscript模糊图像 - Xamarin

要求

enter code here

此配方仅适用于Android 4.2.2(API级别17)或更高版本。

配方

此配方的UI包含在以下XML布局文件中:

这是一个非常简单的UI,它有一个ImageView和一个SeekBar。我们对此布局进行了扩充,并在事件的OnCreate方法中将事件处理程序连接到SeekBar,如以下代码所示:

当用户停止沿着SeekBar滑动手指时,将触发StopTrackingTouch事件并调用BlurImageHandler方法。此处理程序的内容可能会在以下代码段中看到:

如果SeekBar的值大于零,那么我们需要应用模糊滤镜。如果SeekBar的值为零,则不需要应用模糊。 DisplayBlurredImage中的代码如下所示:

让我们快速了解一下DisplayBlurredImage。我们要做的第一件事是禁用SeekBar并显示一个不确定的进度对话框。为了保持我们的应用程序响应并防止来自Android的ANR(应用程序无响应)消息,实际过滤将在单独的线程中执行。此示例将通过任务并行库调用方法CreateBlurredImage。我们为完成此工作而创建的任务有一个延续,它将使用模糊图像更新UI并关闭进度对话框。此延续计划在我们的应用程序的UI线程中运行。

我们需要检查的最后一段代码将执行图像的实际模糊。其代码包含在CreateBlurredImage方法中。此代码具有内联注释,以帮助解释代码的相关部分:

http://developer.xamarin.com/recipes/android/other_ux/drawing/blur_an_image_with_renderscript/