参考博客:(130条消息) windows10 vs 版本:cmake将 opencv_contrib-4.5.5 扩展模块编译添加到 opencv-4.5.5 正式版中_Chhjnavy的博客-CSDN博客
环境准备:window10+android studio4.1.1+opencv-4.5.5+opencv_contrib-4.5.5+cmake+MinGW
opencv 主页:/opencv
opencv-4.5.5源码下载:/opencv/opencv/tree/4.5.5
opencv_contrib源码下载:/opencv/opencv_contrib/tree/4.5.5
cmake下载:/download/
MinGW 下载:/projects/mingw-w64/files/
android studio下载:https://developer./studio
下载的文件目录如下:
一.准备文件
1. 双击 cmake-3.24.0-rc2-windows-x86_64 安装cmake,并将安装路径:C:\Program Files\CMake\bin 添加到系统变量中
2. 双击 x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.7z 安装MinGW,并将安装路径:D:\Program Files\mingw64\bin 添加到系统变量中
3. 解压 opencv_contrib-4.5.5.zip 和opencv-4.5.5.zip 到当前文件夹,并创建一个新的文件夹android-build 用来存放两者编译合并后的android版文件
二. 编译opencv和opencv_contrib
1. 打开cmake ,填写opencv-4.5.5 路径以及opencv-build 路径
2. 点击Add Entry
,添加以下几个属性
#编译后的 opencv 应用在 android 系统cpu 所对应的架构#armeabiv-v7a:第7代及以上的 ARM 处理器。15月以后的生产的大部分Android设备都使用它.#arm64-v8a:第8代、64位ARM处理器,很少设备,三星 Galaxy S6是其中之一。#armeabi:第5代、第6代的ARM处理器,早期的手机用的比较多。#x86:平板、模拟器用得比较多。#x86_64:64位的平板ANDROID_ABI:arm64-v8a#当前windows 安装android studio sdk 路径ANDROID_SDK:D:/Sdk#当前windows 安装android studio ndk 路径ANDROID_NDK:D:/Sdk/ndk/21.1.6352462#默认API为21ANDROID_NATIVE_API_LEVEL STRING:21#填入ANT路径下的bin(用于java 封装,便于 AS 导入module)ANT_EXECUTABLE:E:/opencv/apache-ant-1.10.12/bin#根据需求写入c++_static或c++_shared(默认是gnustl_static)ANDROID_STL:c++_static
3.点击Configure 按钮,弹出对话框,设置MinGW Makefiles编译,选择Specify toolchain file for cross-compiling
点击next ,选择ndk 下的toolchain 编译:
D:/Sdk/ndk/21.1.6352462/build/cmake/android.toolchain.cmake
点击finish 开始编译(编译的时候尽量连接稳定的网络)。编译完如果显示红色,再点击一次的Configure 按钮,看到 Java wrappers:YES 说明编译成功.
4.添加opencv-contrib模块,根据需要调整参数
#添加opencv_contrib-4.5.5扩展模块OPENCV_EXTRA_MODULES_PATH:E:/opencv/opencv_contrib-4.5.5/modulesBUILD_ANDROID_PROJECTS=ONBUILD_ANDROID_EXAMPLES=ONBUILD_PERF_TESTS=OFFBUILD_TESTS=OFFBUILD_opencv_world=OFFBUILD_SHARED_LIBS=OFF#添加移动端的并行架构支持WITH_OPENCL=ON#建议开启共享虚拟内存WITH_OPENCL_SVM=ON#不然编译会出错OPENCV_DISABLE_FILESYSTEM_SUPPORT=ON
******* 最终编译出现如下错误:
E/cv::error(): OpenCV(4.5.5) Error: Requested object was not found (could not open directory: /data/app/com.terawins.opencvaruco-bB-0D-WPbkdy80TatPWA1g==/base.apk!/lib/armeabi-v7a) in glob_rec, file E:\Lightning2\cameraOpencv\opencv-4.5.5\modules\core\src\glob.cpp, line 279
解决方案:OPENCV_DISABLE_FILESYSTEM_SUPPORT=ON
调整完后再次点击Configure,如果显示红色,再点击一次的Configure 按钮,完成后点击Generate
三.MinGW 编译
1. 在路径:E:\opencv\android-build\opencv_android 下添加文件 local.properties
## This file must *NOT* be checked into Version Control Systems,# as it contains information specific to your local configuration.## Location of the SDK. This is only used by Gradle.# For customization when using a Version Control System, please read the# header note.#Tue Dec 07 09:53:57 CST ndk.dir=D\:\\Sdk\\android-ndk-r16bsdk.dir=D\:\\Sdk
2. 右击android-build -->Git Bash Here 或者在cmd 中打开路径:E:\opencv\android-build
输入命令:mingw32-make.exe
编译成功如下:
3. 再输入命令:mingw32-make.exe install
路径:E:\opencv\android-build\install 生成可以在android 系统中使用的opencv
4. 与官方给出的android opencv 对比
官方android opencv地址:/releases/
官方编译如下:
自己编译出来的含有opencv-contrib 扩展模块的android版 opencv:
四. android studio 三种加载opencv 的方式
1. 打开android studio File->Project Structure->Modules 点击+号添加opencv-android-sdk
并且在调用opencv 的项目的build.gradle中添加依赖,就可以在java 使用opencv
implementation project(path: ':opencv-android-sdk')
2.将opencv-android-sdk 打包成jar 包,并且在调用opencv 的项目的build.gradle中添加依赖,就可以在java 使用opencv
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
3. 添加opencv-android-sdk 中 libopencv_java4.so库到项目中,可以在jni 层直接添加opencv 头文件使用opencv库
cmake_minimum_required(VERSION 3.4.1)#1.配置第三方so库.h头文件路径include_directories(${CMAKE_SOURCE_DIR}/include)# 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.xxx# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).xxx.cpp)#2.添加第三方库add_library(opencv_java4 SHARED IMPORTED)#3.添加库的路径set_target_properties(opencv_java4PROPERTIES IMPORTED_LOCATION${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libopencv_java4.so)# 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.xxxopencv_java4jnigraphics# Links the target library to the log library# included in the NDK.${log-lib})
编译好的opencv-android-sdk(含有opencv-contrib扩展模块你)文件下载路径:
/download/Chhjnavy/85946437
/download/Chhjnavy/85946359