java 如何在 IntelliJ IDEA 12 中使用 OpenCV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16412936/
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-10-31 22:52:58  来源:igfitidea点击:

How to use OpenCV with IntelliJ IDEA 12

javaopencvideintellij-idea

提问by Cadel Watson

I'm trying to use IntelliJ IDEA 12 to develop OpenCV 2.4.5 applications in Java. I've followed the instructions for Eclipse from the website here.

我正在尝试使用 IntelliJ IDEA 12 在 Java 中开发 OpenCV 2.4.5 应用程序。我已按照网站here 中的Eclipse 说明进行操作。

The problem I am running into is that I can add the jar to my library but I don't know how to add the natives to my classpath.

我遇到的问题是我可以将 jar 添加到我的库中,但我不知道如何将本机添加到我的类路径中。

回答by dlx.folmead1

In order to use native libraries in Java you need to specify java.library.path system property, so that JVM knows where to look for them.

为了在 Java 中使用本机库,您需要指定 java.library.path 系统属性,以便 JVM 知道在哪里查找它们。

In IntelliJ this be can be done in Run/Debug Configuration -> Application -> VM options, enter:

在 IntelliJ 中,这可以在运行/调试配置 -> 应用程序 -> VM 选项中完成,输入:

-Djava.library.path=path/to/dll

回答by Anthony Keane

  • Download OpenCV-2.4.5-android-sdk.zip from OpenCV site
  • Extract to where ever as OpenCV-2.4.5-android-sdk, mine happened to be

    /home/anthony/Documents/OpenCV-2.4.5-android-sdk/

  • Open IntelliJ and choose Import

  • Select the folder to import

    /home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/

     yours will be a little different, 
     don't worry, just chose where you 
     extracted OpenCV-2.4.5-android-sdk
    
  • Once the Import wizard finishes, build the app with menu

  • OpenCV 站点下载 OpenCV-2.4.5-android-sdk.zip
  • 提取到 OpenCV-2.4.5-android-sdk 的任何地方,我的恰好是

    /home/anthony/Documents/OpenCV-2.4.5-android-sdk/

  • 打开 IntelliJ 并选择导入

  • 选择要导入的文件夹

    /home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/

     yours will be a little different, 
     don't worry, just chose where you 
     extracted OpenCV-2.4.5-android-sdk
    
  • 导入向导完成后,使用菜单构建应用程序

Build -> Rebuild Project

构建 -> 重建项目

  • Close Project
  • 关闭项目


  • Create New or Open existing project
  • Then
  • 创建新项目或打开现有项目
  • 然后

File->Import Module

文件->导入模块

  • This time select
  • 这次选择

/home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/XXX.iml

/home/anthony/Documents/OpenCV-2.4.5-android-sdk/sdk/java/XXX.iml

mine was sdk.iml, but yours could be anything but there will be only one

我的是 sdk.iml,但你的可以是任何东西,但只有一个

iml file Select iml file to import module

.iml 文件 选择 iml 文件导入模块

You can now start using OpenCV functions, start by typing

您现在可以开始使用 OpenCV 函数,首先输入

import org.

进口组织

once you type the period IntelliJ should drop a list of options one of which is

一旦你输入句号 IntelliJ 应该删除一个选项列表,其中一个是

opencv

opencv

Now OpenCV is correctly integrated in your IDE

现在 OpenCV 已正确集成到您的 IDE 中

The rest is up to you.

剩下的就看你了。

回答by Hagai

I think things have changed a little since the previous answers were posted and I have tried them right now (opencv 2.4.9) and wanted to add a few things:

我认为自从之前的答案发布以来事情发生了一些变化,我现在已经尝试过(opencv 2.4.9)并想添加一些东西:

From the beginning:

从一开始

first execute from terminal cmake -DBUILD_SHARED_LIBS=OFF opencv-2.4.9/from the folder "above" the open-cv document that you have just extracted, then execute make -j8, this might take a while..

首先从cmake -DBUILD_SHARED_LIBS=OFF opencv-2.4.9/您刚刚提取的 open-cv 文档“上方”文件夹中的终端执行,然后执行make -j8,这可能需要一段时间..

Now, in Intellij go to File | Project Structure, and choose Global Libraries, and add the open-cv jar that is located under opencv/bin.

现在,在 Intellij 中转到File | Project Structure,然后选择Global Libraries,然后添加位于 下的 open-cv jar opencv/bin

In that point, if you'll try to run one of the examples you'll probably get something like Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path

在这一点上,如果您尝试运行其中一个示例,您可能会得到类似的结果 Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path

Next, as dlx.folmead1 suggested above, go to Run | Edit Configuration, and add to VM options -Djava.library.path=/absolute-path-to/opencv/lib

接下来,按照上面建议的 dlx.folmead1,转到Run | Edit Configuration,然后添加到 VM 选项-Djava.library.path=/absolute-path-to/opencv/lib

Of course, it is always a good idea to take a look at open-cv's documentationabout java and open-cv

当然,看看open-cv关于java和open-cv的文档总是一个好主意

回答by Tommy Mertell

None of these answers helped me with my solution. I found this: https://github.com/ctodobom/OpenCV-3.1.0-Androidand it is, by far, the easiest solution for android. -edit- To use this, you do have to have a Github repository that you want, but the steps are as follows. 1. Find the GitHub repo 2. Add maven { url 'https://jitpack.io' }to the project/build.gradle. 3. Then, inside of your app/build.gradleadd compile 'com.github.ctodobom:OpenCV-3.1.0-Android:-SNAPSHOT'This has the github website along with the username, the repo, and the first of the repo. This will link it into your application. -Edit- Additionally, if this method doesn't actually work for you as it did for me, then try using JavaCV. When I first implemented this method, it seemed like it worked because the IDE was reading that all of the functions were valid and I would get no errors. It was reading it but not importing so in some occasions, it would work as expected but it some occasions it would not. Therefore, it can work for you but I eventually went to JavaCV, which is far easier to work with in my opinion.

这些答案都没有帮助我解决问题。我发现了这个:https: //github.com/ctodobom/OpenCV-3.1.0-Android,它是迄今为止最简单的 android 解决方案。-edit- 要使用它,你必须有一个你想要的 Github 存储库,但步骤如下。1. 找到 GitHub 存储库 2. 添加maven { url 'https://jitpack.io' }project/build.gradle. 3. 然后,在你的app/build.gradleadd里面compile 'com.github.ctodobom:OpenCV-3.1.0-Android:-SNAPSHOT'这有 github 网站以及用户名、repo 和第一个 repo。这会将其链接到您的应用程序中。-Edit- 此外,如果此方法实际上对您不起作用,就像对我一样,请尝试使用 JavaCV。当我第一次实现这个方法时,它似乎有效,因为 IDE 正在读取所有函数都有效并且我不会出错。它正在读取它但没有导入,因此在某些情况下,它会按预期工作,但在某些情况下却不会。因此,它可以为您工作,但我最终选择了 JavaCV,在我看来,它更容易使用。

回答by SentientFlesh

In order to run any programs that utilize OpenCV you need to make sure that the OpenCV binary is on the java.library.pathaka. Environment Variable 'Path'should contain the path to your opencv_java***.dllfile. Whenever java runs an external library it needs to know where to find the binary files for your specific OS. In my case, Windows 10 x64:

为了运行任何使用 OpenCV 的程序,您需要确保 OpenCV 二进制文件在java.library.pathaka 上。环境变量'Path'应包含opencv_java***.dll文件的路径。每当 java 运行外部库时,它需要知道在哪里可以找到特定操作系统的二进制文件。就我而言,Windows 10 x64:

D:\software\opencv\build\java\x64\opencv_java420.dll

Make sure to change the directory above to match yours. Ensure you are using the proper binary for your OS.

确保更改上面的目录以匹配您的目录。确保为您的操作系统使用正确的二进制文件。

If you're strictly looking for dependency management go to your OpenCV folder and find the opencv-***.jarfile inside the program directory:

如果您严格寻找依赖项管理,请转到您的 OpenCV 文件夹并opencv-***.jar在程序目录中找到该文件:

D:\software\opencv\build\java\opencv-420.jar

We need to add this jar file as a dependency in Gradle, so in your build.gradlefile add the following to your dependencies:

我们需要将此 jar 文件添加为 Gradle 中的依赖项,因此在您的build.gradle文件中将以下内容添加到您的依赖项中:

compile fileTree(dir: 'D:\software\opencv\build\java\', includes: ['*jar'])

Make sure to change the directory name above to match yours.

确保更改上面的目录名称以匹配您的名称。

You can also keep the dependencies inside the project directory, creating a 'libs'folder in your project root, and changing the above directory to libs. This can be helpful for organization or maintaining the library version of software you might otherwise update.

您还可以将依赖项保留在项目目录中,'libs'在您的项目根目录中创建一个文件夹,并将上述目录更改为libs. 这对于组织或维护您可能会更新的软件库版本很有帮助。