Add OpenCV library into Android Studio

OpenCV (Open Source Computer Vision Library) provides a lot of useful implementation for computer vision and image processing. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. In this article, I am going to show you how to add OpenCV into Android Studio step by step.

First, go to OpenCV release page, download and unzip the Android pack version (2.4.13.2 for example).

Next, lunch Android Studio and open(or create ) a project. In Android Studio, select “File > New > Import Module…”

1. Import Module : File > New > Import Module…

Find your “opencv-2.4.13.2-android-sdk” folder and navigate to “OpenCV-android-sdk\sdk\java”

Select sub directory “OpenCV-android-sdk\sdk\java”

Click “Next”

Select Next

Click “Finish”

Then, you will see “openCVLibrary24132” in your project

Open build.gradle for openCVLibrary 24132, modified “compileSdkVersion”, “buildToolsVersion”, “minSdkVersion” and “targetSdkVersion” to be the same as app build.gradle

Open “Project Structure” in File > Project Structure in Android Studio and add module dependency as shown below

Click “OK”

In Android Studio, change tab to “Project” and create a new directory named “jniLibs” under directory “Your Application Root\app\src\main\”

Copy all files from “opencv-2.4.13.2-android-sdk\OpenCV-android-sdk\sdk\native\libs” into “jniLibs” directory

At last, we need to load OpenCV library in our App. Add the following code in your main activity and your are ready to use any function in OpenCV!

static {
System.loadLibrary("opencv_java");
}