OpenCV with Android on Eclipse

Untitled

Pre-Requisites:

If you haven’t already done it, get started with OpenCV from here. Also, OpenCV with Java tutorial is here.

Download opencv-android-sdk from here.

Download and install JDK from here

Download and Install Eclipse from here.

Download and extract Android NDK from http://developer.android.com/tools/sdk/ndk/index.html

Now, edit the PATH in Environment Variables to add a new variable named NDKROOT and give its value as the NDK’s extracted folder destination.

 

  1. Start Eclipse, then select Help ‣ Install New Software…
  2. Click Add. In the Add Repository dialog that appears, enter “ADT Plugin” for the Name and the following URL for the Location:   https://dl-ssl.google.com/android/eclipse/
    Untitled
  3. In the Available Software dialog, select all and click Next and accept the License to install all the packages.

OpenCV integration with Eclipse:

In eclipse workspace, right click on the left window and click import.

Untitled

General -> Existing Project into Workspace -> Browse to opencv-android-sdk folder

It should automatically load all the sample projects. If you have any errors, go to the end of the post to troubleshoot.

If you don’t have any errors, build the project and to run it,

First, connect your android device through USB. Go to phone settings -> Developer Options -> USB Debugging on

Then, go to play store and download OpenCV Manager app.

Untitled

In the next window, select your Android device and the app runs on your mobile.

If you are having troubles with detecting your device, maybe you would have to install drivers from here.

If you want to set up a virtual device, go to here.


 

Adding C++ code to your Android Application:

Create a new Android Application and follow the defaults.

Untitled

Now, after you’ve created the project,

Right click the project in Project Explorer -> New -> Other.. -> C++

Untitled

Untitled

Untitled

Click Finish. Now go to Project Properties -> C/C++ Build,

Untitled

Also, go to the Behavior tab and

Untitled

copy the jni folder from other projects (Mixed Processing or Face detection) into the current project.

Next, go to the copied jni folder, open OpenCV.mk file and copy these 2 lines

Untitled

Also, give the absolute path to the OpenCV.mk file which will be in your

Now, go to Project Properties -> C/C++ General -> Paths and Symbols and add the following

Untitled

Note: The last path should be your path-to-Android-sdk/sdk/native/jno/include

Troubleshooting:

First, if you get many errors, configuration problem might be the cause. For this, right click on each project Properties -> Android and click on any build targetUntitled

If you get the error “Program ‘make’ not found in the PATH”, then

Right click on the project. Go to C++ Build and paste ${NDKROOT}/ndk-build.cmd in builder as shown

Untitled

Note: Make sure NDKROOT is in your environment variables

Next, go to Behavior and do as below

Untitled

Now, right click on project and clean. If you still get an error, copy the jni folder from other projects (Mixed Processing or Face detection) into the current project.


If there’s an error generating R file. Like the one below,

Untitled

The reason is that, if you included the generated appcompat_v7, this and your project should run the same version of android.

Untitled

Change both appcompat_v7 and your project’s target platform to the latest android version (in my case API 23) and clean and build your project.


If you get any error in “appcompat“, like

Then, right click on its project properties and change the android apk to the latest version.

Then, Project -> Clean all


For OpenCV on Android Studio, go here.

5 Comments

  1. android apk download location

    An impressive share! I’ve just forwarded this onto
    a colleague who had been doing a little research
    on this. And he actually ordered me dinner because I discovered it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending time to talk about this matter
    here on your site.

    Reply
    1. admin

      You are welcome. Thanks for the appreciation. 😉

      Reply
  2. admin

    Thank you

    Reply
  3. android apk apps mobile9

    Hello fantastic blog! Does running a blog such as this require a lot of work?
    I’ve very little knowledge of programming but I was hoping to start my own blog soon. Anyways,
    if you have any ideas or techniques for new blog
    owners please share. I understand this is off subject nevertheless I
    simply needed to ask. Many thanks!

    Reply
    1. admin

      We are just trying to share our knowledge. A small effort to payback to the community. Whatever knowledge you have, if you think you can share it, start your blog.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *