Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
Tutorial: First java application with ViSP

Introduction

We assume that you have already followed the previous tutorial Tutorial: Installing ViSP for Java.

This tutorial will guide you through the creation of a simple Java console application using ViSP library in Eclipse.

Create a new project

Open Eclipse and create a new Java project; open the "File" menu, go to "New" and click on "Java Project".

img-tutorial-java-eclipse-create-new-project.jpeg

In the "New Java Project" dialog write the name of your project (let say visp-java-started) and click on "Finish".

img-tutorial-java-eclipse-java-started.jpeg

In the "New module-info.java" dialog that may appear, press "Don't Create" button.

img-tutorial-java-eclipse-dont-create.jpeg

Add a user library

If you followed the previous tutorial (Tutorial: Installing ViSP for Java), you should already have ViSP library set in your workspace’s user libraries; if not please check out the previous tutorial. Now you should be ready to add the library to your project. Inside Eclipse’s Package Explorer just right-click on your project’s folder and go to "Build Path --\> Add Libraries...".

img-tutorial-java-eclipse-add-libraries.jpeg

Select "User Libraries" and click on "Next":

img-tutorial-java-eclipse-add-libraries2.jpeg

Check the checkbox of the ViSP library and click "Finish".

img-tutorial-java-eclipse-add-libraries3.jpeg

Create a simple application

Now add a new Class to your project by right-clicking on your project’s folder and go to "New --\> Class".

img-tutorial-java-eclipse-new-class.jpeg

Write a name of your choice for both the package and the class then click on "Finish".

img-tutorial-java-eclipse-new-class2.jpeg

Now we are ready to write the code of our first application. Let’s start by importing the main classes part of core module (vpCameraParameters, vpColVector, vpImage, vpMatrix, vpRGBa). Then we load visp_java library. After we continue defining the main() method that shows how to manipulate ViSP classes in Java.

The code of the Main class is the following:

import org.visp.core.VpCameraParameters;
import org.visp.core.VpColVector;
import org.visp.core.VpImageRGBa;
import org.visp.core.VpImageUChar;
import org.visp.core.VpMatrix;
import org.visp.core.VpRGBa;
public class Main {
static {
System.loadLibrary("visp_java320");
}
public static void main(String[] args) {
// VpMatrix
VpMatrix vp = new VpMatrix(2,3,1.5);
System.out.println(vp.getCol(0));
System.out.println(vp.transpose());
// VpColVector
VpColVector vpColVector = new VpColVector(10,1.5);
System.out.println(vpColVector.infinityNorm());
// VpImageUChar
VpImageUChar imageUChar = new VpImageUChar(2, 4, (byte)220);
System.out.println(imageUChar);
// VpImageRGBa
VpImageRGBa colorImage = new VpImageRGBa(3, 5, new VpRGBa((char)255,(char)0,(char)0,(char)255));
System.out.println(colorImage);
// VpCameraParameters
VpCameraParameters vpCameraParameters = new VpCameraParameters(1.0, 1.0, 1.0, 1.0);
System.out.println(vpCameraParameters.get_K());
}
}

After a copy/paste you should have something similar to:

img-tutorial-java-eclipse-started-code.jpeg

We can now try to build and run our application by clicking on the "Run" button. You should have the following output:

img-tutorial-java-eclipse-started-console.jpeg