OpenCV is an open source library which provides a wide range of machine learning and computer vision algorithms. OpenCV original library is written in C++ but there are wrappers for other major programming languages, including Java (for android).
Since I use OpenCV quite often, I decided to dedicate a separate post and write a tutorial on how to add the library to an existing android application, developed in android studio.
General OpenCV Installation
Whether you’re using a real android device for testing and running your application, or using the integrated android emulator inside the android studio IDE, you need to follow those steps:
- Download OpenCV SDK for Android, and extract it somewhere in your home folder – I use the 241 version
- Make sure your App build.gradle file looks like this – you need to add the OpenCV compile command. (Alternatively you can add a new module from the file -> Project Structure menu)
- Create a new folder named jniLibs inside app/src/main and copy all content from SDK_FOLDER/SDK/native/libs into the new folder jniLibs
- Build -> Rebuild Project
In case you encounter build errors, it means your app SDK version and OpenCV SDK version does not match. Open the build.grade file of the OpenCV library (important) and make sure those values match the values in your app build.grade file:
-
compileSdkVersion
-
buildToolsVersion
-
minSdkVersion
-
targetSdkVersion
Developing On a Real Device
In case you connect your real android device to your computer and use it for running your app, you won’t need to install the library on the emulator. In this case, just install the OpenCV manager android app from the play store. You can actually skip this because when you will run your app, your device will automatically ask you to install it.
Developing On The Emulator
In case you use the built in android emulator, you need to install the OpenCV library inside it
- Start the emulator from the android studio by clicking the play button
- Open a terminal and navigate to SDK_FOLDER/apk and execute this command to install the OpenCV in your emulator:
$adb install OpenCV_2.4.11_Manager_2.20_x86.apk
Initialize OpenCV Library In Your Code
Conclusion
Installing OpenCV is not hard but requires some work. Don’t worry, you only need to do it once. I found it much easier to use a real device for development instead of the integrated emulator, a real device just performs better.
Cheers