Android JNI Development Demo by NDK (native develop kit)

Source: Internet
Author: User
Tags tidy

Finally have some free time, so think about the previous Android related projects to tidy up, a few days ago to organize the Android OCR project, today will simply tidy up the NDK JNI calls.


The configuration and development of local calls via JNI in Android has been explained in great detail in the link http://blog.csdn.net/watkinsong/article/details/9849973. Please read it carefully.


At the same time, the description of Android as an OCR has been explained in detail in http://blog.csdn.net/watkinsong/article/details/45541215. Therefore, this article no longer describes how to configure the development environment, how to create projects, and so on, but directly give a project, listing the points to be noted.


1. First, download the project through the Https://github.com/weixsong/libra link and locate the Ndkdemo project.

2. Import the project through Eclipse.

3. Before invoking JNI, we need to define an interface, implement and JNI communication, the interface definition of this example:

Package Com.example.ndk;public class Cannydetect {static {system.loadlibrary ("native");}  /** * @param width * The current            view width * @param height * The current            view height */public static native int[] Cannydetect (int[] buf, int w, int h);}

interface, to load the. So file through a static initialization code block, you do not need to add a suffix name. The interface of the static native is then defined, and the interface needs to conform to the specification of JNI.


4. Now that you have the JNI invocation interface, look at how to write the JNI program:

In the Native.cpp:

#include <jni.h> #include <stdio.h> #include <stdlib.h> #include <opencv2/opencv.hpp>using Namespace Cv;iplimage * Change4channelto3iniplimage (iplimage * src); extern "C" {jniexport jintarray jnicall java_com_ Example_ndk_cannydetect_cannydetect (jnienv* env, Jobject obj, Jintarray buf, int w, int h); Jniexport jintarray jnicall java_com_example_ndk_cannydetect_cannydetect (jnienv* env, Jobject obj, JintArray buf, int W, int h) {Jint *cbuf;cbuf = env->getintarrayelements (buf, 0); if (cbuf = = NULL) {return 0;} Mat myimg (H, W, CV_8UC4, (unsigned char*) cbuf); Iplimage image = Iplimage (myimg); iplimage* Image3channel = Change4channelt O3iniplimage (&image); iplimage* pcannyimage = Cvcreateimage (Cvgetsize (Image3channel), IPL_DEPTH_8U, 1); CvCanny ( Image3channel, Pcannyimage, 3, int* outimage = new int[w * H];FOR (int i = 0; i < w * H; i++) {Outimage[i] = ( int) pcannyimage->imagedata[i];} int size = W * H;jintarray result = Env->newintarray (size); env->setintArrayregion (result, 0, size, outimage), Env->releaseintarrayelements (buf, cbuf, 0); return result;}} Iplimage * Change4channelto3iniplimage (iplimage * src) {if (src->nchannels! = 4) {return NULL;}  Iplimage * destimg = cvcreateimage (cvgetsize (SRC), ipl_depth_8u, 3); for (int row = 0; row < src->height; row++) {for (int col = 0; col < src->width; col++) {Cvscalar s = cvget2d (src, row, col); Cvset2d (destimg, Row, col, s);}} return destimg;}

Before the blog comments, there are always some small partners said Oops, JNI can not find the function of the Divine Code, trouble you first thoroughly read the blog how to write ...

Again, here's a little bit of emphasis:

Jniexport Jintarray jnicall Java_com_example_ndk_cannydetect_cannydetect (* * *)

The declaration of this function must follow the standard specification of JNI, first of all

Jniexport Jintarray Jnicall

Then, Java_ + package name, where the point in the package is replaced with _, and then, the class name of the interface defined in step 3, and then the method name.


5. Take a look at the Android.mk file:

Local_path: = $ (call My-dir) include $ (clear_vars) Opencv_lib_type:=staticopencv_install_modules:=onopencv_camera_ Modules:=offifeq ("$ (wildcard $ (Opencv_mk_path))", "") #try to the load OPENCV.MK from the default install locationinclude/home/ Wesong/software/opencv-2.4.10-android-sdk/sdk/native/jni/opencv.mkelseinclude $ (OPENCV_MK_PATH) endifLOCAL_ MODULE    : = nativelocal_src_files: = native.cpplocal_ldlibs + =  -llog-ldlinclude $ (build_shared_library)

Modify the path of the opencv.mk inside.


6. Take a peek at APPLICATION.MK

App_stl:=gnustl_static  app_cppflags:=-frtti-fexceptions  app_abi:=armeabi armeabi-v7aapp_platform: = Android-19


7. Main program:

Package Com.example.ndk;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.drawable.bitmapdrawable;import Android.os.Bundle;import Android.widget.button;import Android.view.view;import Android.widget.imageview;public class MainActivity extends Activity {ImageView Imgview; Button Btnndk, btnrestore;private String title = "Canny Detect by NDK"; @Overridepublic void OnCreate (Bundle savedinstances Tate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); This.settitle (title); Btnrestore = (Button) This.findviewbyid (R.id.btnrestore); Btnrestore.setonclicklistener (new Clickevent ()); BtnNDK = ( Button) This.findviewbyid (R.ID.BTNNDK); Btnndk.setonclicklistener (new Clickevent ()); Imgview = (ImageView) This.findviewbyid (R.ID.IMAGEVIEW01); Bitmap img = ((bitmapdrawable) getresources (). getdrawable (R.drawable.lena)). Getbitmap (); Imgview.setimagebitmap (img );} Class Clickevent implements View.onclicklistener {public void OnClick (View v) {if (v = = btnndk) {Long current = System.currenttimemillis (); Bitmap IMG1 = ((bitmapdrawable) getresources (). getdrawable (R.drawable.lena)). Getbitmap (); int w = Img1.getwidth (), h = Img1.getheight (); int[] pix = new INT[W * h];img1.getpixels (pix, 0, W, 0, 0, W, h); int[] ResultInt = Cannydetect.cannydetec T (Pix, W, h); Bitmap resultimg = Bitmap.createbitmap (W, H, config.rgb_565); Resultimg.setpixels (ResultInt, 0, W, 0, 0, W, h); Long perform ance = System.currenttimemillis ()-Current;imgview.setimagebitmap (RESULTIMG); MainActivity.this.setTitle ("NDK consumed:" + string.valueof (performance) + "MS");} else if (v = = btnrestore) {Bitmap Img2 = ((bitmapdrawable) getresources (). getdrawable (R.drawable.lena)). Getbitmap (); Imgview.setimagebitmap (IMG2); MainActivity.this.setTitle ("Use OPENCV for image Processing");}}}}





Android JNI Development Demo by NDK (native develop kit)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.