前言
最近研究在Android移動(dòng)設(shè)備上進(jìn)行OpenCV開發(fā), 遇到很多坑假褪,終于成功搭建環(huán)境,現(xiàn)在跟大家分享一下成果皆愉,希望為大家減少不必要的困惑和麻煩艇抠。
開發(fā)工具配置
Android studio 版本2.3.3
下載地址:http://www.android-studio.org/
OpenCV 3.3
下載地址:https://opencv.org/opencv-3-3.html
開發(fā)opencv需要Android NDK 大家可以自行下載NDK庫并配置好環(huán)境變量
新建一個(gè)studio項(xiàng)目 注意一定要勾選支持C++
配置CMakeLists.txt文件 添加文件中缺少的代碼 切勿全部復(fù)制粘貼
# 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)
#工程路徑
#OpenCV-android-sdk路徑
#修改為你的OpenCV-SDK目錄
set(pathToOpenCv /Users/hzzj/Desktop/SDL/Opencv/OpenCV-android-sdk)
#CMake版本信息
cmake_minimum_required(VERSION 3.4.1)
#支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#配置加載native依賴->引入OpenCV頭文件
include_directories(${pathToOpenCv}/sdk/native/jni/include)
#動(dòng)態(tài)方式加載
#引入libopencv_java3.so文件
add_library( opencv_java3
SHARED
IMPORTED)
set_target_properties(opencv_java3
PROPERTIES IMPORTED_LOCATION
../../../../src/main/jniLibs/armeabi/libopencv_java3.so)
# 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.
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 opencv_java3 jnigraphics
# Links the target library to the log library
# included in the NDK.
${log-lib} )
配置build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "27.0.1"
defaultConfig {
applicationId "com.hzzj.opencv.demo"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'armeabi' //添加平臺(tái)架構(gòu)類型
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
配置完成后開始我們項(xiàng)目之旅項(xiàng)目源碼稍后提交