首页 文章详情

Android NDK 实现图像高斯模糊

字节流动 | 346 2021-10-16 06:09 0 0 0
UniSMS (合一短信)

JNI 层操作 Bitmap 对象


Android中JNI层处理Bitmap通常有两种方法


  • 获取到 Bitmap 中的byte数组并传入native方法,JNI层处理得到的byte数组后返回一个新的byte数组,Java层重建Bitmap对象。(不推荐)

  • Java层直接向JNI层传入Bitmap的引用,JNI层得到Bitmap对象的图像数据的地址,直接修改Bitmap的byte数组。


阅读了很多篇博客,很多开发者都会采用第一种方法,本人是极不推荐的。这种方法会在内存中重建一个byte数组,会造成内存的浪费,性能低下。


第二种方法是性能最优的,JNI层充分利用的C/C++指针的特性,直接获取到 Bitmap 中byte数组在内存中的地址,通过指针直接修改图像数据,所以用到了NDK中的android/bitmap.h。


android/bitmap.h


android/bitmap.h这个头文件用于在JNI层操作Bitmap对象的,其包含于 jnigraphics 库中,所以要在 CMakeLists.txt 中的 target_link_libraries 加入 -ljnigraphics,如下

target_link_libraries(native-lib -ljnigraphics ${log-lib})

三个常用函数


  • AndroidBitmap_getInfo()  从位图句柄获得信息(宽度、高度、像素格式)

  • AndroidBitmap_lockPixels() 对像素缓存上锁,即获得该缓存的指针。

  • AndroidBitmap_unlockPixels() 解锁

JNI接口函数


请看注释


JNIEXPORT void JNICALL
Java_com_glee_ndkroad1006_MainActivity_gaussBlur(JNIEnv *env, jobject /* this */, jobject bmp) {
    AndroidBitmapInfo info = {0};//初始化BitmapInfo结构体
    int *data=NULL;//初始化Bitmap图像数据指针
    AndroidBitmap_getInfo(env, bmp, &info);
    AndroidBitmap_lockPixels(env, bmp, (void **) &data);//锁定Bitmap,并且获得指针
    /**********高斯模糊算法作对int数组进行处理***********/
    //调用gaussBlur函数,把图像数据指针、图片长宽和模糊半径传入
    gaussBlur(data,info.width,info.height,80);
    /****************************************************/
    AndroidBitmap_unlockPixels(env,bmp);//解锁
}


Java层代码

public class MainActivity extends AppCompatActivity {

    static {
        //通过静态代码块加载so库
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化两个ImageView
        ImageView iv1 = (ImageView) findViewById(R.id.img1);
        ImageView iv2 = (ImageView) findViewById(R.id.img2);
        //iv1设置图片
        iv1.setImageResource(R.drawable.test);
        //生成bitmap对象
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        //调用native方法,传入Bitmap对象,对Bitmap进行高斯迷糊处理
        gaussBlur(bitmap);
        //把Bitmap对象设置给iv2
        iv2.setImageBitmap(bitmap);
    }
    //native方法声明
    public native void gaussBlur(Bitmap bitmap);
}


上方的 ImageView 是没有进行高斯模糊处理的,下方的 ImageView 调用了JNI方法进行高斯模糊处理。




高斯模糊算法

void gaussBlur1(int* pix, int w, int h, int radius)
{
    float sigma = (float) (1.0 * radius / 2.57);
    float deno  = (float) (1.0 / (sigma * sqrt(2.0 * PI)));
    float nume  = (float) (-1.0 / (2.0 * sigma * sigma));
    float* gaussMatrix = (float*)malloc(sizeof(float)* (radius + radius + 1));
    float gaussSum = 0.0;
    for (int i = 0, x = -radius; x <= radius; ++x, ++i)
    {
        float g = (float) (deno * exp(1.0 * nume * x * x));
        gaussMatrix[i] = g;
        gaussSum += g;
    }
    int len = radius + radius + 1;
    for (int i = 0; i < len; ++i)
        gaussMatrix[i] /= gaussSum;
    int* rowData  = (int*)malloc(w * sizeof(int));
    int* listData = (int*)malloc(h * sizeof(int));
    for (int y = 0; y < h; ++y)
    {
        memcpy(rowData, pix + y * w, sizeof(int) * w);
        for (int x = 0; x < w; ++x)
        {
            float r = 0, g = 0, b = 0;
            gaussSum = 0;
            for (int i = -radius; i <= radius; ++i)
            {
                int k = x + i;
                if (0 <= k && k <= w)
                {
                    //得到像素点的rgb值
                    int color = rowData[k];
                    int cr = (color & 0x00ff0000) >> 16;
                    int cg = (color & 0x0000ff00) >> 8;
                    int cb = (color & 0x000000ff);
                    r += cr * gaussMatrix[i + radius];
                    g += cg * gaussMatrix[i + radius];
                    b += cb * gaussMatrix[i + radius];
                    gaussSum += gaussMatrix[i + radius];
                }
            }
            int cr = (int)(r / gaussSum);
            int cg = (int)(g / gaussSum);
            int cb = (int)(b / gaussSum);
            pix[y * w + x] = cr << 16 | cg << 8 | cb | 0xff000000;
        }
    }
    for (int x = 0; x < w; ++x)
    {
        for (int y = 0; y < h; ++y)
            listData[y] = pix[y * w + x];
        for (int y = 0; y < h; ++y)
        {
            float r = 0, g = 0, b = 0;
            gaussSum = 0;
            for (int j = -radius; j <= radius; ++j)
            {
                int k = y + j;
                if (0 <= k && k <= h)
                {
                    int color = listData[k];
                    int cr = (color & 0x00ff0000) >> 16;
                    int cg = (color & 0x0000ff00) >> 8;
                    int cb = (color & 0x000000ff);
                    r += cr * gaussMatrix[j + radius];
                    g += cg * gaussMatrix[j + radius];
                    b += cb * gaussMatrix[j + radius];
                    gaussSum += gaussMatrix[j + radius];
                }
            }
            int cr = (int)(r / gaussSum);
            int cg = (int)(g / gaussSum);
            int cb = (int)(b / gaussSum);
            pix[y * w + x] = cr << 16 | cg << 8 | cb | 0xff000000;
        }
    }
    free(gaussMatrix);
    free(rowData);
    free(listData);
}

作者:李季_
链接:
https://juejin.cn/post/6844903502112784397


-- END --


进技术交流群,扫码添加我的微信:Byte-Flow



获取视频教程和源码



推荐:

Android NDK 开发中快速定位 Crash 问题

Android NDK 开发:JNI 基础篇

Android NDK 开发:Java 与 Native 相互调用

全网最全的 Android 音视频和 OpenGL ES 干货,都在这了

Android NDK 开发基础:C 语言的内存管理

NDK 开发中 Native 方法的静态注册与动态注册

Android Native (C++) 开发中如何使用 ASan 检测内存错误

抖音传送带特效是怎么实现的?

所有你想要的图片转场效果,都在这了


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter