Hey everyone, today we are going to learn how we can install and use OpenCV in our android application project.
If you have not heard about OpenCV or used it before, this is your opportunity to learn how it can be installed or used in your project.
In subsequently tutorials, we will learn how to use android openCV library to create an android camera application and android document scan app.
There are so much we can do with OpenCV library in android but before we go into that, we will learn more about what openCV is all about.
With reference to OpenCV website, “OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library.
OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.
Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.
The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms.
It has C++, Python, Java and MATLAB interfaces and supports Windows, Linux, Android and Mac OS”
Now that we have an idea of what OpenCV is and it’s usage in computer vision and machine learning, we will proceed to learn how to install and use it in our android application.
1. CREATE NEW ANDROID PROJECT
Lets soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.
Sony Xperia H1313
Min SDK 19
Target SDK 28
To create a new android application project, follow the steps as stipulated below.
Go to File menu
Click on New menu
Click on Android Application
Enter Project name: Opencvexample
Select Empty Activity
Name your activity: MainActivity
Keep other default selections
Continue to click on next button until Finish button is active, then click on Finish Button.
2. CREATE NEW ANDROID PROJECT
At time of writing this application, I am using openCV version 3.4.3. Once you click on the download button on the page above, you will wait for the download to complete.
Once the download is completed, unzip the library folder and you will see a folder structure that appears like the image below.
You are free to go through the different folders in the library. Our first interest is the sdk folder.
Double click the sdk folder to open it. We are going to import the java folder to our android project as a module.
3. IMPORT THE JAVA FOLDER AS A MODULE
Go to Android Studio file menu and follow the path as shown File > New > Import Module.
When the file chooser dialog opens, go to the path where you placed the OpenCV library.
Navigate to the java folder and click OK button.
3. SYNC PROJECT WITH GRADLE
Check if your imported module has been sync by gradle. If not you need to go to your module level gradle and click the sync link.
In most case, you will be faced with a gradle error that says – Failed to find target with hash string ‘android-14’ in :
This error is an indication that since we are using min-sdk version 19, we need to update the sdk compiler and targeted version in our openCV module.
One way to fix this issue is to switch to the project pane in your android studio.
Then, go to the openCV library and find the build.gradle file. You need to change the compileSDKversion and targetSDKVersion with what you have in your android project or the latest version.
Finally, we will repeat the gradle sync process once again. Hopefully, this time around everything will work with no error.
4. ADD OPENCV MODULE AS DEPENDENCIES
For us to use OpenCV in our android project, we are going to add OpenCV as part of our project dependency.
What this implies is that our android project will be able to use or access the available class, interfaces and methods in OpenCV libraries.
To achieve this, you need to click on the File > Project Structure. You will see an open dialog, click on the app module.
Next, you will see some tab buttons at the center of the open dialog, click on the dependencies tab to open up a center box that list all your project dependencies.
At the right column, there is a green plus icon, click on this icon to open an option menu dialog.
Click on the module dependencies, select the OpenCV library as a module to add and click the OK button.
5. NOW ADD THE NATIVE LIBRARIES
I know you will be asking, ooh what again is native libraries? I thought we must have been done by now?
We are almost at the end of this tutorial but before openCV installation is completed, we need to add the native libraries.
Native libraries are library or source code written in a native language other than android default supported languages like Java and kotlin.
Most of the native libraries are written with C and C++ languages and compile to .so and .a files.
Android uses the Java Native Interface API as a bridge to communication with native libraries with android java or kotlin codes.
Also, we will not go into details on how native apps can supports different android CPU architecture but if you are interested you can read more on Android CPUs and Architectures.
Now, we need to navigate once again to the OpenCV folder we download, open the sdk folder, inside the sdk folder, we will import or copy the native folder into our projects src > main > libs folder.
Rename the libs folder to jniLibs. Build the your project and if everything works well, then it is time to go out and take a fresh air.
Android OpenCV contains different sample projects that you can test and see how things work but in our next tutorial we will build a simple camera app using android OpenCV library
Free feel to download the source code from our Github account.
If you have questions or suggestions kindly use the comment box below. If you have done a project with OpenCV before you can as well share it with us.
Have fun and see you on my next tutorial.