有很多问题和答案围绕获取原生opencv的Android构建正确。一些使用gradle,另一些使用外部工具。对于原生OpenCV构建而言,这些众多,复杂且经常相互冲突的描述可能会以一致的起点进行简化;创建一个Android Studio 2.2中测试版项目时,有一个办法,包括C++的支持: OpenCV 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)
你的OpenCV ** build.gradle **文件是什么样的?我们需要知道这些是你正在编译的静态库还是共享库。 –
它看起来并没有做任何有趣的事情:https://github.com/sengsational/HelloCv/blob/master/openCVLibrary310/build.gradle – Dale
OpenCV团队还有更多关于在Android上执行本机代码的内容:http ://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#native-c – Dale