How to build OpenCV with Java under Linux using co

2020-05-23 05:19发布

Recently I'm trying OpenCV out for my graduation project. I've had some success under Windows enviroment. And because with Windows package of OpenCV it comes with pre-built libraries, so I don't have to worry about how to build them. But since the project is suppose to run on a cluster with CentOS as host OS for each node, I have to know how to correctly compile, and run these library under Linux enviroment.

I've set up a VM with VirtualBox and installed Ubuntu 13.04 on it. But so far I still can't find a way to adjust the settings of CMakeList to make Java a build target. (A jar, and a native library so I can import and use them in MapReduce program) Following is the official tutorial of desktop Java for OpenCV

http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html

The part about the compiling is pretty brief. So I still can't quite understand it. Is it because I missed some dependency for Java? I've already got JVM(Jre-7u7) installed. Or is it because I didn't configure CMakeList.txt correctly?

Here is the things I've done so far, other than that it's a complete clean Ubuntu

  1. Installed vim
  2. Installed g++
  3. Installed cmake
  4. installed cmake-curses-gui
  5. installed java7 JVM
  6. download OpenCV package for Linux

The target enviroment is Cluster with CentOS as host OS for each node, and the project is a MapReduce program.

Thanks in advance.

4条回答
来,给爷笑一个
2楼-- · 2020-05-23 05:20

For someone directed here from google :

If your JRE crashes after you run the java tutorials then most probably you have the python Bindings installed as well (cv2.so) . You will have to re-make OpenCV without those bindings

cmake -DBUILD_SHARED_LIBS=OFF -D BUILD_NEW_PYTHON_SUPPORT=NO

That solved the problem for me.

查看更多
放我归山
3楼-- · 2020-05-23 05:21

Probably what you're missing is ant, which is a java oriented build tool.

When you go:

cmake -D BUILD_SHARED_LIBS=OFF ../opencv-2.4.5/

(i.e. where you're configuring the make for your machine), check for the section that says java. It should say something like this (possibly with different paths/versions):

--   Java:
--     ant:                         /usr/bin/ant (ver 1.8.2)
--     JNI:                         /usr/lib/jvm/java-6-openjdk/include /usr/lib/jvm/java-6-openjdk/include /usr/lib/jvm/java-6-openjdk/include
--     Java tests:                  YES

When I first ran cmake, I had this:

--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java tests:                  YES

Which indicated it couldn't find ant, and so it didn't create a .jar file. I simply use the version that's in the repository:

sudo apt-get install ant

I ran cmake again, with the above options, which got the path to ant (and I got JNI for free!).

Note: You probably want to read the output of cmake fairly carefully as it tells you what it's going to build on your machine, and you may have some missing libraries.

If you find JNI is still missing.

cmake is (mostly? first? conveniently? can be interpreted as?) looking for jni.h, which should be specifically $JAVA_HOME/include/jni.h So you need to set $JAVA_HOME to the home folder of your jdk.

I used which javac and then ls -l to follow a series of symbolic links, to identify where my java 1.7 install was. You could also use locate jni.h and work up the tree, or simply look around /usr/lib/jvm or similar.

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

You probably want to check that you've got that right with a couple of

ls $JAVA_HOME/bin/javac
/usr/lib/jvm/java-7-openjdk-amd64/bin/javac

ls $JAVA_HOME/include/jni.h
/usr/lib/jvm/java-7-openjdk-amd64/include/jni.h

Interestingly when I ran the cmake command again, it picked up a different JNI folder:

--     JNI:                         /usr/lib/jvm/java-7-openjdk-amd64/include /usr/lib/jvm/java-7-openjdk-amd64/include /usr/lib/jvm/java-7-openjdk-amd64/include

Once you've got that, run the make command. If you watch obsessively, you'll see a bunch of .java files trundle past. Either way you'll end up with a .jar file in the ./bin folder. (in my case it's called opencv-245.jar as that's the version of the code I downloaded).

查看更多
Lonely孤独者°
4楼-- · 2020-05-23 05:36

I tried to build OpenCV 3 on Ubuntu 14 and encountered similar problems with Oracle java 8 JDK.

These are the steps I preformed to build and run a java program using the java OpenCV bindings.

  1. FindJNI. Since the "FindJNI" module of cmake 3 doesn't support the detection of Oracle Java 8, we have to add our Java 8 directories manually. Therefore, locate the file FindJNI.cmake in your cmake directory,e.g /home/foo/bar/cmake-3.2.2-Linux-x86_64/share/cmake-3.2/Modules/FindJNI.cmake

    Goto the JAVA_APPEND_LIBRARY_DIRECTORIES variable and add the path to your java lib architecture directory, e.g. /home/foo/bar/jdk1.8.0_45/lib/amd64

    Goto the JAVA_AWT_INCLUDE_DIRECTORIES variable and add the path to your java include directory, e.g. /home/foo/bar/jdk1.8.0_45/include

  2. Build. The shared lib flag tiggers the build of the java bindings, if your modified FindJNI found your Oracle 8 JDK.

    cd /home/foo/bar/opencv-3.0.0/
    mkdir build
    cd build
    # install dependencies if needed, see [1]
    cmake -DBUILD_SHARED_LIBS=OFF ..
    make -j
    
  3. Write Testcode. In your build directory you should see a jar file like ./bin/opencv-300.jar. This file can be added to your java build dependencies, e.g. in your favorite IDE.

  4. Run Testcode. You have to add the opencv lib path to your VM options:

    -Djava.library.path=/home/foo/bar/opencv-3.0.0/build/lib

[1] Note: Depending on your personal needs or taste you can install various other dependencies before building, e.g. compare http://rodrigoberriel.com/2014/10/installing-opencv-3-0-0-on-ubuntu-14-04/

查看更多
▲ chillily
5楼-- · 2020-05-23 05:37

Since version 3.1.0 of OpenCV there is a Maven POM project under OpenCV_Root_Directory/platforms/maven. With Maven installed on your Linux machine (using package manager or manually installed) this project will ensure you have the correct dependencies installed, build the native and Java libraries. See the README in the above directory.

查看更多
登录 后发表回答