OpenCV4Android中NDK开发(一)--- OpenCV4.1.0环境搭建

2019-07-25 16:58:54 浏览数 (1)

前言

以前的文章中《OpenCV简介与Android SDK环境》列出过OpenCV4Android的开发,不过当时是用的Java实现,做了一些Demo后慢慢发现,在图像处理中用JAVA调OpenCV的开发处理速度是个瓶颈,所以才激起了学习NDK开发的想法,具体NDK开发的配置可以看我前面的文章《Android NDK编程(一)---NDK介绍及环境搭建》,本章主要说后面的OpenCV在Android NDK开发的环境搭建。

准备工作

开发环境

  • Android Studio 3.3.2
  • NDK 19.2.5345600
  • OpenCV for Android 4.1.0

NDK的搭建

详见《Android NDK编程(一)---NDK介绍及环境搭建》


OpenCV下载及准备

下载地址:https://opencv.org/releases/

找到最新版本4.1.0的下载地址,点击Android进入下载

下载完后就是我们下图红框中的ZIP文件,然后我们解压一下后即生成了OpenCV-android-sdk的文件夹(下图蓝框)


OpenCV4Android项目配置

1.创建项目

我们打开Android Studio新建一个项目,选择Native C

将程序名称改为OpenCVDemo

C Stand选择为C 11

点击Finish后我们完成了项目的创建

2.修改build.gradle文件

双击build.gradle文件

修改要我们要支持的CPU架构下

调用OpenCV4Android中的so动态库用于打包进APK,下图中红框内的路径就是我们上面下载的OpenCV4.1.0中的动态库路径

我们看一下那个路径

完整的build.gradle代码

代码语言:javascript复制
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "dem.vac.opencvdemo"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c  11"
                //我们的APP要支持的CPU架构
                abiFilters'x86','armeabi-v7a'
            }
        }
    }
    //加上
    sourceSets{
        main{
            //当前这个目录下的库文件会被调用并且被打包进apk中
            jniLibs.srcDirs = ['D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs']
        }
    }
    buildTypes {
   
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

3.修改CMakeLists.txt文件

下图中标红框的地方就是我在原来的CMakeLists.txt中修改的地方

完整的CMakeList.txt代码

代码语言:javascript复制
# 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)

#该变量为真时会创建完整版本的Makefile
set(CMAKE_VERBOSE_MAKEFILE on)

#定义变量ocvlibs使后面的命令可以使用定位具体的库文件
set(opencvlibs "D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs")

#调用头文件的具体路径
include_directories(D:/PersonalStudio/OpenCV-android-sdk/sdk/native/jni/include)

#增加我们的动态库
add_library(libopencv_java4 SHARED IMPORTED)

#建立链接
set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
        "${opencvlibs}/${ANDROID_ABI}/libopencv_java4.so")


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

# 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 libopencv_java4

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

划重点

修改完后我们需要点击选项上的Build-Refresh Linked C Projects

完成后我们展开native-lib下面的includes后里面有个opencv2已经添加进来了,如下图:


上面我们的Android Studio通过NDK配置OpenCV已经完下了,下面开始我就可以进行代码的编写了。

-END-

0 人点赞