javah
当我在 java 类中编译本机方法以生成 JNI 头文件时,我只是遇到了一个恼人的 jni 问题。
如果该类使用了 3rd-party 包,例如:org.opencv.core.Mat,javah
则会显示找不到 org.opencv.core.Mat 类的错误。
OpenCV 示例代码如下:
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);
}
首先,我使用了命令
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
然后我将命令修改为
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 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)
我认为,在其中添加 opencv lib 的目录-bootclasspath
是有用且必要的。错误是因为我刚刚添加了两个路径-bootclasspath
或者格式有问题?
真的很迷茫。请提供一些帮助,谢谢!