Android, generate jni Header Files with javah, show an error that org.opencv.core.Mat cannot find

I just have a nasty problem with jni when I compile my own method in a java class with javah to create JNI header files.

If the class used a third-party package, for example: org.opencv.core.Mat, then javah will show an error that the org.opencv.core.Mat class cannot find.

Sample OpenCV code as shown 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); } 

I used the command first

 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 changed 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 opencv lib directory to -bootclasspath is useful and necessary. The error is that I just added two paths to -bootclasspath or is there something wrong with the format?

Really confused. Please help, thanks u!

+6
source share
2 answers

This is what I did:

1. Open a command prompt, enter in the project (project) / bin / classes: 2.type: javah -classpath (path opencv4android sdk) / java / bin / classes: (location of your project) / bin / classes -jni (your file java class, which contains the interfaces of the native library)

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

It works on Linux Ubuntu 04/12/02 64-bit OS

+6
source

I also faced the same problem, it cost me half a day. I just copy DetectionBasedTracker.java to my project. I am using Android Studio. When I use ExternalTools> javah to generate .h files, the print can not find org console. opencv.core.Mat.copying mat.java and matofRect.java to the DetectionBasedTracker.java directory exists only because it can no longer find the java class. Finally, I find that this problem was caused by the import * clause in the java file. And we rarely use the java class in our own method, which we defined. I cut this java method and got the following:

 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 the javah command to generate .h file.After, add java methods and import the packages you need.

+1
source

All Articles