Introduction of OpenCV
OpenCV, short for Open Source Computer VisionLibrary, is written based on C/C++. It is a Computer vision development framework under BSD Open Source license. Its Open Source license allows it to be used for free in academic research and commercial application development. OpenCV supports application development on Windows, Linux, Mac OS, iOS and Android operating systems.
download
- OpenCV官网 the latest version of Android SDK (I use 4.5.0)
- Select the Android platform of the version of OpenCV
An overview of the SDK
The SDK directory structure is as follows:
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.androidCopy the code
directory | file |
---|---|
samples | OpenCV run case |
sdk | OpenCV API and dependent libraries |
sdk/etc | Haar and LBP cascade classifier |
sdk/java | OpenCV Java API |
sdk/libcxx_helper | bring libc++_shared.so into packages |
sdk/native | OpenCV static library, dynamic library and JNI file |
Integrated OpenCV SDK
There are many ways to call OpenCV for image processing in Android applications. Considering performance issues, I recommend using NDK for development. After all, C/C++ is much better than Java performance.
First, integrate OpenCV
OpenCV integration is very simple, we do not need to cross the generation of dynamic/static libraries, decompressed files have included a dynamic library. The usual routine is downloading libraries, importing.h and dynamic/static libraries, and configuring CmakeList. Detailed steps:
1. Download the latest OpenCV SDK
2. Create a project
If the NDK is not available, please download the NDK and import it into the “Project Structure” : If you check “Include C++ support” in your new project, you already support NDK development (that is, native-lib). What you need to do is to add JNI interfaces according to your project needs.
3. Import the. H file and the
Copy the OpenCV SDK include files into the CPP folder, and import the OpenCV dynamic libraries into the jniLibs file
4. CMakeLists. TXT configuration
# 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 a greater immediacy and names the project. Project ("learning") set(LIbs) "${CMAKE_SOURCE_DIR}/ SRC /main/jnilibs") 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 # add opencv_java4 libopencv ${jnigraphics-lib} # Links the target library to the log library # included in the NDK. ${log-lib})Copy the code
5. Modify the build file
Android {compileSdkVersion 30 buildToolsVersion "30.0.2" defaultConfig {···· externalNativeBuild {cmake {cppFlags "-std=c++17" arguments "-dandroid_stl =c++_shared" 'arm64-v8A'}}} ···· externalNativeBuild {cmake {path "cmakelists. TXT "// cmakelists. TXT Path version "3.10.2"// cmakelists.txt version}} compileOptions {sourceCompatibility JavUncomfortable.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }Copy the code
Clean and recompile so that the NDK supports OpenCV development.
The NDK ++ layer can support OpenCV development. If you want to use The OpenCV Java package directly in the Java layer, you also need to import the “OpencV-Android-SDK” Java. Blog.csdn.net/u010097644/…