fastLearner fastLearner - 24 days ago 6
Java Question

Where to get the jar for openCV?

Where are the Java jar libraries for the openCV core extensions so that I can import it in my java code?

I cannot find a single place where they have taught how to get everything set up properly. I am using

Ubuntu 12.04
and I have openCV installed. I want to use it in eclipse IDE, and eclipse needs a jar file so that I can use openCV functions. I saw the following link which has used the
import org.opencv.core.Core;


How can I get those .jar files?

Answer

You can find jars for openCV for linux kicking around the internet like at this link. However it won't work unless you have the native libraries openCV needs to do its work.

A sure way to get openCV available in your eclipse java project is to compile your own jar file from source to make it available as described here: https://udallascs.wordpress.com/2014/03/30/adding-opencv-and-configuring-to-work-with-eclipse-and-java/

Here is the jist of instructions for Linux, open a terminal and run these commands:

cd ~
mkdir Vision
cd Vision
git clone git://github.com/Itseez/opencv.git
cd opencv
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=OFF ..
make -j8

If everything succeeded, then your jars will be under the be under the bin directory:

./bin/opencv-300.jar

Move that opencv-300.jar into the lib directory in your project and include it as an external jar. Here is the barebones program that uses it.

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to OpenCV " + Core.VERSION);
        System.out.println(System.getProperty("java.library.path"));
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("m = " + m.dump());
    }
}

Within eclipse, your jar file will need the native libraries you built earlier to be available. So in eclipse, navigate to:

Project->Properties->Java Build Path->Libraries tab-> Add external jars -> opencv-300.jar

Then double click: "Native library location" and enter in the build/lib where you built it, in my case: /home/el/Vision/opencv/build/lib

Run the java program, the program prints:

Welcome to OpenCV 3.0.0-dev
/home/el/Vision/opencv/build/lib
m = [  1,   0,   0;
   0,   1,   0;
   0,   0,   1]

If you want to give this program to someone else and have them be able to run it, they will need to have openCV version 3.0.0 also available on their system, or else the java program will not find the libraries, and immediately exit.

Why is this so hard, why is this not just a simple jar?

Because openCV is written in C, and the jar file is just a window into that C world. So we have to make a rube goldberg machine to make the methods in OpenCV available to your java application.

Comments