In this blog post I'll describe what you have to do in order to build OpenCV 3.1 with Java Support on a Mac with El Capitan.
(If you don't want to do source builds and you don't have all necessary tools installed, there are also some alternatives like brew or macports available. In fact, I blogged about using this approach some years ago.)
First, you will have to download the sources directly from the official opencv.org site. It is a rather large source distribution, so the download may take a while.
Unpack the source ball into a directory, I did it like this:
~/opencv/ (1)
~/opencv/opencv-3.1.0/ (2)
I created a directory named "build"
~/opencv/build/ (3)
(1) ... a directory which includes all opencv versions, so you have everything in place if you want to follow more than one version and you don't use git for doing this
(2) ... the sources like you've downloaded them
(3) ... the build directory which will be cluttered up by cmake with all kinds of stuff
Change to the directory (3) and issue following command:
cmake ../opencv-3.1.0/
This command will investigate what is already installed on your system. It will do its best to download stuff it will need to compile what is possible. Certain tools or libraries you still need to download manually, though. This command will finish in a minute and end with a thorough report of how the build is configured. Check this list and see if your feature is configured aswell. To my surprise and as opposed to the last try with version 2.4.6 the java bindings are activated per default which is great. Huge thanks for the build guys of this library.
The cmake command is setup in a way that in theory you just have to invoke another command, named
make
which will invoke by itself all programs which contribute to the final build artifacts. This means it will invoke c++ compilers, c compilers, helper programs and whatnot. This command will also take some time to complete.
After waiting for some minutes the command exits successfully and in the bin folder there is a jar file which contains all bindings to the opencv library. This API is then the entrance for using the excellent library from your Java applications.
After having compiled opencv like described above, several directories and files had been created. Amongst those, in the 'bin' folder, there is a file called
opencv-310.jar
This jar file can be installed in the local maven repository like this:
mvn install:install-file
-Dfile=opencv-310.jar
-DgroupId=org.opencv
-DartifactId=opencv-java
-Dversion=3.1.0
-Dpackaging=jar
From now on you should be able to reference the library with maven in the pom.xml like this:
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv-java</artifactId>
<version>3.1.0</version>
</dependency>
Still, in order to get OpenCV to work, you'll need the native part of the library which contains "the real thing" - the Java code is just a thin wrapper which makes it more convenient to call the native code. Those files are generated in the "lib" folder:
libopencv_objdetect.3.1.0.dylib
libopencv_calib3d.3.1.0.dylib
libopencv_objdetect.3.1.dylib
libopencv_calib3d.3.1.dylib
...
This should help you start developing against the OpenCV API with Java and MacOsX.
It may happen however, like mentioned, that you will need to install additional software packages such that the compilation step of OpenCV is successful for you.
(If you don't want to do source builds and you don't have all necessary tools installed, there are also some alternatives like brew or macports available. In fact, I blogged about using this approach some years ago.)
Compiling OpenCV 3.1
Unpack the source ball into a directory, I did it like this:
~/opencv/ (1)
~/opencv/opencv-3.1.0/ (2)
I created a directory named "build"
~/opencv/build/ (3)
(1) ... a directory which includes all opencv versions, so you have everything in place if you want to follow more than one version and you don't use git for doing this
(2) ... the sources like you've downloaded them
(3) ... the build directory which will be cluttered up by cmake with all kinds of stuff
Change to the directory (3) and issue following command:
cmake ../opencv-3.1.0/
This command will investigate what is already installed on your system. It will do its best to download stuff it will need to compile what is possible. Certain tools or libraries you still need to download manually, though. This command will finish in a minute and end with a thorough report of how the build is configured. Check this list and see if your feature is configured aswell. To my surprise and as opposed to the last try with version 2.4.6 the java bindings are activated per default which is great. Huge thanks for the build guys of this library.
The cmake command is setup in a way that in theory you just have to invoke another command, named
make
which will invoke by itself all programs which contribute to the final build artifacts. This means it will invoke c++ compilers, c compilers, helper programs and whatnot. This command will also take some time to complete.
After waiting for some minutes the command exits successfully and in the bin folder there is a jar file which contains all bindings to the opencv library. This API is then the entrance for using the excellent library from your Java applications.
Using OpenCV 3.1.0 with Maven
After having compiled opencv like described above, several directories and files had been created. Amongst those, in the 'bin' folder, there is a file called
opencv-310.jar
This jar file can be installed in the local maven repository like this:
mvn install:install-file
-Dfile=opencv-310.jar
-DgroupId=org.opencv
-DartifactId=opencv-java
-Dversion=3.1.0
-Dpackaging=jar
From now on you should be able to reference the library with maven in the pom.xml like this:
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv-java</artifactId>
<version>3.1.0</version>
</dependency>
Still, in order to get OpenCV to work, you'll need the native part of the library which contains "the real thing" - the Java code is just a thin wrapper which makes it more convenient to call the native code. Those files are generated in the "lib" folder:
libopencv_objdetect.3.1.0.dylib
libopencv_calib3d.3.1.0.dylib
libopencv_objdetect.3.1.dylib
libopencv_calib3d.3.1.dylib
...
One of them is called "libopencv_java310.so" - this library contains the native code for the api which is accessible through the jar file. Make sure this library is loaded before you call the first time into the jar file via
System.load(new File("/path/to/libopencv_java310.so"))
This should help you start developing against the OpenCV API with Java and MacOsX.
It may happen however, like mentioned, that you will need to install additional software packages such that the compilation step of OpenCV is successful for you.
Thanks for your post. I succeeded in building the dylibs, but the libopencv_java310.so is missing. Any idea where this can be found?
ReplyDeleteFind the problem myself. It turns out ANT is needed. If ANT cannot be found, the libopencv_java310.so will not be generated.
Delete