OpenCV (Open Source Computer Vision Library), a popular and widely used computer-vision library, supports a wide range of platforms.
If you want to implement any kind of moderately complex image processing, this is the best library to start with. In this post, we go through the steps to add the OpenCV library as a module to your Android application project.
Step 1: Download OpenCV SDK for Android
Head over to this link to download the Android pack from the releases page of OpenCV SDK for Android. Unzip the folder, which will have the following contents.
There are 2 folders of importance here. sdk\java
native\libs
contains the native code .so
Step 2: Import OpenCV Android library to your application
In Android Studio, go File -> New -> Import Module
java
Step 3: Adjust relative paths of the OpenCV build.gradle
file
Open the build.gradle
file and fix the paths.
sourceSets {
main {
jniLibs.srcDirs = ['../../jni']
java.srcDirs = ['src']
aidl.srcDirs = ['src']
res.srcDirs = ['res']
manifest.srcFile 'AndroidManifest.xml'
}
}
Step 4: Copy the . so files to your application folder
Copy the sdk\native\libs
folder to your applications folder, src\main
folder. Rename this folder to jniLibs
.
Step 5: Load the opencv_java4
library
Before you can use any OpenCV related features, you will have to execute this command.
System.loadLibrary("opencv_java4");
That’s it. You are ready to get started with OpenCV on Android.