Ask Your Question

Wrapping OpenCV with desktop Java: Building for 64-bit x86

asked 2012-09-06 22:02:03 -0500

emchristiansen gravatar image

updated 2012-09-12 11:35:12 -0500

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/”. 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, 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 ... (more)

edit retag flag offensive close merge delete


Hi, just to let you know I find your post and the work you are doing interesting :)

Rui Marques gravatar imageRui Marques ( 2012-09-07 04:57:20 -0500 )edit

Thank you :)

emchristiansen gravatar imageemchristiansen ( 2012-09-07 12:33:09 -0500 )edit

I think a ticket on would be a better place for this discussion. You really need to talk to OpenCV developers, not users. Please create a ticket, and I'll try to ask guys to help you :)

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-09-12 10:46:38 -0500 )edit

Additionally to filing a feature request on you could create a wiki page to document the progress.

Philipp Wagner gravatar imagePhilipp Wagner ( 2012-09-12 12:05:42 -0500 )edit

I've got a pull request underway:

emchristiansen gravatar imageemchristiansen ( 2012-09-14 01:29:29 -0500 )edit

Merged with corrections.

Use "-DBUILD_opencv_java=ON" argument to CMake to enable Java API for desktop.

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2012-09-18 01:56:01 -0500 )edit

Hi emchristiansen,

I'v been waiting years for this ... Excellent(TM)* Can you create a wiki page and/or help tutorial for this

Another way to set LD_LIBRARY_PATH is as in .

Thanks and cheers, Petr

  • by Charles Montgomery "Monty" Burns
Petr gravatar imagePetr ( 2012-09-23 08:13:58 -0500 )edit

This is great! I've also been waiting for this for years!

Though, I'm not good with compilation.. anyone care to give the steps required to compile these Java bindings for e.g. Ubuntu?

Tommy gravatar imageTommy ( 2012-09-24 09:32:02 -0500 )edit

Hi guys! I tried to compile the OpenCV source (from and including the Java bindings by invoking: "cmake -DBUILD_opencv_java=ON". However, I get the following message at the end of cmake:

"CMake Warning: Manually-specified variables were not used by the project:



Is this parameter only used in: "OpenCVLegacyOptions.cmake"? In this file it performs: "ocv_legacy_option(BUILD_JAVA_SUPPORT BUILD_opencv_java)"

This is the only mention of "BUILD_opencv_java" in any file. There is a use of "BUILD_opencv_java_INIT", anyone knows what this is?

Thank you in advance!

Tommy gravatar imageTommy ( 2012-09-25 08:33:53 -0500 )edit

Hi! Thank you for your work: I've also been waiting for this for years!

I got all working on Mac OS X and Ubuntu x64, also with the "new" 2.4.3 version.

On Windows 7 x64 I don't have any problem during the compilation but... it doesn't work (the JNI-related error is "Can't find dependent libraries"). I suppose that the problem is the compiler: I'm using mingw32...

Just to let you know!

Thank you again!

luigidr gravatar imageluigidr ( 2012-11-03 12:17:01 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2013-03-18 12:17:11 -0500

emchristiansen gravatar image

updated 2013-03-21 03:39:00 -0500

Andrey Pavlenko gravatar image

In case it's not clear from the comments, the wrapper is now in OpenCV:

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2012-09-06 22:02:03 -0500

Seen: 3,293 times

Last updated: Mar 21 '13