9

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.

Cadel Watson
  • 511
  • 2
  • 4
  • 12

6 Answers6

18

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
dlx.folmead1
  • 196
  • 1
  • 3
  • 3
    For example, in Windows environment: `-Djava.library.path="C:/OpenCv 2.4.6/build/java/x64"` – Gondy Oct 16 '13 at 09:09
  • I found it easier to add native libs via Menu->File->Project Structure, see more details here http://stackoverflow.com/questions/40352831/i-need-to-install-opencv-and-use-it-via-java-in-mavenintellij-on-windows-and-m/40354365#40354365 – Dmitry Konovalov Nov 01 '16 at 05:14
10
  • 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 Select iml file to import module

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

Now OpenCV is correctly integrated in your IDE

The rest is up to you.

Anthony Keane
  • 519
  • 7
  • 14
  • Thank you, this was the easiest solution I came about to use OpenCV 3.1 with Android Studio. Small note -- the only missing instructions were: 1) "Open Module Settings" (right-click the "app" in your project to find it) and under "Dependencies", click the "+" button, then choose "Module dependency", and then OpenCV. 2) from the OpenCV sdk, copy the sdk/native/libs directory into your project's app/src/main, renaming it jniLibs. – Greg Sadetsky Jan 28 '16 at 12:50
6

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

Hagai
  • 678
  • 7
  • 20
  • Open the Project Structure dialog ( Ctrl+Shift+Alt+S) – GPrathap Feb 07 '15 at 00:43
  • In IntelliJ, it is easy to add native libs via Menu->File->Project Structure, see http://stackoverflow.com/questions/40352831/i-need-to-install-opencv-and-use-it-via-java-in-mavenintellij-on-windows-and-m/40354365#40354365 – Dmitry Konovalov Nov 01 '16 at 05:18
1

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());
0

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.

Tommy Mertell
  • 105
  • 1
  • 8
0

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.

jon.bray.eth
  • 527
  • 6
  • 13