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:
  • SimpleExample A simple example that shows how to receive the detected skeleton frames, depth frames, and color frames from the Kinect sensor. More details...
  • VideoViewerApp A simple video viewer that shows how to receive and display live the color frames from the Kinect video stream. More details...
  • KinectViewerApp A simple Kinect viewer that visualizes the live depth frames as a 3D surface and also shows the detected skeleton, color, and infrared streams. It also offers many choices for changing the resolution and type of streams and for controlling other parameters of the sensors. More details...
  • AugmentedRealityApp This example combines the skeleton stream, depth stream, and video stream into an augmented reality application that shows the user holding a virtual box. The user can move, rotate, and scale up and down the box using natural gestures. More details...
  • ImageAvatarApp This is a simple Java application that uses the skeleton streams of a Kinect sensor and visualizes them as simple cardboard avatars, composed out of flat, 2D images. It also offers choices for changing the appearance of the avatars and the 3D scene. More details...
  • XEDConvertApp A simple application that can convert Kinect stream files recorded in XED format, by playing them in this Kinect-enabled example program and record the depth frames in a raw binary file that you can open later by your own programs. You can also use this application to record a live sequence of depth frames. More details...
  • MultipleKinectApp In this example, the video, depth, and skeleton streams from 2 Kinect sensors are opened and shown in a graphical user layout similar to the one used in KinectViewerApp. This application can be easily extended to handle more Kinect sensors. It is assumed that your computer has sufficient USB bandwidth to support more than one Kinect sensor. More details...

  • 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{

    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() {
            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

        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.*/
        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.*/
        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.

    University of Florida, Digital Worlds Institute, P.O.Box 115810, 101 Norman Gym, Gainesville, FL 32611-5810, USA