2016-08-15 57 views
14

有很多问题和答案围绕获取原生opencv的Android构建正确。一些使用gradle,另一些使用外部工具。对于原生OpenCV构建而言,这些众多,复杂且经常相互冲突的描述可能会以一致的起点进行简化;创建一个Android Studio 2.2中测试版项目时,有一个办法,包括C++的支持: Include C++ Supportenter image description hereOpenCV for Android可以利用标准C++支持在Android Studio 2.2 for Windows上获得本机构建支持吗?

此功能是围绕6月新增的2016见Android tools technical docs以获取更多信息。

使用的Android Studio 2.2中或与Android插件的摇篮版本2.2.0或更高更高,您可以通过它编译成摇篮可以与您的APK包本机库添加C和C++代码到你的应用程序。然后,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文件包含与使用#include <opencv...hpp>功能实现沿JNIEXPORT条目。这与导入OpenCV模块并将libs文件夹复制到仅允许从Java调用OpenCV功能的jniLibs相反。

是否有可能使用此起点来配置OpenCV原生'hello world'应用程序,证明构建正在工作?

附加信息8/22
由于这让人不解的是约CMake少约OpenCV的,我想我给了那些在OpenCV的不感兴趣的一个项目的起点。您可以使用OpenCV in Android Studio中的信息合理快速地获得起点项目。

这是一个youtube video,它显示了一个新的Android Studio项目的创建,导入OpenCV,配置本机C++构建,导致OpenCV“hello world”应用程序等于gitHub中的应用程序。

附加信息8/27
今天犯下的版本的基础上,从布鲁诺亚历山大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) 
+0

你的OpenCV ** build.gradle **文件是什么样的?我们需要知道这些是你正在编译的静态库还是共享库。 –

+0

它看起来并没有做任何有趣的事情:https://github.com/sengsational/HelloCv/blob/master/openCVLibrary310/build.gradle – Dale

+0

OpenCV团队还有更多关于在Android上执行本机代码的内容:http ://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#native-c – Dale

回答

15

看来你已经导入了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' 
    } 
} 

及以下buildTypes的添加:

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

欲了解更多详情,您可以看到:https://github.com/googlesamples/android-ndk/tree/master/cmake/hello-libs

+0

这些建议的更改已合并到GitHub上的示例代码中,并且该项目现在编译,尽管存在安全警告。 https://github.com/sengsational/HelloCv – Dale

+1

摇滚人,你救了我。非常感谢!!! – hunghd

+1

你为什么写'$ \ {log-lib}而不是'$ {log-lib}'? –

3

随着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,这将大大减少你的应用程序/库的大小。

+0

似乎是好的,因为我的Android项目在CMake中链接CMake另一个CMake子项目正确构建 –

+0

您的意思是只添加上述行到'CMakeLists.txt'? – user8663682

+0

我怎么知道'OPENCV_HOME'是否指向写作地点? – user8663682