1.HelloJni.java
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText( stringFromJNI() );
setContentView(tv);
}
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
static {
System.loadLibrary("opencv_java");
System.loadLibrary("hello-jni");
}
}
2.hello-jni.c
#include <string.h>
#include <jni.h>
#include "opencv/cv.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
CvCapture* capture = 0;
return (*env)->NewStringUTF(env, "Hello from JNI _opencv!");
}
3. Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include /root/OpenCV_android/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libopencv_java
LOCAL_SRC_FILES := libopencv_java.so
include $(PREBUILT_SHARED_LIBRARY)
4. cp /root/OpenCV_android/sdk/native/libs/armeabi/libopencv_java.so 到项目jni目录,即
# ll
总用量 7936
-rw-r--r--. 1 root root 932 10月 6 04:02 Android.mk
-rw-r--r--. 1 root root 1264 10月 6 04:10 hello-jni.c
-rw-------. 1 root root 8116660 10月 6 04:04 libopencv_java.so
5. build project
....
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
Install : libopencv_java.so => libs/armeabi/libopencv_java.so
6. Run
显示Hello from JNI _opencv!