Yes, OpenCV for Android exists! You can now easily do all kinds of cool things, like face detection, color detection, and image segmentation.
This tutorial will show how to install it on MaaXBoard and include the OpenCV samples in your own Android Studio projects! There are a few extra steps to get it to run on developer hardware, so I've outlined them in this project.
Download the OpenCV for Android SDK and APKThe latest android SDK and samples are here (make sure you download the zip file labeled "opencv-4.5.1-android-sdk.zip" - the sourceforge links are a bit confusing).
The most recent version of the prebuilt APK is included in version 3.4.3 of Open-CV android. You can find it here: opencv-3.4.3-android-sdk.zip
Create a new projectIn Android Studio, create a new project.
Note: Skip this step if you already have an Android project you want to use the OpenCV library in.
Import the OpenCV 4.5.1 module you downloaded in the first step into your Android project. Click on File -> New -> Import Module…
On the pop-up that comes up, click on the folder icon to get your module's source directory. The source directory is the "sdk" directory inside the OpenCV-android-sdk folder. The module name automatically shows up as :sdk. Rename it to something more obvious, like :opencv-sdk
Click Finish. Your module should now show up in the sidebar of Android Studio.
Fix Gradle sync errorsYou may have gradle sync errors like the following: "platform "android-26" not found.
To fix this, open the project view. Open the build.gradle file under your opencv module.
Change the compileSDKVersion and targetSDKVersion to newer versions of the SDK. They need a minimum of 28 in order to be able to render the layouts:
Select "sync now" in the dialog box to sync gradle.
Add the OpenCV dependencyClick on File -> Project Structure.
Select Dependencies > app.
Select "Add dependency" and select the checkmark next to your OpenCV module. Make sure. the configuration is set to "implementation" (it should be by default). Click "Apply" and then click "OK."
On your file explorer, navigate to the folder where you extracted the content of the OpenCV Android library zip file. Open the sdk
folder and then the native
folder. Copy the libs folder in the native folder over to your project app
module main folder (Usually ProjectName/app/src/main
). Rename the libs
folder you just copied into your project to jniLibs
.
Normally you would simply download OpenCV from the Google Play Store, but because the MaaXBoard Android build doesn't come with the Google Play store, we'll have to install OpenCV using adb. Choose the apk for arm64-v8:
adb install /OpenCV-android-sdk/apk/OpenCV_3.4.3_Manager_3.43_arm64-v8a.apk
Android 6 and above requires the user approve apps using the camera. Since MaaXBoard is on Android 9, you'll need to explicitly grant permissions. Open Android settings, select apps and notifications, select your app, select permissions, and update to allow camera.
Now that the app is set up, it's easy to paste the sample sample code. For instance, if using the color detection code, go to the folder /samples/color-blob-detection/src/org/opencv/samples/colorblobdetect
Copy ColorBlobDetectionActivity.java
and ColorBlobDetector.java
into the java folder of your app. You can delete MainActivity.java
. Copy color_blob_detection_activity_surface_view.xml
into your layout folder and delete the existing layout. Next, in AndroidManifest.xml make sure that you update <activity android:name="MainActivity" to <activity android:name="ColorBlobDetectionActivity", and finally make sure that your namespace is set to your current package (e.g. package com.example.myfirstopencvapp;) at the top of your files.
That's it! You should be able to now run this app as is!
Note: big thanks to Elvis Chidera for their excellent tutorial here.
Comments