Opencv+JNI

How to Create a C++ Android App, Case Study Opencv

Warning I Assume that you know how to develop apps on android if not then this place is the right place to start

Well Currently am finding programming experience so wonderful with android. A lot of stuff is going on including technological wars. As for me, i would like to begin a post on this page by Describing what is necessary for porting C++ native apps into android. I think you are aware that android is built on top of the linux kernel and that means we can easily port some of the interesting library like ffmpeg.

The most programming language used by android developers is Java.With Java, it is possible to port C++ application via Java Native Interface(JNI). using JNI, a programmer can load C++ libraries and use them in java via a wrapper. For example, i have created a thunb print authentication system for java using JNI. The Authentication System Loads Griaule Libraries via an applet that runs on tomcat web server. The applet does the authentication via a wrapper that talks to the Griaule C++ Dynamic Link Libraries(.dll) for windows.

For this case we want to load linux based libraries(.so) . These libraries can be third party libraries or libraries create by a developer during time of development.When using third party libraries, you need to read the document that it comes with. In this example am narrowing donwn the process of creating libraries for even smaller devices,android and with a specific library opencv.

Opencv is a Computer Vision library and their wiki is pretty good. I tried out the android example on their wiki and i was able to compile the library. The problem is the CVCamera app kept crashing. I thought that i was not doing the work correctly so i downloaded the app from the app market but the problem persisted so i gave up. But there are a number of tutorials online for Building android using JNI or JAVA.I will List a few

  1. http://opencv.willowgarage.com/wiki/Android2.2
  2. http://www.stanford.edu/~zxwang/android_opencv.html

Required Setup

  1. Android SDK ( http://developer.android.com/sdk/index.html )
  2. Eclipse and ADT plug-in
  3. Android NDK r5 by Crystax ( http://www.crystax.net/android/ndk-r5.php ) The Android Native Development Kit (NDK) is a companion tool to the Android SDK that lets Android application developers build performance-critical portions of their apps in native code. This will be necessary because we implement parts of the OpenCV library, which is written in C/C++. The original NDK doesn’t support STL and exceptions and that’s why we use the Crystax version.
  4. I Advise using Linux especially Ubuntu for ease

Android.mk

This is the most important android make file that a developer creates. It is this file that is used by NDK to create libraries that will be used for the project and in this case opencv libraries. Appropriate path for all module to be used in the library project is specified here.This describes the native sources to NDK build system. More

The following Examples assumes you have read some links i provided

 Example 1

C++ Native Source

Header

//this function sets the source image for opencv

JNIEXPORT
jboolean
JNICALL
Java_org_mistaguy_opencv_OpenCV_setSourceImage(JNIEnv* env,
                                             jobject thiz,
                                             jintArray photo_data,
                                             jint width,
                                             jint height);

src

// Set the source image and return true if successful or false otherwise.
JNIEXPORT
jboolean
JNICALL
Java_org_mistaguy_opencv_OpenCV_setSourceImage(JNIEnv* env,
jobject thiz,
jintArray photo_data,
jint width,
jint height)
{
// Release the image if it hasn’t already been released.
if (m_sourceImage) {
cvReleaseImage(&m_sourceImage);
m_sourceImage = 0;
}
m_facesFound = 0;

m_sourceImage = getIplImageFromIntArray(env, photo_data, width, height);
if (m_sourceImage == 0) {
LOGE(“Error source image could not be created.”);
return false;
}

return true;
}

and then call then create a Java interface like this;

Java Access

First of all we have to load the library like this in the wrapper class called opencv.java.Notice the package name is kind of similar to the JNICALL preceding the functions with “_”;

package org.mistaguy.opencv;

public class opencv {

static{
System.loadLibrary(“opencv”);
}

public native boolean setSourceImage(int[] pixels, int width, int height);

}

This way the function can be accessed normally in android

Example 2: Face Detection

header

JNIEXPORT
void
JNICALL
Java_org_mistaguy_opencv_OpenCV_detectdraw(JNIEnv* env, jobject thiz);

src

JNIEXPORT void JNICALL Java_org_mistaguy_opencv_OpenCV_detectdraw(JNIEnv* env, jobject thiz)
{
    int scale = 1;

    // Create a new image based on the input image
    IplImage* temp = cvCreateImage( cvSize(m_sourceImage->width/scale,m_sourceImage->height/scale), 8, 3 );

    // Create two points to represent the face locations
    CvPoint pt1, pt2;
    int i;

    // Clear the memory storage which was used before
    cvClearMemStorage(m_storage);

    // Find whether the cascade is loaded, to find the faces. If yes, then:
    if(m_cascade)
    {

        // There can be more than one face in an image. So create a growable sequence of faces.
        // Detect the objects and store them in the sequence
        CvSeq* faces = cvHaarDetectObjects(m_sourceImage, m_cascade, m_storage,
                                            1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                            cvSize(40, 40) );

        // Loop the number of faces found.
        for( i = 0; i < (faces ? faces->total : 0); i++ )
        {
           // Create a new rectangle for drawing the face
            CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

            // Find the dimensions of the face,and scale it if necessary
            pt1.x = r->x*scale;
            pt2.x = (r->x+r->width)*scale;
            pt1.y = r->y*scale;
            pt2.y = (r->y+r->height)*scale;

            // Draw the rectangle in the input image
            cvRectangle( m_sourceImage, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
        }
    }

    // Show the image in the window named “result”
   // cvShowImage( “result”, img );

    // Release the temp image created.
    cvReleaseImage( &temp );
}

Java Access

public class opencv {

static{
        System.loadLibrary(“opencv”);
    }

public native void  detectdraw();

}

I will posting more detailed examples during my free time

Advertisements

6 thoughts on “Opencv+JNI

  1. hii,,, i am chan from ind..
    i have project nudity detection with opencv n implement on android device,
    i try u’r tutorial but fail, can u share u’r code for me?..please help me…

  2. Can u give me a detailed explanation of how to use opencv functions in android…..I have created a libopencv.so file using ndk-build but I am not getting how to use opencv functions after that…help….its..urgent
    can u please send some source code of android-opencv

  3. I have taken long because i wanted to create a new application for this particular tutorial but i have failed to do so. The above links should have helped but the good news is that i landed on the source code for one of the projects. at that point the project was in its initial stages. so you are free to use it for learning purposes. I hope that your choices to use C++ was as a result of performance or love for C++ but if you know how to code in java, javacv is the answer and i have a couple of links and projects for that.

    here is the link to the source code

    https://skydrive.live.com/redir.aspx?cid=ddfd376e9dfca134&resid=DDFD376E9DFCA134!342&parid=root

  4. Hi mistaguy , I am trying to develop face detection android application using open cv. i just want to detect the face in the image which choosed from the Gallery. could you help me to out the problem. If you give sample source to do that means i will be the happiest man in the world. Hoping for better response.. Thanks in advance.

  5. Hello!

    I have one question, how do u load haarcascade xml file from jni/C++/C code? can u share some code? thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s