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.
Now create a new folder in your app’s main directory and name it jniLibs: go to app/src/main and right click on in. From 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.
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.
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.
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.
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!
For more interesting content follow our blog and tutorials! Next up: few examples of the OpenCV library usage.
A code sample that we’ve used in a commercial Android application can be found on our GitHub page.
Popular posts
From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.
Remember the multimedia craze in the early 2000s? It was everywhere, but did it truly revolutionize our lives? Probably not. Today, it feels like every piece of software is labeled "AI-powered." It's easy to dismiss AI chatbots in construction as just another tech fad.
Read moreFears surrounding external support. How to address concerns about outsourcing software development?
Whether you’ve had bad experiences in the past or no experience at all, there will always be fears underlying your decision to outsource software development.
Read moreWhat do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget
Let’s make it clear: if the capabilities are there, a project is best delivered internally. Sometimes, however, we are missing certain capabilities that are required to deliver said project in a realistic timeline. These may be related to skills (e.g. technical expertise, domain experience), budget (hiring locally is too expensive) or just capacity (not enough manpower). What are good reasons for outsourcing software development?
Read more