Search in sources :

Example 1 with SyncMultiFrameListener

use of org.bytedeco.javacpp.freenect2.SyncMultiFrameListener in project javacpp-presets by bytedeco.

the class freenect2Example method main.

public static void main(String[] args) {
    Freenect2 freenect2Context;
    try {
        Loader.load(org.bytedeco.javacpp.freenect2.class);
        // Context is shared accross cameras.
        freenect2Context = new Freenect2();
    } catch (Exception e) {
        System.out.println("Exception in the TryLoad !" + e);
        e.printStackTrace();
        return;
    }
    Freenect2Device device = null;
    PacketPipeline pipeline = null;
    String serial = "";
    pipeline = new CpuPacketPipeline();
    if (serial == "") {
        serial = freenect2Context.getDefaultDeviceSerialNumber().getString();
        System.out.println("Serial:" + serial);
    }
    device = freenect2Context.openDevice(serial, pipeline);
    // [listeners]
    int types = 0;
    types |= freenect2.Frame.Color;
    types |= freenect2.Frame.Ir | freenect2.Frame.Depth;
    SyncMultiFrameListener listener = new freenect2.SyncMultiFrameListener(types);
    device.setColorFrameListener(listener);
    device.setIrAndDepthFrameListener(listener);
    device.start();
    System.out.println("Serial: " + device.getSerialNumber().getString());
    System.out.println("Firmware: " + device.getFirmwareVersion().getString());
    // / [start]
    FrameMap frames = new FrameMap();
    // Fetch 100 frames.
    int frameCount = 0;
    for (int i = 0; i < 100; i++) {
        System.out.println("getting frame " + frameCount);
        if (// 10 sconds
        !listener.waitForNewFrame(frames, 10 * 1000)) {
            System.out.println("timeout!");
            return;
        }
        freenect2.Frame rgb = frames.get(freenect2.Frame.Color);
        freenect2.Frame ir = frames.get(freenect2.Frame.Ir);
        freenect2.Frame depth = frames.get(freenect2.Frame.Depth);
        // / [loop start]
        System.out.println("RGB image, w:" + rgb.width() + " " + rgb.height());
        byte[] imgData = new byte[1000];
        rgb.data().get(imgData);
        for (int pix = 0; pix < 10; pix++) {
            System.out.print(imgData[pix] + " ");
        }
        System.out.println();
        frameCount++;
        listener.release(frames);
        continue;
    }
    device.stop();
    device.close();
}
Also used : FrameMap(org.bytedeco.javacpp.freenect2.FrameMap) CpuPacketPipeline(org.bytedeco.javacpp.freenect2.CpuPacketPipeline) org.bytedeco.javacpp.freenect2(org.bytedeco.javacpp.freenect2) CpuPacketPipeline(org.bytedeco.javacpp.freenect2.CpuPacketPipeline) PacketPipeline(org.bytedeco.javacpp.freenect2.PacketPipeline) Freenect2(org.bytedeco.javacpp.freenect2.Freenect2) SyncMultiFrameListener(org.bytedeco.javacpp.freenect2.SyncMultiFrameListener) Freenect2Device(org.bytedeco.javacpp.freenect2.Freenect2Device)

Example 2 with SyncMultiFrameListener

use of org.bytedeco.javacpp.freenect2.SyncMultiFrameListener in project javacv by bytedeco.

the class OpenKinect2FrameGrabber method start.

@Override
public void start() throws FrameGrabber.Exception {
    if (freenect2Context == null) {
        try {
            OpenKinect2FrameGrabber.tryLoad();
        } catch (Exception e) {
            System.out.println("Exception in the TryLoad !" + e);
            e.printStackTrace();
        }
    }
    if (freenect2Context == null) {
        throw new Exception("FATAL error: OpenKinect2 camera: driver could not load.");
    }
    if (freenect2Context.enumerateDevices() == 0) {
        throw new Exception("FATAL error: OpenKinect2: no device connected!");
    }
    device = null;
    PacketPipeline pipeline = null;
    pipeline = new CpuPacketPipeline();
    // pipeline = new libfreenect2::OpenGLPacketPipeline();
    // pipeline = new libfreenect2::OpenCLPacketPipeline(deviceId);
    // pipeline = new libfreenect2::CudaPacketPipeline(deviceId);
    serial = freenect2Context.getDeviceSerialNumber(deviceNumber).getString();
    device = freenect2Context.openDevice(serial, pipeline);
    frameListener = new freenect2.SyncMultiFrameListener(frameTypes);
    if (colorEnabled) {
        device.setColorFrameListener(frameListener);
    }
    if (depthEnabled || IREnabled) {
        device.setIrAndDepthFrameListener(frameListener);
    }
    rawVideoImage = IplImage.createHeader(1920, 1080, IPL_DEPTH_8U, 4);
    device.start();
    System.out.println("OpenKinect2 device started.");
    System.out.println("Serial: " + device.getSerialNumber().getString());
    System.out.println("Firmware: " + device.getFirmwareVersion().getString());
}
Also used : SyncMultiFrameListener(org.bytedeco.javacpp.freenect2.SyncMultiFrameListener) CpuPacketPipeline(org.bytedeco.javacpp.freenect2.CpuPacketPipeline) org.bytedeco.javacpp.freenect2(org.bytedeco.javacpp.freenect2) PacketPipeline(org.bytedeco.javacpp.freenect2.PacketPipeline) CpuPacketPipeline(org.bytedeco.javacpp.freenect2.CpuPacketPipeline)

Aggregations

org.bytedeco.javacpp.freenect2 (org.bytedeco.javacpp.freenect2)2 CpuPacketPipeline (org.bytedeco.javacpp.freenect2.CpuPacketPipeline)2 PacketPipeline (org.bytedeco.javacpp.freenect2.PacketPipeline)2 SyncMultiFrameListener (org.bytedeco.javacpp.freenect2.SyncMultiFrameListener)2 FrameMap (org.bytedeco.javacpp.freenect2.FrameMap)1 Freenect2 (org.bytedeco.javacpp.freenect2.Freenect2)1 Freenect2Device (org.bytedeco.javacpp.freenect2.Freenect2Device)1