如何使用命令行在 Linux 下使用 Java 构建 OpenCV?(将在 MapReduce 中使用它)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17386551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:21:36  来源:igfitidea点击:

How to build OpenCV with Java under Linux using command line?(Gonna use it in MapReduce)

javalinuxopencvbuildmapreduce

提问by user2535650

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.

最近我正在为我的毕业项目尝试 OpenCV。我在 Windows 环境下取得了一些成功。而且因为使用 OpenCV 的 Windows 包,它带有预构建的库,所以我不必担心如何构建它们。但是由于该项目假设运行在以 CentOS 作为每个节点的主机操作系统的集群上,我必须知道如何在 Linux 环境下正确编译和运行这些库。

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

我已经使用 VirtualBox 设置了一个 VM 并在其上安装了 Ubuntu 13.04。但是到目前为止,我仍然找不到调整 CMakeList 设置以使 Java 成为构建目标的方法。(一个 jar 和一个本地库,以便我可以在 MapReduce 程序中导入和使用它们)以下是 OpenCV 的桌面 Java 官方教程

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

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?

关于编译的部分非常简短。所以我还是不太明白。是因为我错过了对 Java 的一些依赖吗?我已经安装了 JVM(Jre-7u7)。还是因为我没有正确配置 CMakeList.txt?

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

这是我到目前为止所做的事情,除此之外它是一个完全干净的 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
  1. 安装 vim
  2. 已安装 g++
  3. 安装 cmake
  4. 安装 cmake-curses-gui
  5. 安装java7 JVM
  6. 下载适用于 Linux 的 OpenCV 包

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

目标环境是Cluster,每个节点的宿主OS是CentOS,项目是一个MapReduce程序。

Thanks in advance.

提前致谢。

采纳答案by GregHNZ

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

可能您缺少的是ant,这是一个面向 Java 的构建工具。

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):

(即您为机器配置 make 的位置),检查显示java的部分。它应该是这样的(可能有不同的路径/版本):

--   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:

当我第一次运行 cmake 时,我有这个:

--   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:

这表明它找不到ant,因此它没有创建 .jar 文件。我只是使用存储库中的版本:

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!).

我再次运行 cmake,使用上述选项,它获得了 ant 的路径(我免费获得了 JNI!)。

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.

注意:您可能希望相当仔细地阅读 cmake 的输出,因为它告诉您它将在您的机器上构建什么,并且您可能缺少一些库。

If you find JNI is still missing.

如果您发现 JNI 仍然缺失。

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

cmake是(主要是?第一?方便?可以解释为?)寻找jni.h,应该是具体的$JAVA_HOME/include/jni.h所以你需要把$JAVA_HOME设置为你jdk的home文件夹。

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

我使用which javac然后ls -l跟踪一系列符号链接,以确定我的 java 1.7 安装在哪里。您还可以使用locate jni.h并处理树,或者只是环顾四周/usr/lib/jvm或类似。

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:

有趣的是,当我再次运行 cmake 命令时,它选择了一个不同的 JNI 文件夹:

--     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 makecommand. 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.jaras that's the version of the code I downloaded).

一旦你得到了,运行make命令。如果你痴迷地观看,你会看到一堆 .java 文件滚过去。无论哪种方式,您最终都会在 ./bin 文件夹中得到一个 .jar 文件。(在我的情况下,它被称为opencv-245.jar我下载的代码版本)。

回答by UtsavDrolia

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

如果您的 JRE 在运行 Java 教程后崩溃,那么很可能您也安装了 python 绑定 (cv2.so) 。您将不得不在没有这些绑定的情况下重新制作 OpenCV

cmake -DBUILD_SHARED_LIBS=OFF -D BUILD_NEW_PYTHON_SUPPORT=NO

cmake -DBUILD_SHARED_LIBS=OFF -D BUILD_NEW_PYTHON_SUPPORT=NO

That solved the problem for me.

那为我解决了问题。

回答by Marco

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

我尝试在 Ubuntu 14 上构建 OpenCV 3 并遇到了与 Oracle java 8 JDK 类似的问题。

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

这些是我使用 java OpenCV 绑定构建和运行 java 程序的步骤。

  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.cmakein 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_DIRECTORIESvariable 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_DIRECTORIESvariable 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 builddirectory 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. 查找JNI。由于 cmake 3 的“FindJNI”模块不支持检测 Oracle Java 8,我们必须手动添加我们的 Java 8 目录。因此,FindJNI.cmake在您的 cmake 目录中找到该文件,例如/home/foo/bar/cmake-3.2.2-Linux-x86_64/share/cmake-3.2/Modules/FindJNI.cmake

    转到JAVA_APPEND_LIBRARY_DIRECTORIES变量并将路径添加到您的 java lib 架构目录,例如/home/foo/bar/jdk1.8.0_45/lib/amd64

    转到JAVA_AWT_INCLUDE_DIRECTORIES变量并将路径添加到您的 java 包含目录,例如/home/foo/bar/jdk1.8.0_45/include

  2. 建造。如果您修改的 FindJNI 找到了您的 Oracle 8 JDK,则共享库标志会触发 java 绑定的构建。

    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. 编写测试代码。在您的build目录中,您应该会看到一个 jar 文件,例如./bin/opencv-300.jar. 该文件可以添加到您的 java 构建依赖项中,例如在您最喜欢的 IDE 中。

  4. 运行测试代码。您必须将 opencv lib 路径添加到您的 VM 选项中:

    -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/

[1] 注意:根据您的个人需求或品味,您可以在构建之前安装各种其他依赖项,例如比较http://rodrigoberriel.com/2014/10/installing-opencv-3-0-0-on-ubuntu-14 -04/

回答by Kerry

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.

从 OpenCV 3.1.0 版本开始,在OpenCV_Root_Directory/platforms/maven. Maven 安装在你的 Linux 机器上(使用包管理器或手动安装),这个项目将确保你安装了正确的依赖项,构建本机和 Java 库。请参阅上述目录中的 README。