Android的OpenCV可以利用标准的C ++支持在Windows的Android Studio 2.2上获得本机版本支持吗? [英] Can OpenCV for Android leverage the standard C++ Support to get native build support on Android Studio 2.2 for Windows?

查看:262
本文介绍了Android的OpenCV可以利用标准的C ++支持在Windows的Android Studio 2.2上获得本机版本支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

围绕正确获取适用于Android的本机opencv,有很多问题和答案.有些使用gradle,有些使用外部工具.对于本机OpenCV构建,这些众多,复杂且经常相互矛盾的描述可能会以一致的起点简化.创建Android Studio 2.2 Beta项目时,有一种包含C ++支持的方法:

There are many questions and answers surrounding getting native opencv for android building properly. Some use gradle, others use external tools. These numerous, complicated, and often conflicting descriptions for native OpenCV builds might be simplified with a consistent starting point; when creating an Android Studio 2.2 Beta project, there is an way to include C++ support:

此功能于2016年6月前后添加.请参见 Android工具技术文档有关更多信息.

This feature was added around June of 2016. See Android tools technical docs for more information.

使用Android Studio 2.2或更高版本以及Gradle 2.2.0或更高版本的Android插件,您可以将C和C ++代码编译到Gradle可以与APK打包在一起的本机库中,从而将C和C ++代码添加到您的应用中.然后,您的Java代码可以通过Java本机接口(JNI)调用本机库中的函数.如果您想了解有关使用JNI框架的更多信息,请阅读Android的JNI技巧.

Using Android Studio 2.2 or higher with the Android plugin for Gradle version 2.2.0 or higher, you can add C and C++ code to your app by compiling it into a native library that Gradle can package with your APK. Your Java code can then call functions in your native library through the Java Native Interface (JNI). If you want to learn more about using the JNI framework, read JNI tips for Android.

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

Checking the Include C++ Support generates an external build file called 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功能.

To recognize an Android project that uses native (C++) OpenCV code, the project will typically include a *.cpp file containing JNIEXPORT entries along with implementations that use #include <opencv...hpp> functionality. This, as opposed to importing the OpenCV module and copying the libs folder into jniLibs, which only allows calling OpenCV functionality from Java.

是否可以使用此起点来配置OpenCV本机"hello world"应用程序,以证明该版本正在运行?

Is it possible to use this starting point to configure a OpenCV native 'hello world' app, proving the build is working?

其他信息8/22
由于这个难题是关于CMake的,而不是关于OpenCV的,所以我想为那些对OpenCV不感兴趣的人提供一个项目起点.使用 Android Studio中的OpenCV 中的信息,您可以使起点项目相当迅速地进行.

ADDITIONAL INFORMATION 8/22
Since this puzzle is about CMake and less about OpenCV, I thought I'd give out a project starting point for those not interested in OpenCV. You could get the starting point project going reasonably quickly using the information in OpenCV in Android Studio.

以下是显示创建内容的 youtube视频 一个新的Android Studio项目的示例,导入OpenCV,配置本机C ++构建,从而导致OpenCV"hello world"应用程序等于gitHub中的应用程序.

Here is a youtube video that shows the creation of a new Android Studio project, importing OpenCV, configuring the native C++ build, resulting in the OpenCV "hello world" application that's equal to the one in gitHub.

其他信息8/27
今天提交的版本基于Bruno Alexandre Krinski的回答可以编译本机OpenCV调用: https ://github.com/sengsational/HelloCv .关于安装被阻止"消息存在一个单独的问题,在安装过程中,Android会警告用户此应用包含试图绕过Android的安全保护的代码".由于我不确定这是构建技术中的问题,因此,我不会扩展此问题以包括该问题(但是,如果有人对此问题有意见,请提出建议).

ADDITIONAL INFORMATION 8/27
The version committed today, based on the answer from Bruno Alexandre Krinski does compile native OpenCV calls: https://github.com/sengsational/HelloCv . There is a separate problem concerning the "Installation Blocked" message, where, upon installation, Android warns the user "This app contains code that attempts to bypass Android's security protections." Since I am unsure that this is an issue with the build technique, I will not expand this question to include that issue (but if someone has input on that problem, please advise).

#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并添加以下几行:

It seems you already have imported the opencv module, now, open your CMakeList.txt and add the follow lines:

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.最后,添加以下行:

to include your lib_opencv that you have created. To finish, you add the follow line:

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

在您的模块应用中,如下所示:

in your module app, like this:

externalNativeBuild {

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

及以下您添加的buildTypes:

and below of buildTypes you add:

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

有关更多详细信息,您可以看到以下内容: https://github.com/googlesamples/android-ndk/tree/master/cmake/hello-libs

For more details, you can see this: https://github.com/googlesamples/android-ndk/tree/master/cmake/hello-libs

这篇关于Android的OpenCV可以利用标准的C ++支持在Windows的Android Studio 2.2上获得本机版本支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆