在 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:
并且您应该在 logcat 消息中看到指定 OpenCV 已加载(第一个错误是正常的):
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:
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:
Here are the errors I'm getting while building the gradle:
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:
- Within the
opencv
module, edit thebuild.gradle
file as such:
- 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
):
- Within the
app
(application module, could be another name) module, edit thebuild.gradle
file as such:
- 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 :
And you should see within logcat messages specifying the OpenCV has been loaded (the first error is normal):
这篇关于在 Android Studio 上通过 CMake 将 OpenCV 添加到本机 C 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!