Android配置OpenCV的JNI接口

Android Studio配置OpenCV的JNI接口

本篇主要记录AS配置OpenCV的JNI接口实现C++代码编程,虽然OpenCV提供的Java接口,可以直接在Android Java调用,但是由于速度的原因和追求高性能,因此采用JNI方式调用OpenCV。

1. 准备

  • Android studio 3.6或其他版本

  • Android SDK、Android NDK、CMake、OpenCV-android-sdk

2. 新建工程

(1)新建工程选择Native C++,然后选择下一步Next

(2)选择C++版本,C++ 11、C++14、C++17按需求选择一个:

(3)项目自动构建完成后,设置SDK,NDK,JDK,一般情况如果已安装AS会自动配置好:

(4)创建完的Native C++自身已经配置好了JNI接口,构建的项目结构如下:

3. 配置OpenCV到项目

(1)依次点击 File->New->Import Module导入OpenCV-android-sdk/sdk/javaModule name会根据导入的OpenCV版本自动生成,点击Next,会出现三个复选框,都选上,点击Finish

(2)依次点击File->Project Structure,在左边的Dependencies栏下选择app,点击右边栏的 “+” 号,选择第三个Module Dependency,里面有对应的OpenCV的文件,点击OModuleK。

(3)将appbuild.gradleopencvbuild.gradle文件进行版本号的修改,使得两个文件中版本号一致(让后者与前者保持一致的版本),删除opencvAndroidManifest.xml中的<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />,同时将opencv下的build.gradle中的defaultConfig部分删掉,点击Build-Make project,等待构建完成。配置这一步的目的不是因为C++代码需要,而是因为java代码中需要这些包,网上很多文章都没有这一步的配置,仅仅只配置jni接口的话,我们在activity中就无法调用opencv的函数。

(4)配置OpenCV的JNI接口,第一步将OpenCV-android-sdk/sdk/native/jni/include文件夹拷贝到你的项目中src/main/cpp下面,第二步将OpenCV-android-sdk/sdk/native/libs文件夹拷贝到你的项目中src/main/下面。并修改名字为jniLibs

(5)既然要通过JNI调用C++,现在对CMakeList.txt文件进行修改,如下代码:

# 设置include文件夹的地址
include_directories(${CMAKE_SOURCE_DIR}/include)

# 设置opencv的动态库,注意这里需要把项目路径填写进去,尽量注意不要填写错误,
add_library(libopencv_java3 SHARED IMPORTED)
set_target_properties(libopencv_java3 PROPERTIES IMPORTED_LOCATION F:/Work/Android/writeBlog/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

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).
        native-lib.cpp)


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)


target_link_libraries( # Specifies the target library.
        native-lib
        libopencv_java3 #添加libopencv_java3

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

(6)接下来配置app中的build.gradle文件,打开代码需要添加三段代码:

  • 第一部分代码:

  • 第二部分:

  • 第三部分:

到现在为止OpenCV的JNI的接口配置完成了,整个appbuild.gradle的代码如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }

        ndk{
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }

    sourceSets{
        main{
            jniLibs.srcDirs = ['src/main/jniLibs']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }

    splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' //select ABIs to build APKs for
            universalApk true //generate an additional APK that contains all the ABIs
        }
    }

    project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.versionCodeOverride =
                    project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
        }
    }


}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation project(path: ':openCVLibrary349')
}

(7)现在可shi以开始测试JNI调用OpenCV是否正常,在native-lib.cpp中加入头文件\#include <opencv2/opencv.hpp>, 如果变成绿色同时调用OpenCV函数时自动弹出选项,说明已经配置好了,可以开始Android平台下C++开发OpenCV代码了。

4. 分享一个配置好的工程

https://github.com/xiezhongzhao/ImageEnhancementAndroid.git

posted @ 2020-12-18 09:38  BrianX  阅读(906)  评论(0编辑  收藏  举报