Android + OpenCV: Part 1 — Setup

Homan Huang
4 min readApr 18, 2020

--

This is Homan Huang. I have tried OpenCV SDK since the beginning of COVID-19. I am trying to develop an Android app for face check-in service. I will share my experience with OpenCV in a journal series. Of course, the first part is set up. Here is the menu of Part 1:

1. Import Module
2. Match Build.Gradle
3. Add Dependency
4. Add JNI Files
5. Install NDK Tool
6. Code
7. Test

Let’s begin:

😄1. Import Module

First, for all, you need to download the OpenCV Android library. I am trying on OpenCV 3.4.3. You need to download and unzip it to a folder. Mine is “OpenCV-android-sdk-343” with no space or special character. Otherwise, the Android Studio(version 3.6) cannot import the file.

Next, you need to choose an Android project to import its module. I open a new project — “OpenCV Camera Demo”. On top of the menu, let’s click
File-> New->Import Module.

Now, you need to choose your SDK directory.

😤 : Don’t make the thing too complicated. Please do not add any space and special character in your path and folder’s name. Fix it before we continue.

😅2. Match Build.Gradle

Is it Done? Not at all, you need to fix the Gradle files. Please open the Build.Gradle file of OpenCV343 and project Build.Gradle file.

Let’s match the SDK version.

Sync…

An error will pop up if the option of minSDKVersion still exists in OpenCV’s AndroidManifest.XML. Let’s remove it by clicking the blue line and “Do Refactor”.

😎3. Add Dependency

Now, let’s use the new module in your app.

File -> Project Structure… -> Dependencies -> app -> “+” -> 3 Module Dependency

If you don’t have a corrupted download, you shall see OpenCV343.

😐4. Add JNI Files

The OpenCV module is converted from its C/C++ files. So you need the JNI library (*.so files) to support the C/C++. Let’s create a JNI folder if you don’t have one.

(Right click) app ->New -> Folder -> JNI Folder

I rename my jni folder to open_cv_jni.

To use Project view, you can copy the files from sdk/native/libs to your new JNI folder. These are your Java libraries.

😪5. Install NDK Tool

Next, we need to NDK tool to support the C++ files in Android Studio. First, let’s edit gradle.properties to add “android.useDeprecatedNDK=true”.

If you have not installed NDK tool, please follow these steps:

Tools -> SDK Manager -> … SDK Tools -> NDK

Pick the last version and download it. After you finished the download, please click:

File -> Project Structure… -> … (Android NDK location) ->
Pick the Last version of NDK

😃6. Code

Finally, let’s edit the code. I modify the TextView in activity_main.xml.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/opencv_demo"
android:textColor="@android:color/background_dark"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Also, it needs the hardcode strings in strings.xml.

<resources>
<string name="app_name">OpenCV Camera Demo</string>
<string name="opencv_demo">OpenCV Camera\nDemo</string>
</resources>

At MainActivity.kt (I am using Kotlin), I add some functions in the companion object.

companion object {

val TAG = "MYLOG " + MainActivity::class.java.simpleName
fun lgd(s: String) = Log.d(TAG, s)
fun lge(s: String) = Log.e(TAG, s)
fun lgi(s: String) = Log.i(TAG, s)

fun shortMsg(context: Context, s: String) =
Toast.makeText(context, s, Toast.LENGTH_SHORT).show()

// messages:
private const val OPENCV_SUCCESSFUL = "OpenCV Loaded Successfully!"
private const val OPENCV_FAIL = "Could not load OpenCV!!!"

fun checkOpenCV(context: Context) =
if (OpenCVLoader.initDebug()) shortMsg(context, OPENCV_SUCCESSFUL)
else shortMsg(context, OPENCV_FAIL)
}

Simple, isn’t it? Let’s call checkOpenCV() in onCreate().

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

checkOpenCV(this)
}

😅 7. Test

Let’s run the app.

Have you seen the message?

--

--

Homan Huang
Homan Huang

Written by Homan Huang

Computer Science BS from SFSU. I studied and worked on Android system since 2017. If you are interesting in my past works, please go to my LinkedIn.

Responses (1)