OpenCV for Android可以利用标准的C ++支持来获得Android Studio 2.2 for Windows上的本机构build支持吗?

有很多问题和答案围绕获取原生opencv的Androidbuild设正常。 有些使用gradle,有些使用外部工具。 对于本地OpenCV构build而言,这些众多,复杂且经常相互冲突的描述可能会以一致的起点进行简化; 在创buildAndroid Studio 2.2 Beta项目时,有一种方法可以包含C ++支持: 包括C ++支持 在这里输入图像说明

此function是在2016年6月左右添加的。有关更多信息,请参阅Android工具技术文档 。

使用Android Studio 2.2或更高版本以及Gradle 2.2.0或更高版本的Android插件,可以将C和C ++代码编译为Gradle可以用APK打包的本地库,从而将其添加到应用程序中。 然后,Java代码可以通过Java本地接口(JNI)调用本地库中的函数。 如果您想了解更多关于使用JNI框架的信息,请阅读Android的JNI提示。

检查Include C++ Support生成一个名为CMakeLists.txt的外部构build文件。

 # 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>function的实现。 这与导入OpenCV模块和将libs文件夹复制到仅允许从Java调用OpenCVfunction的jniLibs相反。

是否有可能使用这个起点configuration一个OpenCV原生的'你好世界'的应用程序,certificate构build工作?

其他信息8/22
由于这个难题是关于CMake而不是关于OpenCV,所以我想我会给那些对OpenCV不感兴趣的项目出发点。 您可以使用Android Studio中的OpenCV中的信息来合理快速地获得起点项目。

这是一个youtubevideo ,显示了一个新的Android Studio项目的创build,导入OpenCV,configuration本地C ++构build,导致OpenCV“hello world”应用程序等于gitHub中的应用程序。

其他信息8/27
根据Bruno Alexandre Krinski的回答,今天承诺的版本会编译原生OpenCV调用: https : //github.com/sengsational/HelloCv 。 还有一个关于“安装阻止”消息的单独问题,安装时,Android会警告用户“这个应用程序包含试图绕过Android安全保护的代码”。 由于我不确定这是一个构build技术的问题,我不会扩大这个问题,以包括这个问题(但如果有人对这个问题的投入,请告知)。

 #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) 

看来你已经导入了opencv模块,现在打开你的CMakeList.txt并添加以下几行:

 set(CMAKE_VERBOSE_MAKEFILE on) add_library(lib_opencv SHARED IMPORTED) set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION path to your project/MyApplication/app/src/main/jniLibs/${ANDROID_ABI} libopencv_java3.so) include_directories(path to opencv directory/OpenCV-android-sdk/sdk/native/jni/include) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") 

并编辑:

 target_link_libraries( # Specifies the target library. native-lib lib_opencv # Links the target library to the log library # included in the NDK. $\{log-lib} ) 

包含你创建的lib_opencv。 要完成,请添加以下行:

 abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64' 

在你的模块应用程序中,像这样:

 externalNativeBuild { cmake { cppFlags "-std=c++11 -frtti -fexceptions" abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64' } } 

以及您添加的buildType的下方:

 sourceSets { main { jniLibs.srcDirs = ['path to your application /MyApplication/app/src/main/jniLibs/'] } } 

有关更多的细节,你可以看到这个: https : //github.com/googlesamples/android-ndk/tree/master/cmake/hello-libs

OpenCV 3.2配置实际上可能非常短:

 set(OpenCV_STATIC ON) set(OpenCV_DIR ${OPENCV_HOME}/sdk/native/jni) find_package (OpenCV REQUIRED) target_link_libraries(native-lib ${OpenCV_LIBS}) 

就是这样,4行,不需要复制任何东西到你的项目中。 只要确保OPENCV_HOME指向OpenCV Android SDK所在的目录即可。

这种方法还有一个好处 – 你可以静态链接到OpenCV,这将大大减少你的应用程序/库的大小。