Skip to content

Commit

Permalink
JavaCV webcam device (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jul 28, 2012
1 parent dd20f83 commit fd83116
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 8 deletions.
3 changes: 0 additions & 3 deletions webcam-capture-driver-javacv/.classpath
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
Expand Down
52 changes: 51 additions & 1 deletion webcam-capture-driver-javacv/pom.xml
Expand Up @@ -12,11 +12,19 @@
<name>Webcam Capture - JavaCV Driver</name>
<description>Webcam Capture driver using JavaCV - Java binding for OpenCV framework</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>
Expand All @@ -28,7 +36,49 @@
<dependency>
<groupId>com.googlecode.javacv</groupId>
<artifactId>javacv</artifactId>
<version>0.2</version>
<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>
<artifactId>javacv</artifactId>
<classifier>android-arm</classifier>
<version>${javacv.version}</version>
</dependency>
</dependencies>

Expand Down
@@ -0,0 +1,112 @@
package com.github.sarxos.webcam.ds.javacv;

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

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;


/**
* UNSTABLE, EXPERIMENTALL STUFF !!!
*
* @author bfiryn
*/
public class JavaCvDevice implements WebcamDevice {

private int address = -1;
private String name = null;
private boolean open = false;
private FrameGrabber grabber = null;

public JavaCvDevice(int address) {
this.address = address;
}

@Override
public String getName() {
if (name == null) {
name = videoInput.getDeviceName(address);
}
return name;
}

@Override
public Dimension[] getSizes() {
throw new WebcamException("Not implemented");
}

@Override
public Dimension getSize() {
throw new WebcamException("Not implemented");
}

@Override
public void setSize(Dimension size) {
throw new WebcamException("Not implemented");
}

@Override
public BufferedImage getImage() {

if (!open) {
throw new WebcamException("Cannot grab image - webcam device is not open");
}

try {
return grabber.grab().getBufferedImage();
} catch (Exception e) {
throw new WebcamException("Cannot grab image...");
}
}

@Override
public void open() {

if (open) {
return;
}

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

try {
grabber = FrameGrabber.createDefault(address);
grabber.start();
open = true;
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
dispose();
throw new WebcamException(e);
}
}

private void dispose() {
if (grabber != null) {
try {
grabber.release();
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
throw new WebcamException(e);
}
}
}

@Override
public void close() {

if (!open) {
return;
}

try {
grabber.stop();
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
throw new WebcamException(e);
} finally {
dispose();
}
}
}
@@ -1,17 +1,54 @@
package com.github.sarxos.webcam.ds.javacv;

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

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


/**
* Webcam driver using JavaCV interface to OpenCV. OpenCV (Open Source Computer
* Vision Library) is library of programming functions for real time computer
* vision. JavaCV provides wrappers to commonly used libraries for OpenCV and
* few others.
*
* UNSTABLE, EXPERIMENTALL STUFF !!!
*
* @author Bartosz Firyn (SarXos)
*/
public class JavaCvDriver implements WebcamDriver {

private static List<WebcamDevice> devices = null;

@Override
public List<WebcamDevice> getDevices() {
// TODO Auto-generated method stub
return null;
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();
}
}
return devices;
}

public static void main(String[] args) {
new JavaCvDriver().getDevices();

int n = videoInput.listDevices();
if (n > 0) {
for (int i = 0; i < n; i++) {
System.out.println(videoInput.getDeviceName(i));
}
}

}

}
Expand Up @@ -111,5 +111,4 @@ public void close() {

LOG.info("OpenIMAJ webcam device has been closed");
}

}
2 changes: 1 addition & 1 deletion webcam-capture-driver-vlcj/pom.xml
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>webcam-capture-driver-vlcj</artifactId>
<name>Webcam Capture - VLCJ Driver</name>
<name>Webcam Capture - VLCj Driver</name>
<description>Webcam Capture driver using vlcj framework to grab frames from camera</description>

<repositories>
Expand Down
@@ -0,0 +1,27 @@
package com.github.sarxos.webcam.util;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;


public class ImageUtils {

public static BufferedImage premultiple(BufferedImage src) {
BufferedImage pre = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = pre.createGraphics();
g2.drawImage(src, 0, 0, null);
g2.dispose();
pre.flush();
return pre;
}

public static BufferedImage unpremultiple(BufferedImage pre) {
BufferedImage src = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = pre.createGraphics();
g2.drawImage(src, 0, 0, null);
g2.dispose();
src.flush();
return src;
}

}

0 comments on commit fd83116

Please sign in to comment.