Android-JNI static Loading OpenCV

Source: Internet
Author: User

Android-JNI static Loading OpenCV
JNI static Loading OpenCV


Address: http://blog.csdn.net/caroline_wendy


Steps:

1. Prepare the OpenCV-Android Library

Copy the OpenCV sdk to the working folder.

Download the latest android-sdk of OpenCV:OpenCV-2.4.9-android-sdk;

PutSdk folder,CopyTo the working directory. the sdk folder contains three folders: native, java, and etc.


2. Load the OpenCV-Android Library

Import)Java folder in the working directory,Set as the library of the current project.

[Project name] Right-click-> Android-> Add Library-> select the added Library.NoSelect Is Library.



3. Create JniClientClass.

IncludeMethod), YesMultiple. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48L3A + PHByZSBjbGFzcz0 = "brush: java;">/*****/package com. example. hellomyjni;/*** @ author Administrator **/public class JniClient {static public native String sayName (); static public native int [] grayImage (int [] pixels, int w, int h );}
Use the command line to generate the header file.

Go to the project folder and generate the JNI header file. Run the following command:

"Javah-classpath bin/classes-d jni com. example. hellomyjni. JniClient"

Or javah-classpath D:/eclipse-android/android-sdk/platforms/android-17/android. jar; bin/classes-d jni com. example. mycamera. CartoonifierView

Contains the Android library.

Command Parsing:

Generate a header file using javah;

-The classpath class position (bin/classes) is a. class file;

-D jni needs to generate 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 in the jni folderApplication. mk is mainly responsible for the use of C ++ standards (APP_STL) and libraries to be compiled.

APP_STL := gnustl_staticAPP_CPPFLAGS := -frtti -fexceptionsAPP_ABI := armeabi-v7a

Modify Android. mk. OPENCV_LIB_TYPE: = STATIC is not required.

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 
 
  #include 
  
   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 grayScale = (int) (ptr[4 * i + 2] * 0.299 + ptr[4 * i + 1] * 0.587+ ptr[4 * i + 0] * 0.114);ptr[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->ReleaseIntArrayElements(buf, cbuf, 0);return result;}
  
 

Add OpenCV header file checkTo avoid file errors.



6. Modify MainActivity

Call the JNI function.

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 present.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 AndroidManifest.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





Related Article

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.