Skip to content

List video capture devices and frame sizes from Java, for use in OpenCV

License

Notifications You must be signed in to change notification settings

regwhitton/video-capture-inventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Video Capture Inventory

Build

Video Capture Inventory provides a Java API and native shared libraries to give an inventory of the video capture devices attached to the local machine and the frame sizes supported by each device.

OpenCV and the missing device information

OpenCV is a great toolkit for manipulating video. Among many other things it allows the developer to open any VideoCapture device, set the frame size to one supported by the device and grab frames.

You might do it like this:

VideoCapture videoCapture = new VideoCapture();
int camera = 0;
if (videoCapture.open(camera)) {
    videoCapture.set(CAP_PROP_FRAME_WIDTH, 1920);
    videoCapture.set(CAP_PROP_FRAME_HEIGHT, 1080);

    Mat mat = new Mat();
    videoCapture.read(mat);

    ... do something with the Mat image
}

Unfortunately, OpenCV does not provide a mechanism for finding out which devices or frame sizes are available. Video Capture Inventory aims to provide this extra information.

You might use it like this:

VideoCaptureInventory vci = VideoCaptureInventory.get();
for (Device d : vci.getDevices()) {
    printf("Camera: %s\n", d.getName());
    ...
    if (videoCapture.open(d.getDeviceId())) {
    ...
    for (Format f : d.getFormats()) {
        if (f instanceof DiscreteFormat) {
            DiscreteFormat df = (DiscreteFormat) f;
            videoCapture.set(CAP_PROP_FRAME_WIDTH, df.getWidth());
            videoCapture.set(CAP_PROP_FRAME_HEIGHT, df.getHeight());
            ...
            
        } else {
            // Linux only, where some cameras (RaspberryPi Camera module)
            // support ranges of sizes.
            StepwiseFormat sf = (StepwiseFormat) f;
            ... sf.getMinWidth(), sf.getMaxWidth(), sf.getStepWidth() ...
            ... sf.getMinHeight(), sf.getMaxHeight(), sf.getStepHeight() ...
        }
    }
}

For examples of use see:

  • Simple - list the devices and frame size to the console.
  • OpenCV - use OpenCV to grab an image from every device, at every frame size.

Maven

Add the follow to your pom.xml to get the Java API and only the 64bit Windows native shared library:

<dependencies>
    <dependency>
        <groupId>com.github.regwhitton</groupId>
        <artifactId>video-capture-inventory</artifactId>
        <version>0.1.1</version>
        <classifier/>
    </dependency>
    <dependency>
        <groupId>com.github.regwhitton</groupId>
        <artifactId>video-capture-inventory</artifactId>
        <version>0.1.1</version>
        <classifier>windows-x86_64</classifier>
    </dependency>
</dependencies>
...
<repositories>
    <repository>
        <id>github</id>
        <name>GitHub regwhitton Apache Maven Packages</name>
        <url>https://maven.pkg.github.com/regwhitton/video-capture-inventory</url>
    </repository>
</repositories>

You need a Github account and a personal access token to build against Github's Maven Repo. See Creating a personal access token for the command line

Edit your ~/.m2/settings.xml and add a server entry.

<settings ...>
    ...
    <servers>
        <server>
            <id>github</id>
            <username>your_github_username</username>
            <password>your_github_access_token</password>
        </server>
    </servers>
    ...
</settings>

Alternatively, you can go to the packages tab on Github, download the jars and install them into your local repo.

Maven Classifiers

Platform Classifier Target Tested On
windows-x86_64 Windows Vista and later (64bit Intel and AMD) Dell XPS 13 9370 Windows 10
linux-x86_64 Linux (64bit Intel and AMD)
linux-armhf Raspberry Pi (32bit ARM) Raspberry Pi 1 & 3

These classifiers are intended to align with those given to the OpenCV native shared libraries by Javacpp-Presets.

Windows and Linux x86_64 are available from the Github maven repo. Raspberry Pi may need to be built manually. (See Raspberry Pi section below).

You might need to know

Currently Video Capture Inventory:

  • Ignores scanners and still image devices.
  • Ignores duplicate frame sizes for different colour depths.
  • Requires Java 8+.

Raspberry Pi

Cross-compiling for Raspberry Pi has proved difficult. However, the sources do build and work on a Pi. Assuming you have a Java 8+ JDK and Maven 3.6+ installed, then you can build from the root source directory with:

mvn install

Then in the example source directories:

mvn verify

The Javacpp-Presets OpenCV linux-armhf library targets the Raspberry Pi, so is unlikely to work on other armhf builds of Linux as Debian.

March 2020: there are currently issues using OpenCV with USB cameras on the current version of Raspbian. See OpenCV example

Changes

From 0.1.0 to 0.1.1

  • Corrected device id provided on Windows when multiple cameras attached (previously did not always match OpenCV).
  • Added support for Linux and stepwise formats (API breaking change). Tested mainly on Raspberry Pi.
  • Applied Apache License.
  • Moved copy of Adam Heinrichi's native-utils to separate repo.
  • Elaborated README.md

License

Apache License, Version 2.0