Environment

  • Operating System: Win 10
  • Android Studio: Version 4.2
  • SDK: API 26: Android 8.0(Oreo)
  • OpenCV: Version 4.5.4

Introduction

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code

The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc. OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 18 million. The library is used extensively in companies, research groups and by governmental bodies

Along with well-established companies like Google, Yahoo, Microsoft, Intel, IBM, Sony, Honda, Toyota that employ the library, there are many startups such as Applied Minds, VideoSurf, and Zeitera, that make extensive use of OpenCV. OpenCV’s deployed uses span the range from stitching streetview images together, detecting intrusions in surveillance video in Israel, monitoring mine equipment in China, helping robots navigate and pick up objects at Willow Garage, detection of swimming pool drowning accidents in Europe, running interactive art in Spain and New York, checking runways for debris in Turkey, inspecting labels on products in factories around the world on to rapid face detection in Japan

It has C++, Python, Java and MATLAB interfaces and supports Windows, Linux, Android and Mac OS. OpenCV leans mostly towards real-time vision applications and takes advantage of MMX and SSE instructions when available. A full-featured CUDAand OpenCL interfaces are being actively developed right now. There are over 500 algorithms and about 10 times as many functions that compose or support those algorithms. OpenCV is written natively in C++ and has a templated interface that works seamlessly with STL containers

Step 1st

安装必要的组件

  1. 打开Android Studio

1

  1. 选择ConfigureSDK Manager
  • 如果在项目界面,则选择ToolsSDK Manager

2

  • 下载NDK以及CMake

3

  1. 下载完成后再创建项目

Step 2nd

创建Android Studio项目

  1. 点击创建项目

4

  1. 模板选择Native C++

5

  1. 选择项目配置

输入项目名称,选择项目路径,所用的语言以及SDK版本

6

由于是Native C++项目,所以需要选择C++的版本,这里我选择了C++14

7

点击finish创建项目

Step 3rd

在项目中导入OpenCV

  1. 首先到OpenCV官网发行页面下载OpenCV
  • 这里我选择了2021-10-11发布的OpenCV-4.5.4版本

8

  1. 下载完成后解压到某个路径下

9

  1. 打开上一步创建的项目
  2. 选择FileNewImport Module

10

  1. 选择.../OpenCV-android-sdk/sdk/java
  • 注意这里是选择/java/文件夹而不是/sdk/文件夹,两种都可以实现导入,但是后续步骤会有不同

11

  1. 编辑模块名
  • 模块名可自定义,方便起见,命名为opencv

12

  1. nextfinish

13

Step 4th

将引入的opencv模块改为库并添加依赖

  1. 由安卓视图切换到项目视图

14

  1. 打开opencv模块中的build.gradle文件
  • 注意这里是打开opencv模块下的build.gradle文件,而不是app模块或项目的文件

15

16

  1. 修改opencv模块中的build.gradle文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apply plugin: 'com.android.application' // 将此处application改为library

android {
compileSdkVersion 30
buildToolsVersion "30.0.2" // 保持此处编译版本与app下的build.gradle一致

defaultConfig { //
applicationId "org.opencv" // 把这三行注释掉
} //

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

修改后的文件如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
apply plugin: 'com.android.library'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

// defaultConfig {
// applicationId "org.opencv"
// }

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
  1. 重新同步编译

17

  1. 给项目添加opencv依赖
  • 选择FileProject Structure

18

  • 选择Dependenciesapp+ Module Denpendency

19

  • 勾选opencv模块并点击OK

20

  • 再次点击OK

21

Step 5th

OpenCV注入APK

  1. 打开.../OpenCV-android-sdk/sdk/native/libs

22

  1. 将其中4个文件夹复制到项目下的libs文件夹中

23

  1. 修改app模块中的build.gradle文件
  • android目录下添加如下内容
1
2
3
4
5
sourceSets {
main {
jniLibs.srcDirs = ["libs"]
}
}
  • android/defaultConfig/externalNativeBuild/cmake目录下添加如下内容
1
2
3
4
cmake {
cppFlags '-std=c++14'
arguments "-DANDROID_STL=c++_shared"
}
  • 修改后的build.gradle文件如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
plugins {
id 'com.android.application'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.androidopencv"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-std=c++14'
arguments "-DANDROID_STL=c++_shared"
}
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
buildFeatures {
viewBinding true
}
sourceSets {
main {
jniLibs.srcDirs = ["libs"]
}
}
}

dependencies {

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.1'
implementation project(path: ':opencv')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
  1. 最后点击同步编译

24

Step 6th

项目环境配置

  1. 打开CMakeLists.txt文件

25

  1. 添加如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
####### OpenCV #######

set( OpenCV_DIR D:\\AndroidStudio\\OpenCV-android-sdk\\sdk\\native\\jni )

find_package( OpenCV REQUIRED )
if(OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
else(OpenCV_FOUND)
message(FATAL_ERROR "OpenCV library not found")
endif(OpenCV_FOUND)

####### ###### #######
  1. 最后点击同步编译
  • 若结果如下图所示,说明配置成功,可以准备开发啦

26

导入成功!