Adding OpenCV to Native C code through CMake on Android Studio(在 Android Studio 上通过 CMake 将 OpenCV 添加到本机 C 代码)
问题描述
我正在尝试通过 Cmake 在 android studio 项目中将 Opencv 包含到我的本机 C 代码中.我在网上做了一些研究,从网上下载了 FindOpenCV.cmake 文件并将其添加到我的 android 项目的 app 目录中.这也是 CMakeLists.txt 所在的位置.我使用本教程将 OpenCV 作为模块导入到我的 Android Studio 项目中:
您应该在每个 abi 架构文件夹下看到一个 libopencv_java3.so
.
在您的 java 类中初始化 OpenCV SDK:
公共类 MyClass {静止的 {如果(BuildConfig.DEBUG){OpenCVLoader.initDebug();}}...}
并且您应该在 logcat 消息中看到指定 OpenCV 已加载(第一个错误是正常的):
05-10 10:42:31.451 D/OpenCV/StaticHelper: 试图获取库列表05-10 10:42:31.452 E/OpenCV/StaticHelper:OpenCV 错误:无法加载 OpenCV 的信息库05-10 10:42:31.452 D/OpenCV/StaticHelper:库列表:"05-10 10:42:31.452 D/OpenCV/StaticHelper:第一次尝试加载库05-10 10:42:31.452 D/OpenCV/StaticHelper: 试图初始化 OpenCV 库05-10 10:42:31.452 D/OpenCV/StaticHelper: 试图加载库 opencv_java305-10 10:42:32.031 D/OpenCV/StaticHelper: 库 opencv_java3 已加载05-10 10:42:32.031 D/OpenCV/StaticHelper:第一次尝试加载库是可以的05-10 10:42:32.045 I/OpenCV/StaticHelper:OpenCV 3.2.0 的常规配置 =====================================05-10 10:42:32.045 I/OpenCV/StaticHelper:版本控制:3.2.005-10 10:42:32.045 I/OpenCV/StaticHelper:平台:05-10 10:42:32.045 I/OpenCV/StaticHelper:时间戳:2016-12-23T13:04:49Z05-10 10:42:32.045 I/OpenCV/StaticHelper:主机:Linux 4.8.0-25-generic x86_6405-10 10:42:32.045 I/OpenCV/StaticHelper:目标:Linux 1 x86_6405-10 10:42:32.045 I/OpenCV/StaticHelper: CMake: 2.8.12.205-10 10:42:32.045 I/OpenCV/StaticHelper:CMake 生成器:Ninja05-10 10:42:32.045 I/OpenCV/StaticHelper:CMake 构建工具:/usr/bin/ninja05-10 10:42:32.045 I/OpenCV/StaticHelper:配置:发布05-10 10:42:32.045 I/OpenCV/StaticHelper:C/C++:05-10 10:42:32.045 I/OpenCV/StaticHelper:构建为动态库?:否05-10 10:42:32.045 I/OpenCV/StaticHelper:C++ 编译器:/usr/bin/ccache/opt/android/android-ndk-r10e/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin/x86_64-linux-android-g++ (ver 4.9)
I am trying to include Opencv to my native C code in an android studio project through Cmake. I did some research online and downloaded the FindOpenCV.cmake file from online and added it to the app directory of my android project. This is also where the CMakeLists.txt is located. I imported OpenCV onto my Android Studio project as a module using this tutorial: https://www.learn2crack.com/2016/03/setup-opencv-sdk-android-studio.html, and when I run:
if(!OpenCVLoader.initDebug()){
System.out.println("Opencv not loaded");
} else {
System.out.println("Opencv loaded");
}
I get that Opencv is loaded.
However, since I'm trying to add OpenCV to my native code, and not the Java code, I don't think I can use this. Here is the CMakeLists I have right now:
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} FindOpenCV.cmake)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library(# Specifies the name of the library.
apriltag
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/apriltag/apriltag.c
src/main/apriltag/apriltag_jni.c
src/main/apriltag/apriltag_quad_thresh.c
src/main/apriltag/common/g2d.c
src/main/apriltag/common/getopt.c
src/main/apriltag/common/homography.c
src/main/apriltag/common/image_f32.c
src/main/apriltag/common/image_u8.c
src/main/apriltag/common/image_u8x3.c
src/main/apriltag/common/matd.c
src/main/apriltag/common/pnm.c
src/main/apriltag/common/string_util.c
src/main/apriltag/common/svd22.c
src/main/apriltag/common/time_util.c
src/main/apriltag/common/unionfind.c
src/main/apriltag/common/workerpool.c
src/main/apriltag/common/zarray.c
src/main/apriltag/common/zhash.c
src/main/apriltag/common/zmaxheap.c
src/main/apriltag/tag16h5.c
src/main/apriltag/tag25h7.c
src/main/apriltag/tag25h9.c
src/main/apriltag/tag36artoolkit.c
src/main/apriltag/tag36h10.c
src/main/apriltag/tag36h11.c
)
STRING(REPLACE "-O0" "-O4" CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
STRING(REPLACE "-O2" "-O4" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
include_directories(src/main/apriltag/)
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(OpenCV REQUIRED)
find_library(log-lib log)
find_library(jnigraphics-lib jnigraphics)
target_link_libraries(apriltag ${log-lib} ${jnigraphics-lib})
Here are the errors I'm getting while building the gradle:
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any of
the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
So my questions are:
- Can I use the imported OpenCV or do I have to download a different opencv and store it somewhere else?
- What do I have to change in my CMakeLists.txt for my gradle to build?
Ideally, I want to build and be able to add #include <opencv2/opencv.hpp>
and using namespace cv
to my c file and add functions that use opencv functions.
Thanks for any help!
UPDATE 21-Oct-19: Deprecated Git/Simpler Way in favor of new AndroidOpenCVGradlePlugin
UPDATE 22-May-18: Added missing step number 6.
UPDATE 10-May-17: New solution provides proper integration of OpenCV into application with CMake and Android Gradle plugin 2.3.1. Tested using Android Studio 2.3.1.
UPDATE 11-May-17: An additional solution has been provided
There are two ways of including OpenCV.
Using AndroidOpenCVGradlePlugin
Visit https://github.com/ahasbini/AndroidOpenCVGradlePlugin for more details.
Git/Simpler Way
Visit https://github.com/ahasbini/Android-OpenCV for more details.
Manual/Advanced Way
To include OpenCV libraries into Android Studio Project, its best to create a new Library Module in the project and port the files from OpenCV Android SDK bundle into it:
- Create a new module by selecting File>New Module.
- Select "Android Library", and then enter the details:
- Library name:
OpenCV
- Module name:
opencv
- Package name:
org.opencv
- Library name:
- Once the new module created, copy the contents of
path_to_opencv_sdk/sdk/java/src
directory intopath_to_your_project/opencv/src/main/java
. - Under
main
, create the following directly path:aidl/org/opencv/engine
and movemain/java/org/opencv/engine/OpenCVEngineInterface.aidl
into it. - Copy the contents of
path_to_opencv_sdk/sdk/java/res
intopath_to_your_project/opencv/src/main/res
. - Create
sdk
folder insidepath_to_your_project/opencv/src/
and copypath_to_opencv_sdk/sdk/native
folder into it. - Within the
opencv
module, createCMakeLists.txt
file and add the following lines in the following order:
cmake_minimum_required(VERSION 3.4.1)
set(OpenCV_DIR "src/sdk/native/jni")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})
- Within the
opencv
module, edit thebuild.gradle
file as such:
...
android {
...
defaultConfig {
minSdkVersion 8
targetSdkVersion 25
versionCode 3200
versionName "3.2.0"
...
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
buildTypes {
...
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets {
main {
jni.srcDirs = [jni.srcDirs, 'src/sdk/native/jni/include']
jniLibs.srcDirs = [jniLibs.srcDirs, 'src/sdk/native/3rdparty/libs', 'src/sdk/native/libs']
}
}
}
...
- Within the
app
(application module, could be another name) module, create/editCMakeLists.txt
file and add the following lines in the following order (Note the different path set toOpenCV_DIR
):
set(OpenCV_DIR "../opencv/src/sdk/native/jni")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
target_link_libraries(YOUR_TARGET_LIB ${OpenCV_LIBS})
- Within the
app
(application module, could be another name) module, edit thebuild.gradle
file as such:
...
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
buildTypes {
...
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
...
compile project(':opencv')
}
- Do a gradle sync, and now the OpenCV native libs, header files and Java wrapper classes are included.
When project is built and apk is launched, you could inspect the packaged apk under path_to_project/path_to_app_module/build/output/
(drag the apk onto the text editor tabs of Android Studio)
You should see a libopencv_java3.so
under each abi architecture folder.
Initialize the OpenCV SDK in your java class :
public class MyClass {
static {
if (BuildConfig.DEBUG) {
OpenCVLoader.initDebug();
}
}
...
}
And you should see within logcat messages specifying the OpenCV has been loaded (the first error is normal):
05-10 10:42:31.451 D/OpenCV/StaticHelper: Trying to get library list
05-10 10:42:31.452 E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV
05-10 10:42:31.452 D/OpenCV/StaticHelper: Library list: ""
05-10 10:42:31.452 D/OpenCV/StaticHelper: First attempt to load libs
05-10 10:42:31.452 D/OpenCV/StaticHelper: Trying to init OpenCV libs
05-10 10:42:31.452 D/OpenCV/StaticHelper: Trying to load library opencv_java3
05-10 10:42:32.031 D/OpenCV/StaticHelper: Library opencv_java3 loaded
05-10 10:42:32.031 D/OpenCV/StaticHelper: First attempt to load libs is OK
05-10 10:42:32.045 I/OpenCV/StaticHelper: General configuration for OpenCV 3.2.0 =====================================
05-10 10:42:32.045 I/OpenCV/StaticHelper: Version control: 3.2.0
05-10 10:42:32.045 I/OpenCV/StaticHelper: Platform:
05-10 10:42:32.045 I/OpenCV/StaticHelper: Timestamp: 2016-12-23T13:04:49Z
05-10 10:42:32.045 I/OpenCV/StaticHelper: Host: Linux 4.8.0-25-generic x86_64
05-10 10:42:32.045 I/OpenCV/StaticHelper: Target: Linux 1 x86_64
05-10 10:42:32.045 I/OpenCV/StaticHelper: CMake: 2.8.12.2
05-10 10:42:32.045 I/OpenCV/StaticHelper: CMake generator: Ninja
05-10 10:42:32.045 I/OpenCV/StaticHelper: CMake build tool: /usr/bin/ninja
05-10 10:42:32.045 I/OpenCV/StaticHelper: Configuration: Release
05-10 10:42:32.045 I/OpenCV/StaticHelper: C/C++:
05-10 10:42:32.045 I/OpenCV/StaticHelper: Built as dynamic libs?: NO
05-10 10:42:32.045 I/OpenCV/StaticHelper: C++ Compiler: /usr/bin/ccache /opt/android/android-ndk-r10e/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin/x86_64-linux-android-g++ (ver 4.9)
这篇关于在 Android Studio 上通过 CMake 将 OpenCV 添加到本机 C 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android Studio 上通过 CMake 将 OpenCV 添加到本机 C 代码
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01