12.07.20164 min

Zaven sp. z o.o.Zaven sp. z o.o.

OpenCV in Android Studio project

OpenCV in Android Studio project

Are you building an Android app to detect if a photo is blurry or overexposed? Then you should definitely try OpenCV: the library, which provides extensive solutions for analysing and processing photographs. If you’re interested in knowing how to add OpenCV to your project in Android Studio, keep on reading.

Including the library files in Android application project

You don’t have to be an advanced Android developer to use OpenCV in your Android app. First of all, download the OpenCV SDK and add the library to the project you’re working on. This is how you do it: the Android library can be downloaded from the organization’s main website OpenCV.org.

The OpenCV package contains three directories:

  • apk (build directories of the OpenCV application manager)
  • samples (compiled app examples along with their source code)
  • sdk (the library's source code)

The sdk directory is split into two parts: the Android module (inside the java folder) and the native, compiled code located in the native/libs folder.

OpenCV library configuration

After getting initially acquainted, you can move on to the most important step: integrating OpenCV with Android Studio.

First of all, add the OpenCV native code to your project inside Android Studio. For starters, let's change the directory display mode to Project. Adding libraries will become a bit easier thanks to that.

The OpenCV library configuration Change the directory display mode to Project

Now create a new folder in your app's main directory and name it jniLibs: go to app/src/main and right click on inFrom the context menu select: new > Directory. In the dialog box type in jniLibs and confirm. Now copy the native/libs folder content and paste it into the one you have just created in the project.

android app building Including the OpenCV module in your project's file structure

In order for the native code to work, we need to include the OpenCV module itself in the project. As mentioned earlier, the OpenCV module is located in sdk/java. We have to import the entire java directory: go to File > New > Import Module. In the dialogue box enter the path to the module and its name.

Next, change the default name to OpenCV3.1 and press OK. Now you should see an error message about a missing Android API in our sdk.

android app building Without additional configuration, an error will pop up

To fix it, you have to properly configure the module, which means making some changes in just two files. First, go to the OpenCV3.1 module folder (next to src), open the AndroidManifest.xml file and then the build.gradle file.

Now, in the AndroidManifest.xml file, you have to delete the uses-sdk tag and the properties: android:versionCode, android:versionName. The property values have to be transferred to the build.gradle file.

Next in the build.gradle file, set the range of  the supported Android API SDK so that it corresponds to project’s settings and add the versionCode plus the versionName, that were in the AndroidManifest.xml file.

After these changes you have to synchronize the gradle files: click the Sync Project with Gradle Files icon located in the menu bar.

Android application Syncing your project with Gradle files

If no errors pop up, then the OpenCV library in your Android application is configured correctly.

Almost there!

Now what we need to do is add the dependency in the project’s settings. From the Android Studio menu pick: New > Project Structure. In the dialog box move on to the Dependencies bookmark, there on the left select app and then click the green plus button on the right. Choose Module dependency and pick OpenCV in the dialog box.

advanced android development Adding OpenCV to your project dependencies

The last step is adding the library loading code. The code should be joined to the launcher activity of our application.

 static {
        if (OpenCVLoader.initDebug()) {
            Log.i(TAG, "OpenCV initialize success");
        } else {
            Log.i(TAG, "OpenCV initialize failed");

It’s testing time: start the Android application on your smartphone. If your logcat shows something similar to the screenshot below, that means that you created the application correctly. Congrats!

Android application The OpenCV library configuration

For more interesting content follow our blog!