Merge pull request #1351 from barisdemiroz:eclipse-java-tutorial

This commit is contained in:
Alexander Smorkalov 2013-09-24 14:44:32 +04:00 committed by OpenCV Buildbot
commit 36e7697d1b
28 changed files with 133 additions and 95 deletions

View File

@ -11,3 +11,4 @@
.. |Author_EricCh| unicode:: Eric U+0020 Christiansen
.. |Author_AndreyP| unicode:: Andrey U+0020 Pavlenko
.. |Author_AlexS| unicode:: Alexander U+0020 Smorkalov
.. |Author_BarisD| unicode:: Bar U+0131 U+015F U+0020 Evrim U+0020 Demir U+00F6 z

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

View File

@ -7,10 +7,9 @@ Introduction to Java Development
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
`Simple Build Tool (SBT) <http://www.scala-sbt.org/>`_ to build the application.
We will use either `Apache Ant <http://ant.apache.org/>`_ or `Simple Build Tool (SBT) <http://www.scala-sbt.org/>`_ to build the application.
For further reading after this guide, look at the :ref:`Android_Dev_Intro` tutorials.
If you want to use Eclipse head to :ref:`Java_Eclipse`. For further reading after this guide, look at the :ref:`Android_Dev_Intro` tutorials.
What we'll do in this guide
===========================
@ -19,7 +18,7 @@ In this guide, we will:
* Get OpenCV with desktop Java support
* Create an ``Ant``, ``Eclipse`` or ``SBT`` project
* Create an ``Ant`` or ``SBT`` project
* Write a simple OpenCV application in Java or Scala
@ -233,97 +232,6 @@ Java sample with Ant
:alt: run app with Ant
: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
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):
.. 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_lib3.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_lib6.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
* Add a new Java class (say ``Main``) containing the application entry:
.. 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;
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
SBT project for Java and Scala
==============================

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -0,0 +1,110 @@
.. _Java_Eclipse:
Using OpenCV Java with Eclipse
*********************************************
Since version 2.4.4 `OpenCV supports Java <http://opencv.org/opencv-java-api.html>`_. In this tutorial I will explain how to setup development environment for using OpenCV Java with Eclipse in **Windows**, so you can enjoy the benefits of garbage collected, very refactorable (rename variable, extract method and whatnot) modern language that enables you to write code with less effort and make less mistakes. Here we go.
Configuring Eclipse
===================
First, obtain a fresh release of OpenCV `from download page <http://opencv.org/downloads.html>`_ and extract it under a simple location like ``C:\OpenCV-2.4.6\``. I am using version 2.4.6, but the steps are more or less the same for other versions.
Now, we will define OpenCV as a user library in Eclipse, so we can reuse the configuration for any project. Launch Eclipse and select :guilabel:`Window --> Preferences` from the menu.
.. image:: images/1-window-preferences.png
:alt: Eclipse preferences
:align: center
Navigate under :guilabel:`Java --> Build Path --> User Libraries` and click :guilabel:`New...`.
.. image:: images/2-user-library-new.png
:alt: Creating a new library
:align: center
Enter a name, e.g. ``OpenCV-2.4.6``, for your new library.
.. image:: images/3-library-name.png
:alt: Naming the new library
:align: center
Now select your new user library and click :guilabel:`Add External JARs...`.
.. image:: images/4-add-external-jars.png
:alt: Adding external jar
:align: center
Browse through ``C:\OpenCV-2.4.6\build\java\`` and select ``opencv-246.jar``. After adding the jar, extend the :guilabel:`opencv-246.jar` and select :guilabel:`Native library location` and press :guilabel:`Edit...`.
.. image:: images/5-native-library.png
:alt: Selecting native library location 1
:align: center
Select :guilabel:`External Folder...` and browse to select the folder ``C:\OpenCV-2.4.6\build\java\x64``. If you have a 32-bit system you need to select the ``x86`` folder instead of ``x64``.
.. image:: images/6-external-folder.png
:alt: Selecting native library location 2
:align: center
Your user library configuration should look like this:
.. image:: images/7-user-library-final.png
:alt: Selecting native library location 2
:align: center
Testing the configuration on a new Java project
=====================================================
Now start creating a new Java project.
.. image:: images/7_5-new-java-project.png
:alt: Creating new Java project
:align: center
On the :guilabel:`Java Settings` step, under :guilabel:`Libraries` tab, select :guilabel:`Add Library...` and select :guilabel:`OpenCV-2.4.6`, then click :guilabel:`Finish`.
.. image:: images/8-add-library.png
:alt: Adding user defined library 1
:align: center
.. image:: images/9-select-user-lib.png
:alt: Adding user defined library 2
:align: center
Libraries should look like this:
.. image:: images/10-new-project-created.png
:alt: Adding user defined library
:align: center
Now you have created and configured a new Java project it is time to test it. Create a new java file. Here is a starter code for your convenience:
.. code-block:: java
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class Hello
{
public static void main( String[] args )
{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
System.out.println( "mat = " + mat.dump() );
}
}
When you run the code you should see 3x3 identity matrix as output.
.. image:: images/11-the-code.png
:alt: Adding user defined library
:align: center
That is it, whenever you start a new project just add the OpenCV user library that you have defined to your project and you are good to go. Enjoy your powerful, less painful development environment :)

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

View File

@ -138,6 +138,24 @@ world of the OpenCV.
:height: 90pt
:width: 90pt
.. tabularcolumns:: m{100pt} m{300pt}
.. cssclass:: toctableopencv
================ =================================================
|EclipseLogo| **Title:** :ref:`Java_Eclipse`
*Compatibility:* > OpenCV 2.4.4
*Author:* |Author_BarisD|
A tutorial on how to use OpenCV Java with Eclipse.
================ =================================================
.. |EclipseLogo| image:: images/eclipse-logo.png
:height: 90pt
:width: 90pt
* **Android**
.. tabularcolumns:: m{100pt} m{300pt}
@ -295,6 +313,7 @@ world of the OpenCV.
../windows_visual_studio_Opencv/windows_visual_studio_Opencv
../windows_visual_studio_image_watch/windows_visual_studio_image_watch
../desktop_java/java_dev_intro
../java_eclipse/java_eclipse
../android_binary_package/android_dev_intro
../android_binary_package/O4A_SDK
../android_binary_package/dev_with_OCV_on_Android