2012年3月23日 星期五

Android with OpenCV native c++ code

This is for the OpenCV 2.3.1 version and using the prebuild version from official site.

First,download these three things and remember to set up eclipse with android-sdk.

1.Android-ndk 

2.opencv-android 

3.cygwin 

--------------------------------------------------Install Step----------------------------------------------------------

[wp_ad_camp_2]
1.Install cygwin.

There will be a step asking Download sites.You can find or add ftp://mirrors.kernel.org.

After clicking next, there has select package window.

Search gcc and make in the search place, and change the Devel Default to Devel Install.

2.Decompress android-ndk

I place andorid-ndk to D:\android-ndk and cygwin to C:\cygwin

Then add D:\android-ndk and C:\cygwin\bin to Environment Variables' Path

3.Create a android project

Project name: opencvtest

Build Target: Android 2.2

Application name: Opencvtest

Package name: com.test.opencv

Create Activity: Opencvtest

4.Create a jni folder

Create a "jni" folder in project opencvtest

5.Copy OpenCV-2.3.1 to project opencvtest

Decompress OpenCV-2.3.1-android-bin.tar.bz2 which was downloaded before.

Copy the OpenCV-2.3.1 folder to project opencvtest

6.Create image.cpp in jni folder

This is the native opencv code part but we need to write in jni format.

Here has an example,just copy it and paste to image.cpp to run.

http://gist.github.com/1167548

The file name (image.cpp) need to be the same in Android.mk at LOCAL_SRC_FILES

7.Add native part to Opencvtest.java

To use jni we need to add two things into our android code

publice native void image();


static{ System.loadLibrary("imagefeature") };


The first part is about the function for native c++ code.

The second part is loading library we create from jni.

This is the last sample code after adding these two parts.

http://gist.github.com/1167546

The library name need to be the same in Android.mk at LOCAL_MODULE

8.Create Android.mk in jni folder

Copy these code into Android.mk

http://gist.github.com/1167551

Remember that LOCAL_SRC_FILES and LOCAL_MODULE need to be the same name in Step.6 and Step.7 for the file name and the library name.

9.Create Application.mk in jni folder

http://gist.github.com/1167550

The APP_ABI := armeabi is for emulator, change to APP_ABI := armeabi-v7a is for android to use

10.Run ndk-build in cygwin

Open cygwin shell, switch to java wordspace, and enter in opencvtest.

Run ndk-build in the command line, if there has error, ndk-build will tell you where is it.

11.Add write sdcard permission

Open AndroidManifest.xml add this line before <application

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

12.Upload file to mnt/sdcard/

Use file explorer to upload c.jpg into mnt/sdcard folder

13.Run!!

[wp_ad_camp_1]

2 意見:

ebit 提到...

I tried it but in image.cpp there are some header unresolved inclusion such as jni.h, stdio.h,and stdlib.h. Have you idea ??
thanks

mar 提到...

Hi,
when I run ndk-build in the command line, I have this:

./obj/local/armeabi/libopencv_contrib.a: No such file: Permission denied
collect2: ld returned 1 exit status
/cygdrive/c/android/NDK/build/core/build-binary.mk:366: recipe for target `obj/local/armeabi/libimagefea.so' failed
make: *** [obj/local/armeabi/libimagefea.so] Error 1

Can you help me please

張貼留言