OpenNI2 on Raspberry PI + Java

Here are the things I did to get some java code accessing OpenNI2 on a Raspberry PI.

Unfortunately there doesn’t seem to be a version of the Orbbec SDK that is compatible with the PI.

I think I’m running Raspbian.

From the OpenNI 2 zip extract OpenNI-Linux-Arm-2.3.zip

simlink the Redist directory contents to /usr/local/lib

cd /usr/local/lib
sudo ln -s /home/pi/openNI/OpenNI-Linux-Arm-2.3/Redist/* .
sudo ldconfig

also simlink the Redist directory contents to /usr/java/packages/lib/arm/

cd /usr/java/packages/lib/arm/
sudo ln -s /home/pi/openNI/OpenNI-Linux-Arm-2.3/Redist/* .

launch the code

sudo java -cp astra2-0.0.1-SNAPSHOT.jar:OpenNI-Linux-Arm-2.3/Redist/org.openni.jar au.com.rsutton.Test

Here is my code which converts the depth stream to a top down view

package au.com.rsutton;

import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import org.openni.CoordinateConverter;
import org.openni.Device;
import org.openni.DeviceInfo;
import org.openni.OpenNI;
import org.openni.PixelFormat;
import org.openni.Point3D;
import org.openni.SensorInfo;
import org.openni.SensorType;
import org.openni.VideoFrameRef;
import org.openni.VideoMode;
import org.openni.VideoStream;
import org.openni.VideoStream.NewFrameListener;

public class Test
{

	public static void main(String s[]) throws InterruptedException
	{

		System.out.println(System.getProperty("java.library.path"));

		VideoStream mVideoStream;

		// initialize OpenNI
		OpenNI.initialize();

		String uri;

		if (s.length > 0)
		{
			uri = s[0];
		} else
		{
			List<DeviceInfo> devicesInfo = OpenNI.enumerateDevices();
			if (devicesInfo.isEmpty())
			{
				System.out.println("No device is connected");
				return;
			}
			uri = devicesInfo.get(0).getUri();
		}

		Device device = Device.open(uri);

		SensorInfo sensorInfo = device.getSensorInfo(SensorType.DEPTH);

		mVideoStream = VideoStream.create(device, SensorType.DEPTH);

		List<VideoMode> supportedModes = mVideoStream.getSensorInfo().getSupportedVideoModes();
		for (VideoMode mode : supportedModes)
		{
			System.out.println(mode);
		}

		VideoMode mode = supportedModes.get(0);
		mVideoStream.setVideoMode(mode);
		mVideoStream.start();

		mVideoStream.addNewFrameListener(new NewFrameListener()
		{
			long lastTime = System.currentTimeMillis();

			@Override
			public void onFrameReady(VideoStream stream)
			{
				VideoFrameRef frame = stream.readFrame();

				PixelFormat pixelFormat = frame.getVideoMode().getPixelFormat();
				// pixelFormat.DEPTH_1_MM
				System.out.println(pixelFormat);

				FloatBuffer buffer = frame.getData().asFloatBuffer();
				int width = frame.getWidth();
				int height = frame.getHeight();

				System.out.println("Width: " + width);
				System.out.println("Height: " + height);

				ShortBuffer sb = frame.getData().order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
				sb.rewind();
				int z, count = 0;

				int[][] flatscan = new int[40][80];
				for (int y = 0; y < height; y += 10)
				{
					for (int x = 0; x < width; x += 10)
					{
						z = sb.get(x + (y * width));
						if (z < 0)
						{
							z = 65536 + z;
						}
						Point3D<Float> point = CoordinateConverter.convertDepthToWorld(mVideoStream, x, y, z);

						if (point.getZ() > 0)
						{
							flatscan[(point.getX().intValue() / 100) + (20)][point.getZ().intValue() / 100]++;
						}
					}
				}
				for (int y = 0; y < 40; y += 1)
				{
					for (int x = 0; x < 40; x += 1)
					{

						if (flatscan[x][y] > 0)
						{
							System.out.print("X");
						} else
						{
							System.out.print(" ");
						}

					}
					System.out.println("");
				}

				// while (buffer.hasRemaining())
				// {
				//
				// float x = buffer.get();
				// float y = buffer.get();
				// float z = buffer.get();
				// if (z > 0)
				// {
				// System.out.println(x + " " + y + " " + z);
				// Point3D<Float> point =
				// CoordinateConverter.convertDepthToWorld(mVideoStream, x, y,
				// z);
				//
				// System.out.println(point.getX() + " " + point.getY() + " " +
				// point.getZ());
				// }
				// }
				frame.release();
				System.out.println("ms per frame " + (System.currentTimeMillis() - lastTime));
				lastTime = System.currentTimeMillis();

			}
		});

		TimeUnit.SECONDS.sleep(60);

		device.close();
		OpenNI.shutdown();

	}
}