I just have an annoying problem with jni when i compile the native method in java class with javah
to generate JNI header files.
If the class has used 3rd-party package, For example: org.opencv.core.Mat, then the javah
will show the error that can't find the org.opencv.core.Mat class.
The OpenCV sample code as below:
package org.opencv.samples.fd;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
public class DetectionBasedTracker
{
public DetectionBasedTracker(String cascadeName, int minFaceSize) {
mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
}
public void start() {
nativeStart(mNativeObj);
}
public void stop() {
nativeStop(mNativeObj);
}
public void setMinFaceSize(int size) {
nativeSetFaceSize(mNativeObj, size);
}
public void detect(Mat imageGray, MatOfRect faces) {
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
}
public void release() {
nativeDestroyObject(mNativeObj);
mNativeObj = 0;
}
private long mNativeObj = 0;
private static native long nativeCreateObject(String cascadeName, int minFaceSize);
private static native void nativeDestroyObject(long thiz);
private static native void nativeStart(long thiz);
private static native void nativeStop(long thiz);
private static native void nativeSetFaceSize(long thiz, int size);
private static native void nativeDetect(long thiz, long inputImage, long faces);
}
First, I used the command
javah -classpath bin/classes -bootclasspath (the directory of android.jar) -d jni (packageName + ClassName) , shows the error "can't find the org.opencv.core.Mat
Then I modified the command to
javah - classpath bin/classes - bootclasspath (the dir of android.jar) ; (the dir of the opencv lib jar) -d jni ..." ", this time it shows error
Exception
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class
name: E:\Computer_Language\Java\soft_android\OpenCV-2.4.3-rc-android-sdk\OpenCV
-2.4.3-rc-android-sdk\sdk\java\bin\opencv library - 2.4.3.jar
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:177)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:68)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:509)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:335)
at com.sun.tools.javah.Main.main(Main.java:46)
I think, adding the directory of opencv lib in -bootclasspath
is useful and neccessary. The error is because i just added two path in -bootclasspath
or the format is something wrong?
Really confused. Please give some help , thank u!
This is what I did:
1.Open Command line, type to the (project)/bin/classes: 2.type: javah -classpath (opencv4android sdk path)/java/bin/classes:(your project location)/bin/classes -jni (your java class file that contains the native library interfaces)
In my project. I did:
javah -classpath /home/zijun/Dev/adt/OpencvAndroid/sdk/java/bin/classes:/home/zijun/workspace/LocTM/bin/classes -jni com.brainport.loctm.TMatching
That works on Linux Ubuntu 12.04.02 64bit OS
I encountered the same problem too,it cost me half day.I just copy DetectionBasedTracker.java to my poject.I use Android Studio.When I use ExternalTools>javah to generate .h files,the console print cant find org.opencv.core.Mat.copying mat.java & matofRect.java to the directory DetectionBasedTracker.java exists just cause more java class cant be find.finally,I find this problem was caused by the import * clause in java file.and we seldom use java class in native method we defined.so I cut these java method and got this:
package cn.ntu.tonguefur.nativemethod;
public class DetectionBasedTracker{
private static native long nativeCreateObject(String cascadeName,int minFaceSize);
//Other native methods
private static native void nativeDetect(long thiz,long inputImage,long faces);
}
Now you can freely use javah command to generate .h file.After that,add java methods and import packages you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With