在Android中c++里调用opencv

本文介绍了如何在Android Studio项目中解决cpp文件引用OpenCV库时的报错问题。通过在CMakeLists.txt中添加特定配置,包括引入头文件路径、创建动态库、设置库导入位置以及链接库,使得cpp代码能够正确链接到OpenCV库,从而实现OpenCV在cpp中的使用。
摘要由CSDN通过智能技术生成

本文接上文

函数创建方式和上文类似

但cpp调用#include <opencv2/opencv.hpp>会报错找不到

因此要在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链接起来

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值