Ask Your Question
9

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

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

emchristiansen gravatar image

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

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

edit retag flag offensive close merge delete

Comments

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 -0600 )edit

Thank you :)

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

I think a ticket on code.opencv.org 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 -0600 )edit

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

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

I've got a pull request underway: https://github.com/Itseez/opencv/pull/31

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

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 -0600 )edit

Hi emchristiansen,

I'v been waiting years for this ... Excellent(TM)* Can you create a wiki page and/or help tutorial for this https://github.com/Itseez/opencv/pull/31?

Another way to set LD_LIBRARY_PATH is as in http://blog.cedarsoft.com/2010/11/setting-java-library-path-programmatically/ .

Thanks and cheers, Petr

  • by Charles Montgomery "Monty" Burns
Petr gravatar imagePetr ( 2012-09-23 08:13:58 -0600 )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 -0600 )edit

Hi guys! I tried to compile the OpenCV source (from https://github.com/Itseez/opencv/) 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:

BUILD_opencv_java

"

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 -0600 )edit
1

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 -0600 )edit

Care to share your steps to compile it?

I used Cygwin to compile it on Windows 7. CMake just didn't recognize the command BUILD_opencv_java....

Tommy gravatar imageTommy ( 2012-11-08 09:37:35 -0600 )edit

Just an update: with the Visual Studio compiler everything works also under Windows 7 x64!

Tommy, I didn't use Cygwin. In CMake (gui) the BUILD_opencv_java flag is present if you correctly link your JDK path (you have to set the JAVA_HOME environment variable first).

luigidr gravatar imageluigidr ( 2012-11-26 06:51:44 -0600 )edit

Man, this drives me crazy! I do not have Visual Studio, but I tried to compile it on Ubuntu 12.10 as well, but doesn't work! I am obviously doing something very wrong..

It says that java is among the unavailable OpenCV modules:

Unavailable: androidcamera java ocl python

And finally that:

Manually-specified variables were not used by the project: BUILD_opencv_java

I've set my JAVA_HOME to "/usr/lib/jvm/java-7-openjdk-amd64/jre", where my java runtime environment is.

And I used the source from: https://github.com/Itseez/opencv/archive/master.zip

Anyone has any ideas about what needs to be done to get it to compile the java sources for me?

Tommy gravatar imageTommy ( 2012-11-27 07:03:07 -0600 )edit

Have you tried to set your JAVA_HOME to your JDK and not to your JRE?

luigidr gravatar imageluigidr ( 2012-11-27 10:54:37 -0600 )edit

Yes, I have tried all possible paths ;-/ This really makes no sense to me.

Did you do anything special before running cmake?

What does your JAVA_HOME looks like?

Tommy gravatar imageTommy ( 2012-11-30 05:16:04 -0600 )edit

Under Windows my JAVA_HOME is set to C:\Program Files\Java\jdk1.7.0_05

luigidr gravatar imageluigidr ( 2012-12-12 05:43:21 -0600 )edit

Yeah, still doesn't work... It doesn't say anywhere that it cant find java, only that it is unavailable. Do you happen to know where in the build process it looks for the java compiler? I can't find it anywhere.

Tommy gravatar imageTommy ( 2012-12-12 08:46:00 -0600 )edit

Very much looking forward to this, thanks a lot for your work! I am a bit unconfortable with the current status of JavaCV being a separate project than OpenCV, with manually-created interfaces.

Do you have plans to create a tutorial in order to enable this experimental support? It would also be good to know what the OpenCV devs think about this port, apart from giving it low priority. I am about to start a new project in Java, and I am not sure which way to go: the experimental but integrated (maybe more future-looking), or the separate but manual (safer for now, but maybe worse for the long term).

Jose Gómez gravatar imageJose Gómez ( 2012-12-15 12:21:15 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

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

emchristiansen gravatar image

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

Andrey Pavlenko gravatar image

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

edit flag offensive delete link more

Question Tools

5 followers

Stats

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

Seen: 5,494 times

Last updated: Mar 21 '13