前言
前面几章我们已经把SurfaceView加载Camera实现实时帧显示图像完成了,我也说过,我们加载实时图像是为了对接OpenCV进行图像处理所以才生成的Bitmap图像。
《OpenCV4Android中NDK开发(一)--- OpenCV4.1.0环境搭建》这篇中我们是新建的项目中直接选择了包含C++,本篇主要是介绍怎么在现在的项目加改为使用JNI的方式。
实现方式
添加CPP的相关文件夹和文件
首先在我们项目的目录app/src/main下建立一个cpp的文件夹
进入cpp目录下我们把别的项目中的CMakeList.txt文件拷贝过来
如果没有配置的可以自己新建一个CMakeList.txt,下面是CMakeList.txt的配置,这个配置是直接关联我们的OpenCV库的,取自《OpenCV4Android中NDK开发(一)--- OpenCV4.1.0环境搭建》
# 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
-ljnigraphics
libopencv_java4
# Links the target library to the log library
# included in the NDK.
${log-lib})
然后我们在cpp文件夹下再建一个native-lib.cpp的文件,这个文件名主要是根据CMakeList.txt里面配置相同的,我们也可以改别的名,不过CMakeList.txt也要跟着修改。
修改build.gradle
打开build.gradle文件
在android下的defaultConfig下加入Cmake的配置
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
//我们的APP要支持的CPU架构
abiFilters 'x86','x86_64','arm64-v8a','armeabi-v7a'
}
}
在android下面加入OpenCV的Lib目录
//加上
sourceSets{
main{
//当前这个目录下的库文件会被调用并且被打包进apk中
jniLibs.srcDirs = ['D:/PersonalStudio/OpenCV-android-sdk/sdk/native/libs']
}
}
然后在android下面加入CMakiLists的指定目录
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
这样的配置完成了,然后左边红框是我们没按右边Sync Now的目录
现在我们点击 一下右上角红框的Sync Now
生成完后可以看到左边红框里面已经出来cpp目录和下面的Cmakelist及native-lib.cpp的文件了,这就说明我们在现有项目中添加JNI成功了。
-END-
Vaccae的往期经典
OpenCV
Android
《Android利用SurfaceView结合科大讯飞修改语音实别UI》
《Android关于语音识别的功能实现分析(一)---结构化思维》
《Android关于语音识别的功能实现分析(二)---语义解析》
《Android中RecyclerView嵌套RecyclerView》
.Net C#
数据库及其它
《SQL Server中With As的介绍与应用(三)--递归的实战应用》
长按下方二维码关注微卡智享