Skip to content

Commit

Permalink
Default driver fetch only one image frame
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
sarxos committed Nov 14, 2012
1 parent c8eb227 commit 2b54c92
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
@@ -1,20 +1,20 @@
package com.github.sarxos.webcam;

import java.awt.Dimension;

import javax.swing.JFrame;


public class WebcamPanelExample {

public static void main(String[] args) {

WebcamPanel panel = new WebcamPanel(Webcam.getDefault());
JFrame jframe = new JFrame("Test webcam panel");
jframe.setPreferredSize(new Dimension(300, 200));
jframe.add(panel);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setFPS(5);

JFrame window = new JFrame("Test webcam panel");
window.add(panel);
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
Expand Up @@ -21,7 +21,7 @@ public class WebcamPanel extends JPanel implements WebcamListener {

private static final Logger LOG = LoggerFactory.getLogger(WebcamPanel.class);

private double frequency = 65; // Hz
private double frequency = 5; // Hz

private class Repainter extends Thread {

Expand All @@ -35,6 +35,8 @@ public void run() {

while (webcam.isOpen()) {

LOG.debug("Read image");

image = webcam.getImage();
if (image == null) {
LOG.error("Image is null");
Expand All @@ -52,6 +54,7 @@ public void run() {
} catch (InterruptedException e) {
e.printStackTrace();
}

repaint();
}
}
Expand All @@ -62,6 +65,7 @@ public void run() {
private Repainter repainter = null;

public WebcamPanel(Webcam webcam) {

this.webcam = webcam;
this.webcam.addWebcamListener(this);

Expand Down
Expand Up @@ -30,13 +30,15 @@ public class DefaultDevice implements WebcamDevice {
* Artificial view sizes. I'm really not sure if will fit into other webcams
* but hope that OpenIMAJ can handle this.
*/
//@formatter:off
private final static Dimension[] DIMENSIONS = new Dimension[] {
new Dimension(176, 144),
new Dimension(320, 240),
new Dimension(352, 288),
new Dimension(640, 400),
new Dimension(640, 480),
};
//@formatter:on

private static final int[] BAND_OFFSETS = new int[] { 0, 1, 2 };
private static final int[] BITS = { 8, 8, 8 };
Expand All @@ -50,7 +52,7 @@ public class DefaultDevice implements WebcamDevice {
private Dimension size = null;
private ComponentSampleModel sampleModel = null;
private ColorModel colorModel = null;
private boolean open = false;
private volatile boolean open = false;

protected DefaultDevice(Device device) {
this.device = device;
Expand Down Expand Up @@ -83,6 +85,8 @@ public BufferedImage getImage() {
throw new WebcamException("Cannot get image when webcam device is not open");
}

grabber.nextFrame();

Pointer<Byte> image = grabber.getImage();
if (image == null) {
return null;
Expand All @@ -93,20 +97,24 @@ public BufferedImage getImage() {

DataBufferByte buffer = new DataBufferByte(data, bytes.length, OFFSET);
WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);
BufferedImage bi = new BufferedImage(colorModel, raster, false, null);

BufferedImage bi = new BufferedImage(colorModel, raster, false, null);
bi.flush();

return bi;
}

@Override
public void open() {
public synchronized void open() {

if (open) {
return;
}

if (size == null) {
size = getSizes()[0];
}

grabber = new OpenIMAJGrabber();

boolean started = grabber.startSession(size.width, size.height, 0, Pointer.pointerTo(device));
Expand All @@ -130,9 +138,11 @@ public void open() {
int i = 0;
do {
grabber.nextFrame();
grabber.getImage();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.error("Nasty interrupted exception", e);
}
} while (i++ < 3);

Expand Down
Expand Up @@ -31,7 +31,7 @@ public class DefaultDriver implements WebcamDriver {
public List<WebcamDevice> getDevices() {

if (grabber == null) {
LOG.debug("Creating grabber");
LOG.debug("Creating grabber for driver");
grabber = new OpenIMAJGrabber();
}

Expand All @@ -55,5 +55,4 @@ public List<WebcamDevice> getDevices() {

return devices;
}

}

0 comments on commit 2b54c92

Please sign in to comment.