Search in sources :

Example 1 with RealSense2FrameGrabber

use of org.bytedeco.javacv.RealSense2FrameGrabber in project javacv by bytedeco.

the class RealSense2DepthMeasuring method main.

public static void main(String[] args) throws FrameGrabber.Exception {
    final RealSense2FrameGrabber rs2 = new RealSense2FrameGrabber();
    // list all cameras
    for (RealSense2FrameGrabber.RealSense2DeviceInfo info : rs2.getDeviceInfos()) {
        System.out.printf("Device: %s %s %s Locked: %b\n", info.getName(), info.getFirmware(), info.getSerialNumber(), info.isLocked());
    }
    // enable the color & depth stream of the realsense camera
    rs2.enableColorStream(640, 480, 30);
    rs2.enableDepthStream(640, 480, 30);
    // here are more examples of streams:
    /*
        rs2.enableColorStream(640, 480, 30); // color stream
        rs2.enableIRStream(640, 480, 90); // ir stream
        rs2.enableStream(new RealSense2FrameGrabber.RealSenseStream(
                RS2_STREAM_INFRARED,
                2,
                new Size(640, 480),
                30,
                RS2_FORMAT_Y8
        )); // second ir stream
        */
    // start realsense camera
    rs2.start();
    // start frame to view the stream
    CanvasFrame canvasFrame = new CanvasFrame("RealSense");
    canvasFrame.setCanvasSize(rs2.getImageWidth(), rs2.getImageHeight());
    // add mouse listener to see the depth at the clicked point
    canvasFrame.getCanvas().addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            try {
                System.out.println("Depth: " + rs2.getDistance(e.getX(), e.getY()));
            } catch (FrameGrabber.Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    // run canvas
    while (canvasFrame.isVisible()) {
        // trigger camera to capture images
        rs2.trigger();
        // display images -> grab will return the first stream added
        // use rs2.grabDepth(), rs2.grabColor() and rs2.grabIR() for the other streams
        Frame frame = rs2.grab();
        if (frame == null) {
            System.err.println("Frame is null!");
            break;
        }
        // display frame
        canvasFrame.showImage(frame);
    }
    // close realsense camera
    rs2.stop();
    rs2.release();
    canvasFrame.dispose();
}
Also used : RealSense2FrameGrabber(org.bytedeco.javacv.RealSense2FrameGrabber) CanvasFrame(org.bytedeco.javacv.CanvasFrame) Frame(org.bytedeco.javacv.Frame) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) CanvasFrame(org.bytedeco.javacv.CanvasFrame)

Aggregations

MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 CanvasFrame (org.bytedeco.javacv.CanvasFrame)1 Frame (org.bytedeco.javacv.Frame)1 RealSense2FrameGrabber (org.bytedeco.javacv.RealSense2FrameGrabber)1