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

13 comments:

  1. hey men!! thank you, your tutorial saved my life

    ReplyDelete
  2. I am a final year student doing IT at SLIIT Sri Lanka. For my final year research, I have to do an android project. It is something to do with the lottery system. All that I want is to extract the lottery numbers and the draw date from the lottery after capturing it from the android camera. I have been trying and trying but still not satisfied. I contacted some indian developers but still I was unable to get what I want. I did post on stack over flow as well:

    1. http://stackoverflow.com/questions/16663504/extract-text-from-a-captured-image?noredirect=1#comment23973954_16663504

    2. http://stackoverflow.com/questions/16874949/extracting-information-from-captured-image-in-android/16880026?noredirect=1#comment24352493_16880026

    3. http://stackoverflow.com/questions/17237676/object-detection-for-android-with-tesseract-or-opencv?noredirect=1#comment24978228_17237676

    Please help me out with this. It would be a great, very great help for me and my group members!!!

    ReplyDelete
  3. thanks nisal. it helped me :)

    ReplyDelete
  4. "open cv manager package is not fond".........can anybody hp me ..........i fllowed all the step above but thers is ne error on emulator...plz help me

    ReplyDelete
  5. It says OpenCV Manager was not found. What should I do?

    ReplyDelete
  6. Great ! You solved my problems !!
    Thanks a lot

    ReplyDelete
  7. when i run on emulator it shows opencv package manager not found

    ReplyDelete
    Replies
    1. Launch emulator
      open command prompt
      cd \Android\android-sdk-windows\platform-tools
      adb.exe
      install /apk/OpenCV_2.4.9_Manager_2.18_armv7a-neon.apk

      Delete
    2. command prompt getting error "can't find '/apk/OpenCV' to install"
      how can i solve it..??
      help me

      Delete
    3. please correct it "install ./apk/OpenCV.... " that must be run in platform-tools directory

      Delete
  8. watch this for more information
    https://www.youtube.com/watch?v=ETc8Pyi2vJ4&feature=youtu.be

    ReplyDelete