Android:快速位图模糊? [英] Android: fast bitmap blur?

查看:28
本文介绍了Android:快速位图模糊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去三天我一直在寻找一种内置的、硬件加速的方式来使用 android 模糊位图.我偶然发现了某些变通方法,例如缩小位图并再次放大,但这种方法产生的结果质量低,不适合我的图像识别要求.我还读到使用着色器或 JNI 实现卷积是一个很好的方法,但我无法相信 Android 框架中没有针对这一非常常见目的的内置解决方案.目前我已经用 Java 完成了一个自写的卷积实现,但它非常慢.我的问题是:

I've been searching the past three days for a built-in, hardware-accelerated way of bluring a bitmap with android. I stumbled upon certain work-arounds like shrinking the bitmap and scaling it up again, but this method produced low quality results which were not suitable for my image recognition requirements. I also read that implementing convolution with shaders or JNI is a good way to go, but I cannot believe that there is no built-in solution in the Android framework for this very common purpose. Currently I've ended up with a self-written convolution implementation in Java, but it is awkwardly slow. My question is:

  • Android 框架中真的没有内置解决方案吗?
  • 如果没有:在实现和维护的复杂性仍然合理的情况下,加速卷积的最有效方法是什么?我们应该使用 JNI、着色器还是完全不同的东西?

推荐答案

我终于找到了合适的解决方案:

I finally found a suitable solution:

  • RenderScript 允许实现对所有内核透明缩放的繁重计算在执行设备上可用.我得出的结论是,就性能和实现复杂性的合理平衡而言,这是比 JNI 或着色器更好的方法.
  • 从 API 级别 17 开始,有 ScriptIntrinsicBlur 类可从API.这正是我一直在寻找的,即高级的、硬件加速的高斯模糊实现.
  • ScriptIntrinsicBlur 现在是 android 的一部分支持 Froyo 及更高版本 (API>8) 的支持库 (v8).支持RenderScript库 有一些关于如何使用它的基本技巧.
  • RenderScript allows implementing heavy computations which are scaled transparently to all cores available on the executing device. I've come to the conclusion, that with respect to a reasonable balance of performance and implementation complexity, this is a better approach than JNI or shaders.
  • Since API Level 17, there is the ScriptIntrinsicBlur class available from the API. This is exactly what I've been looking for, namely a high level, hardware-accelerated Gaussian blur implementation.
  • ScriptIntrinsicBlur is now a part of the android support library (v8) which supports Froyo and above (API>8). The android developer blog post on the support RenderScript library has some basic tips on how to use it.

然而,关于 ScriptIntrinsicBlur 类的文档非常罕见,我花了更多时间来找出正确的调用参数.为了模糊名为 photo 的普通 ARGB_8888 类型位图,它们是:

However, the documentation on the ScriptIntrinsicBlur class is very rare and I've spent some more time on figuring out the correct invocation arguments. For bluring an ordinary ARGB_8888-typed bitmap named photo, here they are:

final RenderScript rs = RenderScript.create( myAndroidContext );
final Allocation input = Allocation.createFromBitmap( rs, photo, 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( myBlurRadius /* e.g. 3.f */ );
script.setInput( input );
script.forEach( output );
output.copyTo( photo );

这篇关于Android:快速位图模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆