Friday, January 1, 2016

Setting up OpenCV in Android Studio

OpenCV provides an SDK for Android development based on Eclipse ADT plugin. However, Android has migrated from Eclipse ADT to IntelliJ based Android Studio long ago, and recommends all app developers to do so. Unfortunately, the OpenCV documentation and examples are still not updated for Android Studio. I have listed below the steps required to setup an OpenCV based project using Android Studio.

Creating new Android Studio project

  • If Android Studio not installed already, download and install it from http://developer.android.com/sdk/index.html
  • Create a new project in Android Studio, and select a 'No activity' template. 
  • Note down the project folder path. It will be referred as <project root path>, later on.

Adding OpenCV library to project

  • Download the latest version of OpenCV Android from http://sourceforge.net/projects/opencvlibrary/files/opencv-android/ , and unzip it to any folder of your choice. Note down the folder path. It will be referred as <opencv-sdk path> ahead.
  • We will be importing OpenCV as an external module into the project. In Android Studio, click on File > New > Import Module. Now, select Source directory path as <opencv-sdk path>\sdk\java. Rename the module name to opencv. Click on Next, keep the default settings as it is, and then click Finish. Android Studio will import the module into folder named opencv in project root path, and automatically run a Gradle Sync.
  • The Gradle Sync would most probably fail with the error shown below.
  • This error is due to the reference of Android 14 in opencv module's build.gradle file as shown below, which was automatically created by Android Studio from Eclipse project artifiacts. 
  • To fix the error, modify the opencv's build.gradle file by replacing compileSdkVersion, buildToolsVersion, minSdkVersion and targetSdkVersion properties highlighted above, with the respective values found in app module's build.gradle. If the specified SDK or Build Tools version is not present, you will be prompted to download and install it using Android SDK Manager.
  • We need to add module opencv as dependency to main module app. Click on File > Project Structure, select Modules > app in the left pane, go to Dependencies tab, add a new module dependency, and select opencv. Now, run a Gradle Sync by clicking Tools > Android > Sync Project with Gradle files.
  • Finally, we need to copy OpenCV native libraries to the project. Go to <opencv-sdk path>\sdk\native\libs folder and copy armeabi-v7a folder. Go to <project root path>\app\src/main folder, create folder jniLibs, and paste the copied contents. Now, build the project.

Running OpenCV sample project

OpenCV Android SDK contains some working samples with their project source files and APKs, in the samples sub-folder. To run any of the sample, use the following steps.
  • Copy the sample project's src, res sub-folders and AndroidManifest.xml 
  • Go to <project root>\app\src/main folder
  • Delete java and res sub-folders and paste the copied items
  • Rename src folder to java
  • Run the project using emulator, or by connecting to a physical Android device.

Using NDK

All the OpenCV sample projects should work now, except face-detection and tutorial-2-mixedprocessing. These samples contain native C++ code, which needs to be built using NDK (Native Development Kit) using the following steps.
  • Download appropriate version of NDK from http://developer.android.com/ndk/downloads/index.htmland install it. You will be asked to provide a folder path, for extracting the NDK contents.
  • NDK can be used within Android Studio, but seems to be deprecated. Instead, we will be using ndk-build command, to build the code and copy the generated .so file to jniLibs. To execute command ndk-build from Windows Cmd, add NDK folder path to system's Path environment variable.
  • Copy the sample project's jni sub-folder containing the native code, to <project root path>\app\src\main. 
  • In the jni folder, open Android.mk file using any Text Editor, and edit the line
    • include ../../sdk/native/jni/OpenCV.mk   with
    • include <opencv-sdk path>/sdk/native/jni/OpenCV.mk
  • Open cmd.exe as administrator, go to the jni folder, and type the below command
    • ndk-build NDK_LIBS_OUT=..\jniLibs
  • Now, run the project using emulator or by connecting to a physical Android device.


No comments:

Post a Comment