import org.openkinect.Freenect;
import org.openkinect.Device;
import org.openkinect.BufferCallback;
import java.util.Arrays;
import java.lang.Math;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JButton;
class TestInterface implements BufferCallback, ActionListener
{
class ImagePanel extends JPanel {
public ImagePanel(int[] buffer) {
super();
this.buffer = buffer;
this.image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
}
protected void paintComponent(Graphics g)
{
image.setRGB(0, 0, 640, 480, buffer, 0, 640);
g.drawImage(image, 0, 0, Color.BLACK, null);
}
private BufferedImage image;
private int[] buffer;
}
public TestInterface(Device device)
{
JPanel button_pane = new JPanel();
button_pane.setLayout(new FlowLayout());
video_buffer = new int[640 * 480];
depth_buffer = new int[640 * 480];
Arrays.fill(video_buffer, 0x00000000);
Arrays.fill(depth_buffer, 0x00000000);
dialog = new JDialog((JFrame) null, "Kinect Camera", true);
video_panel = new ImagePanel(video_buffer);
depth_panel = new ImagePanel(depth_buffer);
up = new JButton("Up");
down = new JButton("Down");
video_panel.setPreferredSize(new Dimension(640, 480));
depth_panel.setPreferredSize(new Dimension(640, 480));
up.addActionListener(this);
down.addActionListener(this);
dialog.setLayout(new BorderLayout());
dialog.add(button_pane, BorderLayout.SOUTH);
dialog.add(video_panel, BorderLayout.WEST);
dialog.add(depth_panel, BorderLayout.EAST);
button_pane.add(up);
button_pane.add(down);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setSize(640 * 2, 500);
this.device = device;
this.angle = 0.0;
device.setTiltAngle(0.0);
}
public void video_capture(byte[] data, long timestamp)
{
synchronized(video_buffer) {
for(int i = 0; i < video_buffer.length; i++) {
video_buffer[i] = (0xFF << 24) | ((data[i * 3] & 0xFF) << 16) | ((data[i * 3 + 1] & 0xFF) << 8) | (data[i * 3 + 2] & 0xFF);
}
}
video_panel.repaint();
}
public void depth_capture(byte[] data, long timestamp)
{
synchronized(depth_buffer) {
for(int i = 0; i < depth_buffer.length; i++) {
int pval = ((data[i * 2] & 0xFF)) | ((data[i * 2 + 1] & 0xFF) << 8);
int dval = (int)((pval / 2048.0) * 256.0);
depth_buffer[i] = (0xFF << 24) | ((dval & 0xFF) << 16) | ((dval & 0xFF) << 8) | (dval & 0xFF);
}
}
depth_panel.repaint();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == up) {
angle += (Math.PI / 360.0) * 10.0;
} else {
angle -= (Math.PI / 360.0) * 10.0;
}
device.setTiltAngle(angle);
}
public void show()
{
dialog.setVisible(true);
}
private JButton up;
private JButton down;
private Device device;
private JDialog dialog;
private JPanel video_panel;
private JPanel depth_panel;
private int[] video_buffer;
private int[] depth_buffer;
private double angle;
}
public class FreenectTest {
public static void main(String[] args) {
Freenect kinect = new Freenect();
TestInterface gui;
try {
System.out.println("Devices: " + kinect.devices());
System.out.println("Subdevices: " + kinect.subdevices());
Device dev = kinect.open(0);
dev.setLED(Device.LED_GREEN);
gui = new TestInterface(dev);
dev.setCallback(gui);
dev.startVideo();
dev.startDepth();
gui.show();
//dev.stopVideo();
//dev.stopDepth();
dev.setLED(Device.LED_RED);
} finally {
kinect.shutdown();
System.out.println("DONE");
}
}
}