简体   繁体   English

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

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

Recently I'm trying OpenCV out for my graduation project.最近我正在为我的毕业项目尝试 OpenCV。 I've had some success under Windows enviroment.我在 Windows 环境下取得了一些成功。 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.并且因为 OpenCV 的 Windows 包附带了预构建的库,所以我不必担心如何构建它们。 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.但是由于该项目假设运行在以 CentOS 作为每个节点的主机操作系统的集群上,我必须知道如何正确编译,并在 Linux 环境下运行这些库。

I've set up a VM with VirtualBox and installed Ubuntu 13.04 on it.我已经使用 VirtualBox 设置了一个 VM,并在其上安装了 Ubuntu 13.04。 But so far I still can't find a way to adjust the settings of CMakeList to make Java a build target.但到目前为止,我仍然找不到调整 CMakeList 设置以使 Java 成为构建目标的方法。 (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 (一个 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?是因为我错过了对 Java 的一些依赖吗? I've already got JVM(Jre-7u7) installed.我已经安装了 JVM(Jre-7u7)。 Or is it because I didn't configure CMakeList.txt correctly?还是因为我没有正确配置 CMakeList.txt?

Here is the things I've done so far, other than that it's a complete clean Ubuntu这是我到目前为止所做的事情,除了它是一个完全干净的 Ubuntu

  1. Installed vim安装的vim
  2. Installed g++安装 g++
  3. Installed cmake安装 cmake
  4. installed cmake-curses-gui安装 cmake-curses-gui
  5. installed java7 JVM安装java7 JVM
  6. download OpenCV package for Linux下载适用于 Linux 的 OpenCV 包

The target enviroment is Cluster with CentOS as host OS for each node, and the project is a MapReduce program.目标环境是CentOS作为每个节点的主机操作系统的集群,项目是一个MapReduce程序。

Thanks in advance.提前致谢。

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/

(ie where you're configuring the make for your machine), check for the section that says java . (即,您正在为您的机器配置 make 的位置),检查显示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:当我第一次运行 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.这表明它找不到ant ,因此它没有创建 .jar 文件。 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!).我再次运行 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.h So you need to set $JAVA_HOME to the home folder of your jdk. cmake 是(主要是?首先?方便?可以解释为?)寻找 jni.h,具体应该是$JAVA_HOME/include/jni.h所以你需要将 $JAVA_HOME 设置为你的 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.我使用which javac然后ls -l跟踪一系列符号链接,以确定我的 java 1.7 安装在哪里。 You could also use locate jni.h and work up the tree, or simply look around /usr/lib/jvm or similar.您也可以使用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 make command.完成后,运行make命令。 If you watch obsessively, you'll see a bunch of .java files trundle past.如果您着迷地观看,您会看到一堆 .java 文件滚过。 Either way you'll end up with a .jar file in the ./bin folder.无论哪种方式,您最终都会在 ./bin 文件夹中得到一个 .jar 文件。 (in my case it's called opencv-245.jar as that's the version of the code I downloaded). (在我的情况下,它被称为opencv-245.jar ,因为这是我下载的代码版本)。

For someone directed here from google :对于从 google 指向这里的人:

If your JRE crashes after you run the java tutorials then most probably you have the python Bindings installed as well (cv2.so) .如果您在运行 java 教程后 JRE 崩溃,那么很可能您也安装了 python 绑定 (cv2.so)。 You will have to re-make OpenCV without those bindings您将不得不在没有这些绑定的情况下重新制作 OpenCV

cmake -DBUILD_SHARED_LIBS=OFF -D BUILD_NEW_PYTHON_SUPPORT=NO

That solved the problem for me.这为我解决了问题。

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.查找JNI。 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.由于 cmake 3 的“FindJNI”模块不支持检测 Oracle Java 8,我们必须手动添加我们的 Java 8 目录。 Therefore, locate the file FindJNI.cmake in your cmake directory,eg /home/foo/bar/cmake-3.2.2-Linux-x86_64/share/cmake-3.2/Modules/FindJNI.cmake因此,在您的 cmake 目录中找到文件FindJNI.cmake ,例如/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, eg /home/foo/bar/jdk1.8.0_45/lib/amd64转到JAVA_APPEND_LIBRARY_DIRECTORIES变量并将路径添加到您的 java lib 架构目录,例如/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, eg /home/foo/bar/jdk1.8.0_45/include转到JAVA_AWT_INCLUDE_DIRECTORIES变量并将路径添加到您的 java 包含目录,例如/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.如果您修改后的 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. Write Testcode.编写测试代码。 In your build directory you should see a jar file like ./bin/opencv-300.jar .在您的build目录中,您应该会看到一个类似./bin/opencv-300.jar的 jar 文件。 This file can be added to your java build dependencies, eg in your favorite IDE.该文件可以添加到您的 java 构建依赖项中,例如在您最喜欢的 IDE 中。

  4. Run Testcode.运行测试代码。 You have to add the opencv lib path to your VM options:您必须将 opencv lib 路径添加到您的 VM 选项中:

    -Djava.library.path=/home/foo/bar/opencv-3.0.0/build/lib -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, eg 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/

Since version 3.1.0 of OpenCV there is a Maven POM project under OpenCV_Root_Directory/platforms/maven .从 OpenCV 3.1.0 版本开始,在OpenCV_Root_Directory/platforms/maven下有一个 Maven POM 项目。 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.在您的 Linux 机器上安装 Maven(使用包管理器或手动安装)后,该项目将确保您安装了正确的依赖项,构建本机和 Java 库。 See the README in the above directory.请参阅上述目录中的 README。

-- Java: -- 爪哇:
-- ant: NO -- JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include -- Java wrappers: NO -- Java tests: NO -- ant: 否 -- JNI: /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include -- Java包装器:否——Java 测试:否

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM