Friday, March 15, 2013

OpenCV on Android

First of all, you need JDK installed on your computer. (JRE is not sufficient). Unless download and install it.
http://www.oracle.com/technetwork/java/javase/downloads/index.html 

You definelty need android SDK ADT Bundle. 
http://developer.android.com/sdk/index.html

Simply extract it. Open Adroid SDK Manger from <extracted path>. Android 3.0 or higher must be installed.   



<extracted path>\eclipse\eclipse.exe is your IDE.

First time you start eclipse IDE it will ask you to select working directory. (Close the welcome page) Working directory is the place you save you android projects. Therefore select a known <working directory> path.

If you do not have experience in android programming, do go through this article,
http://developer.android.com/training/basics/activity-lifecycle/starting.html
because android is not like a simple c program, it is a state machine.


Now it is the time to download OpenCV-Android-SDK from here,
http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.4.3/OpenCV-2.4.3.2-android-sdk.zip/download

Extract it to <working directory>.  

Now you your installations are ready. Open Android Eclipse IDE. 
File -> Import -> General -> Existing Projects into Workspace -> Next




Set the root directory path as OpenCV Android SDK folder. In the figure above "E:\AndroidOpenCV\" is my working directory.

From the projects list, only select the ones I have selected if you are new and finish it. Others require Android NDK.

First one in the list, OpenCV Library, is the most important. It must be in your working directory every time you build a OpenCV project. Others are some examples comes with OpenCV, that you can learn a lot from them.



If everything goes well you must see a screen like this. If so you are lucky and you can skip to Run Example Section of this post. 

In some case you will not be such lucky, and there will be some error on the screen.
First of all make sure OpenCV Library is on you Package Explorer. 

Right Click OpenCV Library on Package Explorer -> Properties

In Android tab check whether at least one project target is selected above Android 3.0

In Java Compiler tab check the java version is correctly configured. 

Then errors in OpenCV library must be eliminated. 

Then Right click a sample (OpenCV-Sample-image-manupilations) on Package Explorer -> Properties

In Android tab check whether at least one project target is selected above Android 3.0
and OpenCV Library is added as in this figure.



Run Example

This is what I summarized here.
http://developer.android.com/training/basics/firstapp/running-app.html

Run On Emulator

In your Eclipse IDE, 
Window -> Android Virtual Device Manager -> New



Set as above figure and OK 
Select TestAVD you created -> Start
Close Virtual Device Manager.

In Package Explorer in Eclipse IDE, 
Right click the OpenCV - Sample - image-manipulations -> Run As -> Android Application.

This will run the application on your emulator :)


Run On Real Device

First of all you need an Android Device with Android 3.0 or higher and you must have USB drivers installed. Unless get them from manufactures website or may be here, http://developer.android.com/tools/extras/oem-usb.html . 

Turn on Debugging mode on you android device. For example, in Samsung Galaxy, 
Settings -> Developer Options -> Check USB Debugging, Check Allow mock locations.

Then connect your phone to computer.


In Package Explorer in Eclipse IDE, 
Right click the OpenCV-Sample-image-manipulations -> Run As -> Android Application.

If prompt select your real device. Then application will be installed and run on your device. 

In the first time use it will automatically download OpenCV Manager from Google Play.

The OpenCV sample will be running on you real device. Try Edge detection on your android device :)


These are the Tutorial provided from OpenCV. They may also helpful to you.
http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/O4A_SDK.html#o4a-sdk
http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/android_dev_intro.html