JNI 问题记录如下:
05-09 01:36:35.029: I/dalvikvm-heap(766): Grow heap (frag case) to 8.971MB for 1228816-byte allocation
05-09 01:36:35.368: D/dalvikvm(766): GC_CONCURRENT freed 1K, 4% free 9116K/9479K, paused 6ms+5ms
05-09 01:36:37.149: W/dalvikvm(766): No implementation found for native Lorg/opencv/samples/tutorial2/JNInterface;.matchFingers (Ljava/lang/String;)Ljava/lang/String;
05-09 01:36:37.149: D/AndroidRuntime(766): Shutting down VM
05-09 01:36:37.149: W/dalvikvm(766): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-09 01:36:37.178: E/AndroidRuntime(766): FATAL EXCEPTION: main
05-09 01:36:37.178: E/AndroidRuntime(766): java.lang.UnsatisfiedLinkError: matchFingers
05-09 01:36:37.178: E/AndroidRuntime(766): at org.opencv.samples.tutorial2.JNInterface.matchFingers(Native Method)
05-09 01:36:37.178: E/AndroidRuntime(766): at org.opencv.samples.tutorial2.ItemDetailFragment3$buttonMatchFingerslistener.onClick(ItemDetailFragment3.java:207)
05-09 01:36:37.178: E/AndroidRuntime(766): at android.view.View.performClick(View.java:3511)
05-09 01:36:37.178: E/AndroidRuntime(766): at android.view.View$PerformClick.run(View.java:14105)
05-09 01:36:37.178: E/AndroidRuntime(766): at android.os.Handler.handleCallback(Handler.java:605)
05-09 01:36:37.178: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 01:36:37.178: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137)
05-09 01:36:37.178: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-09 01:36:37.178: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 01:36:37.178: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 01:36:37.178: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-09 01:36:37.178: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-09 01:36:37.178: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method)
05-09 01:36:37.759: I/dalvikvm(766): threadid=3: reacting to signal 3
05-09 01:36:37.819: I/dalvikvm(766): Wrote stack traces to '/data/anr/traces.txt'
问题显然是没有找到相应的本地函数。解决办法也很简单,就是Java代码,.h文件和.cpp文件中的声明,肯定有一个不对应的。
我的这个问题是JNI回调函数的头文件由Javah生成,第二个参数是Jobject,而我在cpp中一般使用Jclass,在头文件中改过来即可。
//Java_org_opencv_samples_tutorial2_JNInterface.h
//JNIEXPORT jstring JNICALL Java_org_opencv_samples_tutorial2_JNInterface_matchFingers
// (JNIEnv *, jobject, jstring);
//改为JNIEXPORT jstring JNICALL Java_org_opencv_samples_tutorial2_JNInterface_matchFingers (JNIEnv *, jclass, jstring);
//总之要跟Java_org_opencv_samples_tutorial2_JNInterface.cpp中的实现完全一致。