I'm trying to extend the automatically generated Java/JNI wrappers for Android so they work on desktop Java. The ultimate goal is automatic wrapping for Java of all of OpenCV. This should target the same use-case as the manually wrapped JavaCV. Andrey Pavlenko at Itseez has been helping me (much of what I know is thanks to him), but in an effort to minimally pester him, I'm turning to the crowd. This discourse doesn't quite fit the Q/A format, but I'm not sure where else to put it. Let's call it an experiment in assisted development.
First, I'm going to explain what I understand of the problem. Then I'll explain my confusions and ask for help.
What I understand: The Android wrappers are automatically generated, and target the bare-bones JNI interface. To generate the wrappers, a custom C++ parser (!!!) processes the header files in the OpenCV include directories. The C++ parser is at “opencv/modules/python/src2/hdr_parser.py”. Note the source code can be downloaded from github. After parsing, the JNI glue files (they #include jni.h, see this example) and Javadocs are generated, using code in “opencv/modules/java/generator/”. As I understand (I’m new to JNI), the combination of interface .java files, JNI glue files, and the OpenCV source is all we’ll need; the JNI glue files will be compiled against the OpenCV source, producing one or several dynamic libraries (.so). To use the wrapper, we just: 1) Compile our application with the interface .java files, and 2) In our main code, make sure to dynamically load the .so(s) using System.loadLibrary before calling any interface functions.
To generate a Java/JNI wrapper for Android, we can follow the instructions on the wiki. To get the build targets in an Android SDK layout, run “make install” (not as root), which creates the “install” folder. The “install” folder will include a libopencv_java.so, but alas, if you followed the instructions, it won’t be built for a desktop architecture. Following the instructions builds the library using the Android NDK compiler, which only targets mobile platforms.
What I don’t understand: If you don’t pass it any options, a project generated using “opencv/CMakeLists.txt” won’t generate a Java interface. In fact, there’s no mention of Java in the file.
It appears the magic for generating the interface is in “opencv/android/android.toolchain.cmake”, and probably also the three files matching “grep -ri java opencv/cmake”. What I don’t understand is how to modify these files so that we use the system compiler to build for 64-bit x86, or whether modifying these files is even the best solution. This set of CMake scripts is pretty confusing to me, and while unaided I’ll eventually figure it out, I think this could be greatly sped along with the help of someone more familiar with what’s going on. Help, anyone?
Update: I was able to build a wrapper for desktop Java, basically by taking files generated for the Android build and manually modifying them. Now I just need to automate those steps and create a corresponding target in the build scripts.
answered 2013-03-18 12:17:11 -0500emchristiansen
In case it's not clear from the comments, the wrapper is now in OpenCV: http://docs.opencv.org/trunk/doc/tutorials/introduction/desktop_java/java_dev_intro.html
Asked: 2012-09-06 22:02:03 -0500
Seen: 2,411 times
Last updated: Mar 21 '13