在Android中c++里调用opencv

本文接上文

函数创建方式和上文类似

但cpp调用#include 会报错找不到

因此要在cmakelists.txt文件中引入:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

include_directories(C:/Users/ASUS/Desktop/OpencvProject/CVSDK/native/jni/include)

# 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 them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
        lib_opencv # Sets the library as a shared library.
        SHARED
        IMPORTED)
set_target_properties(lib_opencv
        PROPERTIES IMPORTED_LOCATION
        C:/Users/ASUS/Desktop/OpencvProject/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so
        )
add_library( # Sets the name of the library.
        JniImgProc
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        JniImgProc.cpp)
add_library( # Sets the name of the library.
        matrixProc
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        matrixProc.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries 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 this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
#                       JniImgProc
                       matrixProc

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
        lib_opencv)

可以看到新加了很多项:
1、
找你import进入project的opencv SDK中native/jni/include,路径在
C:/Users/ASUS/Desktop/OpencvProject/CVSDK/native/jni/include

然后添加下一条

include_directories(C:/Users/ASUS/Desktop/OpencvProject/CVSDK/native/jni/include)

2、这两句,相当于用已有的 OpenCV 库再新生成一个 lib_opencv 动态库,后面会用

add_library( # Sets the name of the library.
        lib_opencv # Sets the library as a shared library.
        SHARED
        IMPORTED)
set_target_properties(lib_opencv
        PROPERTIES IMPORTED_LOCATION
        C:/Users/ASUS/Desktop/OpencvProject/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so
        )

2、这是引入了一个新的cpp而已

add_library( # Sets the name of the library.
            matrixProc
            # Sets the library as a shared library.
            SHARED
            # Provides a relative path to your source file(s).
            matrixProc.cpp)

3、这里就是把前面生成的lib_opencv链接到matrixProc,也就是链接到我需要使用opencv的cpp

target_link_libraries( # Specifies the target library.
                       matrixProc

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} 
                       lib_opencv)

所以真正能让我在cpp里使用opencv的是include里面的cpp原生代码(用include_directories(C:/Users/ASUS/Desktop/OpencvProject/app/src/main/cpp/include)引入)以及原生代码所需要的动态库,他们包含在libopencv_java3.so中,于是我们用它再生成一个库并和cpp链接起来

你可能感兴趣的:(opencv,opencv,c++,android)