OpenCV簡介
OpenCV,全稱Open Source Computer VisionLibrary,是基于C/C++編寫的,是BSD開源許可的計算機視覺開發框架,其開源協議允許在學術研究與商業應用開發中免費使用它。OpenCV支持Windows、Linux、Mac OS、iOS與Android操作系統上的應用開發。
下載
- 官網OpenCV官網Android最新版本SDK(我自己再用的是4.5.0)
- 選擇OpenCV對應版本下的Android平臺
SDK概述
SDK目錄結構如下:
OpenCV-android-sdk
|_ samples
|_ sdk
| |_ etc
| |_ java
| |_ libcxx_helper
| |_ native
| |_ 3rdparty
| |_ jni
| |_ abi-arm64-v8a
| |_ abi-armeabi-v7a
| |_ abi-x86
| |_ abi-x86_64
| |_ include
| |_ libs
| |_ arm64-v8a
| |_ armeabi-v7a
| |_ x86
| |_ x86_64
| |_ staticlibs
| |_ arm64-v8a
| |_ armeabi-v7a
| |_ x86
| |_ x86_64
|
|_ LICENSE
|_ README.android
目錄 | 文件 |
---|---|
samples | OpenCV運行案例 |
sdk | OpenCV API以及依賴庫 |
sdk/etc | Haar和LBP級聯分類器 |
sdk/java | OpenCV Java API |
sdk/libcxx_helper | bring libc++_shared.so into packages |
sdk/native | OpenCV 靜態庫、動態庫以及JNI文件 |
集成OpenCV SDK
在Android應用中調用OpenCV進行圖像處理的方法有很多種,考慮到性能問題,本人推薦使用NDK進行開發,畢竟C/C++要比Java性能好的多。
一、集成OpenCV
OpenCV集成還是很簡單的,不需要我們自己去交差編譯生成動/靜態庫,解壓后的文件已經包含了動態庫。一般套路都是這樣,下載庫、導入.h和動/靜態庫、配置CmakeList。詳細步驟:
1.下載最新OpenCV SDK
2.新建項目
新建工程一定要勾選“Include C++ support”,這樣新建的Android工程會直接支持NDK開發,避免各種配置問題,如果提示沒有NDK,請下載NDK,并在工程“Project Structure”中導入即可:
新建工程勾選了“Include C++ support”,就已經支持NDK開發了(即native-lib),我們需要做的是,根據自己項目需要,增加JNI接口。
3.導入.h文件和.so動態庫
把OpenCV SDK的include文件復制到cpp文件夾下,把opencv的動態庫引入jniLibs文件下
4.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.10.2)
# Declares and names the project.
project("learning")
set(libs "${CMAKE_SOURCE_DIR}/src/main/jnilibs")
#導入libopencv_java4庫文件
add_library(libopencv SHARED IMPORTED)
set_target_properties(libopencv PROPERTIES
IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_java4.so")
#導入頭文件
include_directories(${CMAKE_SOURCE_DIR}/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.
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)
find_library( # Sets the name of the path variable.
jnigraphics-lib
jnigraphics)
# 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_java4
libopencv
${jnigraphics-lib}
# Links the target library to the log library
# included in the NDK.
${log-lib})
5.修改build文件
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
····
externalNativeBuild {
cmake {
cppFlags "-std=c++17"
arguments "-DANDROID_STL=c++_shared"
//生成.so庫的目標平臺
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
}
····
externalNativeBuild {
cmake {
path "CMakeLists.txt"http:// CMakeLists.txt路徑
version "3.10.2"http://CMakeLists.txt版本
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
clean一下,并重新編譯,這樣NDK就支持OpenCV開發了。
PS:到這里,只是表示NDK C++層可以支持OpenCV開發了,要是想在Java層中,直接使用OpenCV的Java包,還需要導入“OpenCV-android-sdk”的Java,方法是可以參考:http://blog.csdn.net/u010097644/article/details/56849758