安卓:快位图模糊?位图、模糊

2023-09-12 22:07:28 作者:于心忍

我一直在寻找过去三天bluring与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的支持库(V8),支持升级Froyo及以上(API> 8)的一部分。在Android开发博客文章support 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文档类是非常罕见的,我花了一些时间上找出正确的调用参数。对于bluring一个普通的 ARGB_8888 -typed位名为照片,在这里,他们分别是:

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 );