Introduction to Java Development
********************************
-Last updated: 28 February, 2013.
-
As of OpenCV 2.4.4, OpenCV supports desktop Java development using nearly the same interface as for
Android development. This guide will help you to create your first Java (or Scala) application using OpenCV.
We will use either `Eclipse <http://eclipse.org/>`_, `Apache Ant <http://ant.apache.org/>`_ or the
For further reading after this guide, look at the :ref:`Android_Dev_Intro` tutorials.
What we'll do in this guide
-***************************
+===========================
In this guide, we will:
so consult those files if you get lost.
Get proper OpenCV
-*****************
+=================
Starting from version 2.4.4 OpenCV includes desktop Java bindings.
Download
-########
+--------
The most simple way to get it is downloading the appropriate package of **version 2.4.4 or higher** from the
`OpenCV SourceForge repository <http://sourceforge.net/projects/opencvlibrary/files/>`_.
`Apache Ant <http://ant.apache.org/>`_ and `Python` v2.6 or higher to be installed.
Build
-#####
+-----
Let's build OpenCV:
Besides all this will create a ``jar`` containing the Java interface (:file:`bin/opencv-244.jar`)
and a native dynamic library containing Java bindings and all the OpenCV stuff
-(:file:`bin/Release/opencv_java244.dll` or :file:`lib/libopencv_java244.so` respectively).
+(:file:`lib/libopencv_java244.so` or :file:`bin/Release/opencv_java244.dll` respectively).
We'll use these files later.
Java sample with Ant
-********************
+====================
.. note::
The described sample is provided with OpenCV library in the :file:`opencv/samples/java/ant` folder.
:align: center
Java project in Eclipse
-***********************
+=======================
Now let's look at the possiblity of using OpenCV in Java when developing in Eclipse IDE.
* Create a new Eclipse workspace
* Create a new Java project via :guilabel:`File --> New --> Java Project`
- .. image:: images/eclipse_new_java_prj.png
- :alt: Eclipse: new Java project
- :align: center
+ .. image:: images/eclipse_new_java_prj.png
+ :alt: Eclipse: new Java project
+ :align: center
- Call it say "HelloCV".
+ Call it say "HelloCV".
* Open :guilabel:`Java Build Path` tab on :guilabel:`Project Properties` dialog
- and configure additional library (OpenCV) reference (jar and native library location):
+ and configure additional library (OpenCV) reference (jar and native library location):
- .. image:: images/eclipse_user_lib.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib2.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib2.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib3.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib3.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib4.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib4.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib5.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib5.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib6.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib6.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib7.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib7.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
+ |
- .. image:: images/eclipse_user_lib8.png
- :alt: Eclipse: external JAR
- :align: center
+ .. image:: images/eclipse_user_lib8.png
+ :alt: Eclipse: external JAR
+ :align: center
- ` `
* Add a new Java class (say ``Main``) containing the application entry:
- .. image:: images/eclipse_main_class.png
- :alt: Eclipse: Main class
- :align: center
+ .. image:: images/eclipse_main_class.png
+ :alt: Eclipse: Main class
+ :align: center
* Put some simple OpenCV calls there, e.g.:
- .. code-block:: java
- import org.opencv.core.Core;
- import org.opencv.core.CvType;
- import org.opencv.core.Mat;
+ .. code-block:: java
- public class Main {
- public static void main(String[] args) {
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
- System.out.println("m = " + m.dump());
- }
+ 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.loadLibrary(Core.NATIVE_LIBRARY_NAME);
+ Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
+ System.out.println("m = " + m.dump());
}
+ }
* Press :guilabel:`Run` button and find the identity matrix content in the Eclipse ``Console`` window.
- .. image:: images/eclipse_run.png
- :alt: Eclipse: run
- :align: center
+ .. image:: images/eclipse_run.png
+ :alt: Eclipse: run
+ :align: center
SBT project for Java and Scala
-******************************
+==============================
Now we'll create a simple Java application using SBT. This serves as a brief introduction to
those unfamiliar with this build tool. We're using SBT because it is particularly easy and powerful.
:align: center
Running SBT samples
-###################
+-------------------
Now we'll create a simple face detection application using OpenCV.