围绕着如何正确构建Android原生OpenCV有很多问题和答案。有些人使用gradle,有些人使用外部工具。这些关于原生OpenCV构建的大量、复杂且经常相互冲突的描述可以通过一个一致的起点来简化;在创建Android Studio 2.2 Beta项目时,可以通过一种方法来包含C支持:第一次
此功能是在2016年6月左右添加的。有关详细信息,请参阅Android tools technical docs。
使用Android Studio 2.2或更高版本以及适用于Gradle 2.2.0或更高版本的Android插件,您可以通过将C和C代码编译到Gradle可与您的APK打包的原生库中,将其添加到您的应用中。然后,您的Java代码可以通过Java原生接口(JNI)调用原生库中的函数。如果您想了解有关使用JNI框架的更多信息,请阅读适用于Android的JNI提示。
检查Include C++ Support
会生成一个名为CMakeLists.txt
的外部构建文件。
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# 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 it 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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
$\{log-lib} )
为了识别使用原生(C++)OpenCV代码的Android项目,项目通常会包含一个*.cpp
文件,其中包含JNIEXPORT
条目沿着使用#include <opencv...hpp>
功能的实现。这与导入OpenCV模块并将libs文件夹复制到jniLibs相反,后者只允许从Java调用OpenCV功能。
是否可以使用此起点来配置OpenCV原生“hello world”应用程序,以证明构建工作正常?
其他信息8月22日
因为这个谜题是关于CMake
的,而不是关于OpenCV的,所以我想我应该给予那些对OpenCV不感兴趣的人一个项目起点。
这是一个youtube video,它显示了如何创建一个新的Android Studio项目,导入OpenCV,配置原生C++构建,从而生成与gitHub中的OpenCV“hello world”应用相同的OpenCV应用。
其他信息8月27日
今天提交的版本基于Bruno Alexandre Krinski的回答编译本机OpenCV调用:https://github.com/sengsational/HelloCv。还有一个关于“安装受阻”消息的单独问题,在安装时,Android警告用户“此应用程序包含试图绕过Android安全保护的代码”。由于我不确定这是构建技术的问题,因此我不会扩展此问题以包括该问题(但如果有人对该问题有意见,请告知)。
# Added 2 path definitions to support 20160825 additions
set(pathToProject C:/Users/Owner/AndroidStudioProjects/HelloCv)
set(pathToOpenCv C:/Users/Owner/OpenCV-3.1.0-android-sdk)
# Added by the IDE on project create
cmake_minimum_required(VERSION 3.4.1)
# Two sets suggested by Bruno Alexandre Krinski 20160825
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
# Addition suggested by Bruno Alexandre Krinski 20160825
include_directories(${pathToOpenCv}/sdk/native/jni/include)
# Added by IDE on project create
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )
# Addition suggested by Bruno Alexandre Krinski 20160825
add_library( lib_opencv SHARED IMPORTED )
# Addition suggested by Bruno Alexandre Krinski 20160825
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
# Added by IDE on project create
find_library( log-lib log )
# Added by IDE on project create, Removed and replace with additional parameter suggested by Bruno Alexandre Krinski 20160825
# target_link_libraries( native-lib $\{log-lib} )
target_link_libraries( native-lib $\{log-lib} lib_opencv)
4条答案
按热度按时间ne5o7dgx1#
看起来您已经导入了opencv模块,现在,打开CMakeList.txt并添加以下行:
并编辑:
以包含您已建立的lib_opencv。若要完成,请加入下列行:
在您的模块应用程序中,如下所示:
和下面的buildTypes:
如需详细信息,请参阅:https://github.com/googlesamples/android-ndk/tree/master/cmake/hello-libs
8xiog9wr2#
使用OpenCV 3.2配置实际上可能非常短:
就是这样,4行代码,不需要复制任何东西到你的项目中。只要确保
OPENCV_HOME
指向OpenCV Android SDK所在的目录。这种方法的另一个好处是,您可以静态地与OpenCV链接,这将大大减少您的应用程序/库的大小。
我在一个Github项目中使用了这种方法:https://github.com/Fotoapparat/FaceDetector
kmbjn2e33#
按照布鲁诺· Alexandria ·克林斯基的答案做,但是
代替这一行
把这行,(我不知道为什么这对我有用)
(除此之外,请遵循上述答案的所有说明)
ih99xse14#
Android平台不支持ABI [armeabi]
支持的ABI包括[arm 64-v8 a、armeabi-v7 a、x86、x86_64]。