The full exported Javadocs for the iHart Java client library are available here.
For more on how iHart works, see this page.
First, make sure you have downloaded the latest version of the iHart project from this website
(or cloned the repository from GitHub).
You will need to import the
ihart package and its contents, in order to use classes like
In order for Eclipse to compile and run your project, it needs to know about the iHart client library and its dependencies.
To link the
.jar files included with iHart:
javax.json.jarfiles. They are both required for the project to compile and run.
At this point, you should be able to import the ihart package and its contents.
If you're planning on compiling your project from the Terminal/Command Prompt,
the iHart client library and its dependencies need to be available.
Whenever you compile or run your program, you will need to include the necessary
.jar files in the classpath.
Note: if you are unfamiliar with compiling and running Java programs from the command line, Eclipse may be a better option.
To compile your project, run the following where your source
.java files are located,
and replace "<path to ihart project>" with the relative path to where you placed the iHart library.
javac -cp "<path to ihart project>/client/library/java/*" *.java
For example, to compile the PrintlnProgram example application in "ihart/client/sample/PrintlnProgram", you would first change directories to that folder, and then run:
javac -cp "../../library/java/*" PrintlnProgram.java
To run your project, run the following where your compiled ".class" files are located, and again
replace "<path to ihart project>" with the relative path to where you placed the iHart library.
You should also replace "<YourMainClass>" with the class containing your
java -cp ".:<path to ihart project>/client/library/java/*" <YourMainClass>
If you are on Windows rather than OS X, you may have to substitute a colon (
:) for the semicolon (
;), as below:
java -cp ".;<path to ihart project>/client/library/java/*" <YourMainClass>
Note that the
. specifies that Java should refer to your application as well as the iHart libraries when running.
For example, to run the PrintlnProgram example application in "ihart/client/sample/PrintlnProgram" on Windows, you would first change directories to that folder, compile the project as stated above, and then run:
java -cp ".;../../library/java/*" PrintlnProgram
For more details on the classpath option, see the official Java documentation and this Stack Overflow answer.
The iHart client library is set up with a class that manages connecting to the server and receiving messages,
and a set of classes that allow you to receive and process events. Similarly to Java's ActionListener interface,
the library provides a
CVEventListener interface with three methods:
public void shellsArrived(CVEvent shellEvent)
public void facesArrived(CVEvent faceEvent)
public void blobsArrived(CVEvent blobEvent)
Any class that implements the
CVEventListener interface must implement all of these methods (though the method body may be empty for one or more of them).
CVEvents are fired whenever information is received from the server about motion (aka "shells") or faces were detected.
A generic "all blobs" event will also be dispatched whenever faces, shells, or both were detected.
Every event has a type and a list of blobs associated with it, and you can get the number of blobs from the event object as well.
CVEvents also refer to "regions of interest" or "areas of interest," which you can read more about here.
All blobs occur within the bounds of a single region of interest.
CVEvent object, you can choose to
getAllBlobs() regardless of what region of interest they occurred in,
getBlobsInRegion(int regionOfInterest) to get only the blobs that occurred within the specified region.
Both methods return
List<Blob>; you can iterate over this list to get access to each of the
Blobs represent either a face or a shell that was detected by the server.
Each blob has an x coordinate and a y coordinate (representing the top left corner of the blob), a width, a height, a type (face or shell),
and the index of the region of interest it occurred in.
Note: the x, y, width, and height are stored and returned as
doubles, in order to be as precise as possible.
java.swing classes and methods require ints as parameters, so you may need to cast these values before using them for graphics.
Once you have a class that implements
CVEventListener, you can start receiving events from the server. There are a few steps involved:
cvServer.appfile; on Windows, you should run the
In your code, start the connection to the server by creating a
This object needs to know where the server is running; usually, that will be on the same computer ("localhost" is the hostname) with the default port (5204).
When the CVManager is created, it will attempt to connect to the server.
CVManager cvManager = new CVManager(“localhost”, 5204);
Next, let the
CVManager know that your object wants to receive
myClass is an instance of the class that implements
If your application exits with a message about being unable to connect to the server,
make sure you are running the server where the
CVManager expects it to be.