1.在android studio中的配置
1在android studio中创建 Native C++的项目

将CMakelists.txt文件移动到app目录下

到opencv官方 下载android 的sdk 到github上下载
opencv github地址
https://opencv.org/releases/#
将android sdk解压 将 /根目录/sdk/native/lib下的所有文件(so),复制到android studio项目的jniLibs目录下,也将 include/opencv2目录复制到这里



修改Cmake中的配置项
# 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)
# 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.
#支持-std=gnu++11
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)
#set(CMAKE_VERBOSE_MAKEFILE on)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#配置加载native依赖
#include_directories(${pathToOpenCv}/sdk/native/jni/include)
include_directories(src/main/cpp/include)
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../src/main/cpp)
set(CURRENT_DIR ${CMAKE_SOURCE_DIR})
# 在Gradle Console输出信息
# CMAKE_SOURCE_DIR:
message("CURRENT_DIR:" ${CMAKE_SOURCE_DIR})
#动态方式加载
add_library(lib_opencv STATIC IMPORTED ) #表示创建一个导入库,静态方式
#引入libopencv_java4.so文件
set_target_properties(lib_opencv
PROPERTIES
IMPORTED_LOCATION ${CURRENT_DIR}/src/main/cpp/jniLibs/${ANDROID_ABI}/libopencv_java4.so
)
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).
src/main/cpp/native-lib.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.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
android -ljnigraphics lib_opencv)
修改module的gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.testopencv"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"//修改这里
version "3.10.2"
}
}
sourceSets {//添加这里
main {
jni.srcDirs = []
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
opcv在Visual studio中的环境配置
1.需要现在官网下载OPCV的包然后进行安装
2.配置环境变量
找到opencv安装/解压的bin目录,将此目录的路径放到path目录中

3进入vs中进行配置
3.1找到属性管理器
在调试->属性

在opencv的lib目录中找到该文件.lib文件

将该文件名称放置到连接器->输入->附加依赖项中

在VC++目中->包含目录中添加include文件夹

在VC++目中->库目录中添加include文件夹

此时调试会报缺少dll文件
点击调试->选项



在以上配置完成后就可以编译项目使用opencv了,编译完成后可以将上面的选项去掉
网友评论