Source Code Examples
This website offers several source code examples of Kinect-enabled applications in Java. The examples use the J4K Java library, which is compatible with all kinect devices (Kinect for Windows, Kinect for XBOX, new Kinect, or Kinect 2). The classes in the J4K library communicate directly with a native Windows library, which handles the video, depth, and skeleton streams of the Kinect using the Java Native Interface (JNI). More information about the J4K library can be found in this link.

The examples contain the following applications:







How to write your own Kinect-Java programs
This tutorial will show you how to develop your own computer vision applications using Kinect in Java.
The most important part of code in all the provided examples is the definition of a Java class that handles the live streams received from a Kinect sensor. You can easily define such a class in less than 10 lines of java code!
Here is how:
import edu.ufl.digitalworlds.j4k.J4KSDK;
import edu.ufl.digitalworlds.j4k.DepthMap;
import edu.ufl.digitalworlds.j4k.Skeleton;
import edu.ufl.digitalworlds.j4k.VideoFrame;
/*This class is an implementation of the abstract class J4KSDK.
It is a simple example of source code that shows how to read
depth, video, and skeleton frames from a Kinect sensor.*/
public class Kinect extends J4KSDK{
import edu.ufl.digitalworlds.j4k.DepthMap;
import edu.ufl.digitalworlds.j4k.Skeleton;
import edu.ufl.digitalworlds.j4k.VideoFrame;
/*This class is an implementation of the abstract class J4KSDK.
It is a simple example of source code that shows how to read
depth, video, and skeleton frames from a Kinect sensor.*/
public class Kinect extends J4KSDK{
First of all, your class should extend the J4KSDK class from the J4K library, as shown above. Optionally, you can define your own constructor and custom parameters for your class, as shown in the example below.
/*This object will hold the current video frame received from
the Kinect video camera.*/
VideoFrame videoTexture;
/*The constructor of the class initializes the native Kinect
SKD libraries and creates a new VideoFrame object.*/
public Kinect() {
super();
videoTexture=new VideoFrame();
}
Finally, you have to implement the interface of the 3 methods that will be automatically called every time a new depth, video, or skeleton frame is received from the Kinect sensor. You can customize the content of these 3 methods according to the needs of your application. An example of such implementation is shown below.
/*The following method will run every time a new depth frame is
received from the Kinect sensor. The data of the depth frame is
converted into a DepthMap object, with U,V texture mapping if
available.*/
@Override
public void onDepthFrameEvent(short[] depth_frame, byte[] player_index, float[] XYZ, float[] UV) {
DepthMap map=new DepthMap(getDepthWidth(),getDepthHeight(),XYZ);
if(UV!=null) map.setUV(UV);
}
/*The following method will run every time a new skeleton frame
is received from the Kinect sensor. The skeletons are converted
into Skeleton objects.*/
@Override
public void onSkeletonFrameEvent(boolean[] skeleton_tracked, float[] joint_position, float[] joint_orientation, byte[] joint_status) {
Skeleton skeletons[]=new Skeleton[getMaxNumberOfSkeletons()];
for(int i=0;i<getMaxNumberOfSkeletons();i++)
skeletons[i]=Skeleton.getSkeleton(i, skeleton_tracked, joint_position, joint_orientation, joint_status, this);
}
/*The following method will run every time a new color frame
is received from the Kinect video camera. The incoming frame
is passed to the videoTexture object to update its content.*/
@Override
public void onColorFrameEvent(byte[] data) {
videoTexture.update(getColorWidth(), getColorHeight(), data);
}
/*Similarly you can define methods that will run every time an
infrared frame is received from the Kinect sensor.*/
}
That's it! A simpler implementation can be written in less than 10 lines. As easy as A-B-C!
Disclaimer: The names JAVA and KINECT and their associated logos are trademarks of their respective copyright owners Oracle and Microsoft. None of these companies endorse, fund, or are in any way associated with the J4K library.
Disclaimer: This software is provided for free without any warranty expressed or implied for academic, research, and strictly non commercial purposes only. By downloading this library you accept the Terms and Conditions.