jni Static (statically) loading OpenCV
This address: Http://blog.csdn.net/caroline_wendy
Steps:
1. Prepare the Opencv-android library
Copy the OpenCV SDK to the working folder.
Download OpenCV's latest android-sdk: opencv-2.4.9-android-sdk;
copy The SDK folder to the working directory; The SDK folder contains native, Java, etc. three folders.
2. Loading the Opencv-android library
Import The Java folder of the working directory, set as the library for the current project .
[Project name] Right click->android->add library-> Select the added library. Can. you do not need to select the IS Library.
3. Create the
Jniclient class.
Contains the methodthat needs to be used, can be multiple .
/** * */package com.example.hellomyjni;/** * @author Administrator * */public class Jniclient {static public native St Ring Sayname (); static public native int[] grayimage (int[] pixels, int w, int h);}
use the command line to generate the header file.
Enter the project folder, generate the JNI header file, and use the command:
"Javah-classpath bin/classes-d jni com.example.hellomyjni.JniClient"
Command parsing:
Javah Generate header file;
-classpath uses the location of the class (Bin/classes), which are. class files;
-D JNI requires the generation of JNI classes (com.example.hellomyjni.JniClient), including [package]. [classname].
Other references: http://blog.csdn.net/caroline_wendy/article/details/39032551
4. Modify the JNI configuration file
You need to create application.mk within the Jni folder , primarily for the use of the C + + standard (APP_STL), and for libraries that need to be compiled.
App_stl: = gnustl_staticapp_cppflags: =-frtti-fexceptionsapp_abi: = armeabi-v7a
Modify
android.mk, do not need to use opencv_lib_type:=static
Local_path: = $ (call My-dir) include $ (clear_vars) #OPENCV_LIB_TYPE: =staticopencv_install_modules:=oninclude c:/users /administrator/android-workspace/opencv-sdk/native/jni/opencv.mklocal_module : = HelloMyJniLOCAL_SRC_FILES: = Hellomyjni.cpplocal_ldlibs + = -llog-ldl#local_c_includes + =/local_src_files + =/sayname.cppinclude $ (BUILD_ Shared_library)
5. JNI functions
#include <jni.h> #include <opencv2/opencv.hpp>using namespace cv; #include "com_example_hellomyjni_ JniClient.h "#include" sayname.h "Jniexport jstring jnicall java_com_example_hellomyjni_jniclient_sayname (jnienv *env , jclass) {string str = Sayname (); return Env->newstringutf (Str.c_str ());} Jniexport jintarray jnicall java_com_example_hellomyjni_jniclient_grayimage (jnienv *env, Jclass, JintArray buf, Jint W, Jint h) {Jint *cbuf;cbuf = Env->getintarrayelements (buf, false); if (cbuf = = NULL) {return 0;} Mat imgdata (H, W, CV_8UC4, (unsigned char*) cbuf); uchar* ptr = imgdata.ptr (0); for (int i = 0; i < w * H; i++) {int Gray scale = (int) (PTR[4 * i + 2] * 0.299 + ptr[4 * i + 1] * 0.587+ ptr[4 * i + 0] * 0.114);p tr[4 * i + 1] = grayscale;ptr[4 * i + 2] = grayscale;ptr[4 * i + 0] = grayscale;} int size = W * H;jintarray result = Env->newintarray (size); Env->setintarrayregion (result, 0, size, cbuf); env->re Leaseintarrayelements (buf, cbuf, 0); return result;}
in the project to add
OpenCV header file Check , you can avoid file error.
6. Modify Mainactivity
The function that invokes JNI.
Package Com.example.hellomyjni;import Org.opencv.android.opencvloader;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.bitmap.config;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.ImageView;import Android.widget.textview;public class Mainactivity extends Activity{private ImageView iv;private Bitmap bmp;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); TextView TV = (TextView) Findviewbyid (R.id.text_view); Tv.settext (Jniclient.sayname ()); Recall Jniiv = (ImageView) Findviewbyid (r.id.image_view); bmp = Bitmapfactory.decoderesource (Getresources (), r.drawable.shoes); int w = Bmp.getwidth (); int h = bmp.getheight (); int[] pixels = new INT[W * h];bmp.getpixels (pixels, 0, W, 0, 0, W, h); int[] ResultInt = jniclient.grayimage (pixels, W, h); Recall Jnibitmap resultimg = Bitmap.createbitmap(W, H, config.argb_8888); Resultimg.setpixels (ResultInt, 0, W, 0, 0, W, h); Iv.setimagebitmap (resultimg);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} static {if (! Opencvloader.initdebug ()) {} else {system.loadlibrary ("Hellomyjni"); }}}
7. Output
Android-jni static (statically) loading OPENCV