Skip to content

Commit

Permalink
Same image data for multiple images obtained from API, fixes #182
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Feb 26, 2014
1 parent b05817e commit 3bcd742
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
2 changes: 1 addition & 1 deletion webcam-capture/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>com.nativelibs4java</groupId>
<artifactId>bridj</artifactId>
<version>0.7-SNAPSHOT</version>
<version>0.6.3-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>dx</artifactId>
Expand Down
95 changes: 95 additions & 0 deletions webcam-capture/src/example/java/TakePicturesAndPlayExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.github.sarxos.webcam.Webcam;


public class TakePicturesAndPlayExample {

private static final class PlayerPanel extends JPanel {

private static final long serialVersionUID = 1L;

private final Vector<BufferedImage> images;
private final Dimension size;
private int offset = 0;

public PlayerPanel(Vector<BufferedImage> images) {
super();
this.images = images;
this.size = new Dimension(images.get(0).getWidth(), images.get(0).getHeight());
setPreferredSize(size);
}

public void play() {
Thread t = new Thread() {

@Override
public void run() {
do {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
return;
}
} while (++offset < images.size());
}
};
t.setDaemon(true);
t.start();
}

@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(images.get(offset), 0, 0, null);
}
}

public static void main(String[] args) {

Webcam w = Webcam.getDefault();
w.open(true);

Vector<BufferedImage> images = new Vector<BufferedImage>();

System.out.println("recording, please wait");

for (int i = 0; i < 100; i++) {
images.add(w.getImage());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
return;
}
}

w.close();

System.out.println("play");

PlayerPanel panel = new PlayerPanel(images);

JFrame f = new JFrame("Take pictures and play example");
f.add(panel);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

panel.play();

try {
Thread.sleep(100 * images.size());
} catch (InterruptedException e) {
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ protected void handle() {
private String id = null;
private String fullname = null;

private byte[] bytes = null;
private byte[][] data = null;

private long t1 = -1;
private long t2 = -1;

Expand Down Expand Up @@ -210,6 +207,9 @@ public BufferedImage getImage() {
return null;
}

byte[] bytes = new byte[size.width * size.height * 3];
byte[][] data = new byte[][] { bytes };

buffer.get(bytes);

DataBufferByte dbuf = new DataBufferByte(data, bytes.length, OFFSET);
Expand Down Expand Up @@ -296,9 +296,6 @@ public void open() {

LOG.debug("Webcam device {} is now open", this);

bytes = new byte[size.width * size.height * 3];
data = new byte[][] { bytes };

open.set(true);

refresher = new Thread(this, String.format("frames-refresher:%s", id));
Expand Down

0 comments on commit 3bcd742

Please sign in to comment.