KBeanie

Using OpenCV library on Android

OpenCV on Android

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 folder is the java wrapper for the OpenCV native libraries. native\libs contains the native code in .so files.

Step 2: Import OpenCV Android library to your application

In Android Studio, go to File -> New -> Import Module and launch the wizard. Navigate to the java directory, give it a name and finish. After that, add this module to your Android application’s dependencies.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *