我试图通过Cmake将Opencv包含在我的原生C代码中.我在网上做了一些研究并从网上下载了FindOpenCV.cmake文件并将其添加到我的android项目的app目录中.这也是CMakeLists.txt所在的位置.我使用本教程:https://www.learn2crack.com/2016/03/setup-opencv-sdk-android-studio.html将OpenCV作为模块导入到我的Android Studio项目中,当我运行时:
if(!OpenCVLoader.initDebug()){
System.out.println("Opencv not loaded");
} else {
System.out.println("Opencv loaded");
}
我知道Opencv已经加载了.
但是,因为我正在尝试将OpenCV添加到我的本机代码而不是Java代码中,所以我认为我不能使用它.这是我现在拥有的CMakeLists:
# 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})
以下是构建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.
所以我的问题是:
>我可以使用导入的OpenCV,还是必须下载不同的opencv并将其存储在其他地方?
>我需要在CMakeLists.txt中更改哪些内容才能构建我的gradle?
理想情况下,我想构建并能够添加#include< opencv2 / opencv.hpp>并使用命名空间cv到我的c文件并添加使用opencv函数的函数.
谢谢你的帮助!
解决方法:
更新22日 – 5月18日:添加了缺失的第6步.
更新10月10日 – 17日:新解决方案使用CMake和Android Gradle插件2.3.1将OpenCV正确集成到应用程序中.使用Android Studio 2.3.1进行测试.
更新11月5日至17日:提供了另一种解决方案
有两种方法可以包含OpenCV.
Git / Simpler Way
手动/高级方式
要将OpenCV库包含到Android Studio Project中,最好在项目中创建一个新的库模块,并将OpenCV Android SDK包中的文件移植到其中:
>选择File> New Module创建一个新模块.
>选择“Android Library”,然后输入详细信息:
>图书馆名称:OpenCV
>模块名称:opencv
>包名:org.opencv
>创建新模块后,将path_to_opencv_sdk / sdk / java / src目录的内容复制到path_to_your_project / opencv / src / main / java中.
>在main下,创建以下直接路径:aidl / org / opencv / engine并将main / java / org / opencv / engine / OpenCVEngineInterface.aidl移入其中.
>将path_to_opencv_sdk / sdk / java / res的内容复制到path_to_your_project / opencv / src / main / res.
>在path_to_your_project / opencv / src /中创建sdk文件夹,并将path_to_opencv_sdk / sdk / native文件夹复制到其中.
>在opencv模块中,创建CMakeLists.txt文件并按以下顺序添加以下行:
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})
>在opencv模块中,编辑build.gradle文件:
...
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']
}
}
}
...
>在应用程序(应用程序模块,可能是另一个名称)模块中,创建/编辑CMakeLists.txt文件并按以下顺序添加以下行(请注意设置为OpenCV_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})
>在应用程序(应用程序模块,可能是另一个名称)模块中,编辑build.gradle文件:
...
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
buildTypes {
...
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
...
compile project(':opencv')
}
>执行gradle同步,现在包含OpenCV本机库,头文件和Java包装器类.
当构建项目并启动apk时,您可以在path_to_project / path_to_app_module / build / output /下检查打包的apk(将apk拖到Android Studio的文本编辑器选项卡上)
您应该在每个abi体系结构文件夹下看到libopencv_java3.so.
在java类中初始化OpenCV SDK:
public class MyClass {
static {
if (BuildConfig.DEBUG) {
OpenCVLoader.initDebug();
}
}
...
}
您应该在logcat消息中看到指定已加载OpenCV(第一个错误是正常的):
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,opencv,cmake,java-native-interface
来源: https://codeday.me/bug/20190713/1453137.html