简体   繁体   中英

How to use OpenCV with IntelliJ IDEA 12

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 .

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.

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.

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

-Djava.library.path=path/to/dll
  • 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

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

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

iml file 选择 iml 文件导入模块

You can now start using OpenCV functions, start by typing

import org.

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

opencv

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

The rest is up to you.

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:

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

Now, in Intellij go to File | Project Structure File | Project Structure , and choose Global Libraries , and add the open-cv jar that is located under 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

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

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

First install Chocolatey via PowerShell by The Following command

Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iwr https://community.chocolatey.org/install.ps1 -UseBasicParsing | iex

After chocolatey installation

choco install opencv

The Above command will install the opencv latest version available

You will need to manually create an OPENCV_DIR environment variable then add %OPENCV_DIR%\\bin to your PATH. Environment setup AFter this just add The following Maven dependencies

 <dependencies>
    <dependency>
        <groupId>org.openpnp</groupId>
        <artifactId>opencv</artifactId>
        <version>4.5.1-2</version>
    </dependency>
</dependencies>

Then add this piece of code ;

 OpenCV.loadLocally();

and boom it will work hunky dory!!!!

Bellow code snipet is to test .

 OpenCV.loadLocally();
    System.out.println("Welcome to OpenCV " + Core.VERSION);

    Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
    System.out.println("OpenCV Mat: " + m);
    Mat mr1 = m.row(1);
    mr1.setTo(new Scalar(1));
    Mat mc5 = m.col(5);
    mc5.setTo(new Scalar(5));
    System.out.println("OpenCV Mat data:\n" + m.dump());

None of these answers helped me with my solution. I found this: https://github.com/ctodobom/OpenCV-3.1.0-Android and 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.gradle add 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.

In order to run any programs that utilize OpenCV you need to make sure that the OpenCV binary is on the java.library.path aka. Environment Variable 'Path' should contain the path to your opencv_java***.dll file. 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:

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-***.jar file inside the program directory:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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