-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
230 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...-capture-driver-javacv/src/main/java/com/github/sarxos/webcam/ds/javacv/JavaCvDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
41 changes: 39 additions & 2 deletions
41
...-capture-driver-javacv/src/main/java/com/github/sarxos/webcam/ds/javacv/JavaCvDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,5 +111,4 @@ public void close() { | |
|
||
LOG.info("OpenIMAJ webcam device has been closed"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
webcam-capture/src/main/java/com/github/sarxos/webcam/util/ImageUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |