Skip to content

Commit

Permalink
Progress on the OpenCV capture driver implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jun 18, 2014
1 parent 2f1d3ca commit 44b5b2b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 123 deletions.
53 changes: 27 additions & 26 deletions webcam-capture-drivers/driver-javacv/.classpath
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/example/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
66 changes: 3 additions & 63 deletions webcam-capture-drivers/driver-javacv/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,18 @@
<packaging>jar</packaging>

<name>Webcam Capture - JavaCV Driver</name>
<description>Webcam Capture driver using JavaCV - Java binding for OpenCV framework</description>
<description>Webcam Capture driver using JavaCV binding for OpenCV</description>

<properties>
<javacv.version>0.2</javacv.version>
</properties>

<repositories>
<repository>
<id>javacv</id>
<url>http://maven2.javacv.googlecode.com/git</url>
</repository>
<repository>
<id>kevoree</id>
<url>http://maven.kevoree.org/release</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<classifier>windows-x86</classifier>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<classifier>windows-x86_64</classifier>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<classifier>linux-arm</classifier>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<classifier>linux-x86</classifier>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<classifier>linux-x86_64</classifier>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<classifier>macosx-x86_64</classifier>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.javacv</groupId>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<classifier>android-arm</classifier>
<version>${javacv.version}</version>
<version>0.8</version>
</dependency>
</dependencies>

Expand All @@ -93,9 +36,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import javax.swing.JFrame;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.ds.javacv.JavaCvDriver;


public class JavaCvDriverExample {

static {
Webcam.setDriver(new JavaCvDriver());
}

public static void main(String[] args) {
JFrame frame = new JFrame("LTI-CIVIL Webcam Capture Driver Example");
frame.add(new WebcamPanel(Webcam.getDefault()));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;

import org.bytedeco.javacpp.videoInputLib.videoInput;
import org.bytedeco.javacv.FrameGrabber;

import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamException;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.googlecode.javacv.cpp.videoInputLib.videoInput;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.util.OsUtils;


/**
Expand All @@ -18,6 +21,8 @@
public class JavaCvDevice implements WebcamDevice {

private int address = -1;
private File vfile = null;

private String name = null;
private FrameGrabber grabber = null;

Expand All @@ -28,22 +33,37 @@ public JavaCvDevice(int address) {
this.address = address;
}

public JavaCvDevice(File vfile) {
this.vfile = vfile;
}

@Override
public String getName() {
if (name == null) {
name = videoInput.getDeviceName(address);
switch (OsUtils.getOS()) {
case WIN:
name = videoInput.getDeviceName(address).getString();
break;
case NIX:
name = vfile.getAbsolutePath();
break;
case OSX:
throw new UnsupportedOperationException("Mac OS is not supported");
}
}
return name;
}

@Override
public Dimension[] getResolutions() {
// grabber.get

throw new WebcamException("Not implemented");
}

@Override
public Dimension getResolution() {
throw new WebcamException("Not implemented");
return WebcamResolution.VGA.getSize();
}

@Override
Expand All @@ -65,22 +85,37 @@ public BufferedImage getImage() {
}
}

private FrameGrabber buildGrabber() throws FrameGrabber.Exception {
switch (OsUtils.getOS()) {
case WIN:
return FrameGrabber.createDefault(address);
case NIX:
return FrameGrabber.createDefault(vfile);
case OSX:
default:
throw new UnsupportedOperationException("Current OS is not supported");
}
}

public FrameGrabber getGrabber() {
return grabber;
}

@Override
public void open() {

if (open || disposed) {
return;
}

// CvCapture capture =
// opencv_highgui.cvCreateCameraCapture(opencv_highgui.CV_CAP_DSHOW);
// IplImage image = opencv_highgui.cvQueryFrame(capture);

try {
grabber = FrameGrabber.createDefault(address);

grabber = buildGrabber();
grabber.start();

open = true;
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {

} catch (FrameGrabber.Exception e) {
release();
throw new WebcamException(e);
}
Expand All @@ -90,7 +125,7 @@ private void release() {
if (grabber != null) {
try {
grabber.release();
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
} catch (FrameGrabber.Exception e) {
throw new WebcamException(e);
}
}
Expand All @@ -105,7 +140,7 @@ public void close() {

try {
grabber.stop();
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
} catch (FrameGrabber.Exception e) {
throw new WebcamException(e);
} finally {
dispose();
Expand All @@ -121,4 +156,9 @@ public void dispose() {
public boolean isOpen() {
return open;
}

@Override
public String toString() {
return getClass().getName() + "#address=" + address + "#vfile=" + vfile;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.sarxos.webcam.ds.javacv;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.bytedeco.javacpp.videoInputLib.videoInput;

import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamDriver;
import com.googlecode.javacv.cpp.videoInputLib.videoInput;
import com.github.sarxos.webcam.util.NixVideoDevUtils;


/**
Expand All @@ -21,34 +24,41 @@
*/
public class JavaCvDriver implements WebcamDriver {

private static List<WebcamDevice> devices = null;

@Override
public List<WebcamDevice> getDevices() {
if (devices == null) {
devices = new ArrayList<WebcamDevice>();
int n = videoInput.listDevices();
if (n > 0) {
for (int i = 0; i < n; i++) {
devices.add(new JavaCvDevice(i));
}
} else {
devices = Collections.emptyList();
private List<WebcamDevice> getDevicesWindows() {
List<WebcamDevice> devices = new ArrayList<WebcamDevice>();
int n = videoInput.listDevices();
if (n > 0) {
for (int i = 0; i < n; i++) {
devices.add(new JavaCvDevice(i));
}
} else {
devices = Collections.emptyList();
}
return devices;
}

public static void main(String[] args) {
new JavaCvDriver().getDevices();
private List<WebcamDevice> getDevicesLinux() {
List<WebcamDevice> devices = new ArrayList<WebcamDevice>();
for (File vfile : NixVideoDevUtils.getVideoFiles()) {
devices.add(new JavaCvDevice(vfile));
}
return devices;
}

int n = videoInput.listDevices();
if (n > 0) {
for (int i = 0; i < n; i++) {
System.out.println(videoInput.getDeviceName(i));
}
@Override
public List<WebcamDevice> getDevices() {
boolean linux = System.getProperty("os.name").toLowerCase().indexOf("linux") != -1;
if (linux) {
return getDevicesLinux();
} else {
return getDevicesWindows();
}
}

public static void main(String[] args) {
for (WebcamDevice d : new JavaCvDriver().getDevices()) {
System.out.println(d);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configuration scan="true" scanPeriod="30 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit 44b5b2b

Please sign in to comment.