在Android中c++里调用opencv
Posted _less is more
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android中c++里调用opencv相关的知识,希望对你有一定的参考价值。
本文接上文
函数创建方式和上文类似
但cpp调用#include <opencv2/opencv.hpp>
会报错找不到
因此要在cmakelists.txt文件中引入opencv:
# 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/app/src/main/cpp/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.
# libopencv_java3
# # Sets the library as a shared library.
# SHARED
#
# IMPORTED)
#set_target_properties(libopencv_java3
# PROPERTIES IMPORTED_LOCATION
# C:/Users/ASUS/Desktop/OpencvProject/app/libs/x86/libopencv_java3.so
## $CMAKE_SOURCE_DIR/libs/x86/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 )
可以看到新加了很多项:
1、
这里是Android opencv的sdk里的include文件,路径在
C:\\Users\\ASUS\\Desktop\\opencv-3.4.3-android-sdk\\sdk\\native\\jni\\include
,把它复制到工程里面
然后添加下一条
include_directories(C:/Users/ASUS/Desktop/OpencvProject/app/src/main/cpp/include)
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、这也是加了个matrixProc而已
target_link_libraries( # Specifies the target library.
JniImgProc
matrixProc
# Links the target library to the log library
# included in the NDK.
$log-lib )
所以真正能让我在cpp里使用opencv的是include里面的cpp原生代码,将其用include_directories(C:/Users/ASUS/Desktop/OpencvProject/app/src/main/cpp/include)
引入即可
以上是关于在Android中c++里调用opencv的主要内容,如果未能解决你的问题,请参考以下文章
从 Android 中的本机 c++ 文件调用本机 opencv
C++的opencv里怎么读取Mat图像中特定坐标的灰度值?