Skip to content

Latest commit

 

History

History
 
 

depthai

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

JavaCPP Presets for DepthAI

Gitter Maven Central Sonatype Nexus (Snapshots)
Build status for all platforms: depthai Commercial support: xscode

Introduction

This directory contains the JavaCPP Presets module for:

Please refer to the parent README.md file for more detailed information about the JavaCPP Presets.

Documentation

Java API documentation is available here:

Sample Usage

Here is a simple example of DepthAI ported to Java from this C++ source file:

We can use Maven 3 to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the pom.xml and CameraPreviewExample.java source files below, simply execute on the command line:

 $ mvn compile exec:java

The pom.xml build file

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bytedeco.depthai</groupId>
    <artifactId>examples</artifactId>
    <version>1.5.9-SNAPSHOT</version>
    <properties>
        <exec.mainClass>CameraPreviewExample</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>depthai-platform</artifactId>
            <version>2.19.1-1.5.9-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>opencv-platform</artifactId>
            <version>4.6.0-1.5.9-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>.</sourceDirectory>
    </build>
</project>

The CameraPreviewExample.java source file

// Inludes common necessary includes for development using depthai library
import org.bytedeco.javacpp.*;
import org.bytedeco.depthai.*;
import org.bytedeco.depthai.Device;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_highgui.*;
import static org.bytedeco.depthai.global.depthai.*;
import static org.bytedeco.opencv.global.opencv_core.*;
import static org.bytedeco.opencv.global.opencv_highgui.*;

public class CameraPreviewExample {
    static Pipeline createCameraPipeline() {
        Pipeline p = new Pipeline();

        ColorCamera colorCam = p.createColorCamera();
        XLinkOut xlinkOut = p.createXLinkOut();
        xlinkOut.setStreamName("preview");

        colorCam.setPreviewSize(300, 300);
        colorCam.setResolution(ColorCameraProperties.SensorResolution.THE_1080_P);
        colorCam.setInterleaved(true);

        // Link plugins CAM -> XLINK
        colorCam.preview().link(xlinkOut.input());

        return p;
    }

    public static void main(String[] args) {
        Pipeline p = createCameraPipeline();

        // Start the pipeline
        Device d = new Device();

        System.out.print("Connected cameras: ");
        IntPointer cameras = d.getConnectedCameras();
        for (int i = 0; i < cameras.limit(); i++) {
            System.out.print(cameras.get(i) + " ");
        }
        System.out.println();

        // Start the pipeline
        d.startPipeline(p);

        Mat frame;
        DataOutputQueue preview = d.getOutputQueue("preview");

        while (true) {
            ImgFrame imgFrame = preview.getImgFrame();
            if (imgFrame != null) {
                System.out.printf("Frame - w: %d, h: %d\n", imgFrame.getWidth(), imgFrame.getHeight());
                frame = new Mat(imgFrame.getHeight(), imgFrame.getWidth(), CV_8UC3, imgFrame.getData());
                imshow("preview", frame);
                int key = waitKey(1);
                if (key == 'q') {
                    System.exit(0);
                }
            } else {
                System.out.println("Not ImgFrame");
            }
        }
    }
}