Android NDK开发:操作Bitmap实现图像二值化

目录

目录

前言

在Android开发中有时需要我们对Bitmap进行相应的操作,虽然利用Java代码也能实现,但是如果我们通过Native层代码对Bitmap进行操作的话效率会更高。

Java层与Native层效果对比

  • Java层效果


    Java层
  • Native层效果


    Native层

    可以看到Java层代实现的效果是有明显的卡顿的。

Native层实现图像二值化

1.Java层声明Native方法

/**
     * 对图像二值化
     * @param bitmap
     */
    public native void getBinaryBitmap(Object bitmap);

2.生成头文件(参考Android NDK开发小技巧)

/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class com_hehuidai_binarydemo_MainActivity */

#ifndef _Included_com_hehuidai_binarydemo_MainActivity
#define _Included_com_hehuidai_binarydemo_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_hehuidai_binarydemo_MainActivity
 * Method:    getBinaryBitmap
 * Signature: (Ljava/lang/Object;)V
 */
JNIEXPORT void JNICALL Java_com_hehuidai_binarydemo_MainActivity_getBinaryBitmap
  (JNIEnv *, jobject, jobject);

#ifdef __cplusplus
}
#endif
#endif

3.native-lib.c中进行方法实现(这里我是使用的C语言)

#include 
#include 
JNIEXPORT void JNICALL Java_com_hehuidai_binarydemo_MainActivity_getBinaryBitmap
(JNIEnv* env, jobject jobj, jobject jBitmap) {
    int result;
    // 获取源Bitmap相关信息:宽、高等
    AndroidBitmapInfo sourceInfo;
    result = AndroidBitmap_getInfo(env, jBitmap, &sourceInfo);
    if (result < 0) {
        return;
    }
    // 获取源Bitmap像素数据 这里用的是32位的int类型 argb每个8位
    uint32_t* sourceData;
    //锁定像素的地址(不锁定的话地址可能会发生改变)
    result = AndroidBitmap_lockPixels(env, jBitmap, (void**)& sourceData);
    if (result < 0) {
        return;
    }
    // 遍历各个像素点
    int color;
    int red, green, blue , alpha;
    int width = sourceInfo.width;
    int height = sourceInfo.height;
    int w, h;
    for (h = 0; h < height; h++) {
        for (w = 0; w < width; w++) {
            color = sourceData[h * width + w];
            alpha = color & 0xff000000;
            red = (color & 0x00ff0000) >> 16;
            green = (color & 0x0000ff00) >> 8;
            blue = color & 0x000000ff;
            // 通过加权平均算法,计算出最佳像素值
            color = red * 0.3 + green * 0.59 + blue * 0.11;
            if (color <= 95) {
                color = 0;
            }
            else
            {
                color = 255;
            }
            sourceData[h * width + w] = alpha | (color << 16) | (color << 8) | color;
        }
    }
    AndroidBitmap_unlockPixels(env, jBitmap);
}

4.引入jnigraphics库

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             #这里注意要改成.c(如果用C语言写的话)
             src/main/cpp/native-lib.c)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                       #这里对库进行链接
                       jnigraphics)

5.Java层进行调用

...省略部分代码
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_test);
mBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        getBinaryBitmap(bitmap);
                 mImage.setImageBitmap(bitmap);
            }
        });

案例源码

案例源码:https://github.com/myml666/BinaryDemo

你可能感兴趣的:(Android NDK开发:操作Bitmap实现图像二值化)