Skip to content

Commit

Permalink
Small changes in the examples [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 15, 2013
1 parent 58036d7 commit 696bf68
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 53 deletions.
31 changes: 0 additions & 31 deletions webcam-capture/src/example/java/TakePictureFromTwoCamsExample.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import com.github.sarxos.webcam.Webcam;


/**
* @author Bartosz Firyn (SarXos)
*/
public class TakeSnapshotFromAllWebcamsExample {

public static void main(String[] args) throws IOException {

List<Webcam> webcams = Webcam.getWebcams();

// NOTE!
/*
* Yes, I know we could do this in one loop, but I wanted to prove here
* that it's possible to have many native webcams open in the same time.
* I tested this example with 4 webcams simultaneously connected to the
* USB bus - 1 x PC embedded device, and 3 x UVC devices connected to
* the USB concentration hub, which was connected to the USB 2.0 port.
* It's working like a charm.
*/

// USB BANDWIDTH!
/*
* As you probably know the USB has limited bandwidth and therefore it
* may not be possible to transfer images from as many cameras as you
* would like to wish. This example works when I'm using QQVGA (176x144)
* but fails with the error message when I want to fetch VGA (640x480).
*/

// open all at once (this is the most time-consuming operation, all
// others are executed instantly)
for (Webcam webcam : webcams) {
System.out.format("Opening %s\n", webcam.getName());
webcam.open();
}

// capture picture from all of them
for (int i = 0; i < webcams.size(); i++) {
Webcam webcam = webcams.get(i);
System.out.format("Capturing %s\n", webcam.getName());
ImageIO.write(webcam.getImage(), "PNG", new File(String.format("test-%d.png", i)));
}

// close all
for (Webcam webcam : webcams) {
System.out.format("Closing %s\n", webcam.getName());
webcam.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

Expand All @@ -17,7 +19,7 @@


@SuppressWarnings("serial")
public class TakeSnaphotFromVideoExample extends JFrame {
public class TakeSnapshotFromVideoExample extends JFrame {

private class SnapMeAction extends AbstractAction {

Expand All @@ -28,9 +30,12 @@ public SnapMeAction() {
@Override
public void actionPerformed(ActionEvent e) {
try {
File file = new File(String.format("test-%d.jpg", System.currentTimeMillis()));
ImageIO.write(webcam.getImage(), "JPG", file);
System.out.println("Image saved in " + file.getAbsolutePath());
for (int i = 0; i < webcams.size(); i++) {
Webcam webcam = webcams.get(i);
File file = new File(String.format("test-%d.jpg", i));
ImageIO.write(webcam.getImage(), "JPG", file);
System.out.format("Image for %s saved in %s \n", webcam.getName(), file);
}
} catch (IOException e1) {
e1.printStackTrace();
}
Expand Down Expand Up @@ -59,37 +64,45 @@ public void actionPerformed(ActionEvent e) {

@Override
public void run() {
panel.start();
for (WebcamPanel panel : panels) {
panel.start();
}
}
}

private Executor executor = Executors.newSingleThreadExecutor();

private Dimension captureSize = WebcamResolution.VGA.getSize();
private Dimension displaySize = WebcamResolution.QQVGA.getSize();
private Dimension size = WebcamResolution.QQVGA.getSize();

private Webcam webcam = Webcam.getDefault();
private WebcamPanel panel = new WebcamPanel(webcam, displaySize, false);
private List<Webcam> webcams = Webcam.getWebcams();
private List<WebcamPanel> panels = new ArrayList<WebcamPanel>();

private JButton btSnapMe = new JButton(new SnapMeAction());
private JButton btStart = new JButton(new StartAction());

public TakeSnaphotFromVideoExample() {
public TakeSnapshotFromVideoExample() {

super("Test Snap Different Size");

webcam.setViewSize(captureSize);

panel.setFPSDisplayed(true);
panel.setFillArea(true);
for (Webcam webcam : webcams) {
webcam.setViewSize(size);
WebcamPanel panel = new WebcamPanel(webcam, size, false);
panel.setFPSDisplayed(true);
panel.setFillArea(true);
panels.add(panel);
}

// start application with disable snapshot button - we enable it when
// webcam is started

btSnapMe.setEnabled(false);

setLayout(new FlowLayout());
add(panel);

for (WebcamPanel panel : panels) {
add(panel);
}

add(btSnapMe);
add(btStart);

Expand All @@ -99,6 +112,6 @@ public TakeSnaphotFromVideoExample() {
}

public static void main(String[] args) {
new TakeSnaphotFromVideoExample();
new TakeSnapshotFromVideoExample();
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@


import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDiscoveryEvent;
import com.github.sarxos.webcam.WebcamDiscoveryListener;


public class WebcamDiscoveryListenerExample implements WebcamDiscoveryListener {

public WebcamDiscoveryListenerExample() {
for (Webcam webcam : Webcam.getWebcams()) {
System.out.println("This webcam has been found in the system: " + webcam.getName());
System.out.println("Webcam detected: " + webcam.getName());
}
Webcam.addDiscoveryListener(this);
System.out.println("Now, please connect additional webcam, or disconnect already connected one.");
System.out.println("\n\nPlease connect additional webcams, or disconnect already connected ones. Listening for events...");
}

@Override
public void webcamFound(WebcamDiscoveryEvent event) {
System.out.println("Oh! Thou, webcam has been connected! " + event.getWebcam().getName());
System.out.format("Webcam connected: %s \n", event.getWebcam().getName());
}

@Override
public void webcamGone(WebcamDiscoveryEvent event) {
System.out.println("Did I miss something? Webcam has been disconnected! " + event.getWebcam().getName());
System.out.format("Webcam disconnected: %s \n", event.getWebcam().getName());
}

public static void main(String[] args) throws Throwable {
Expand Down

1 comment on commit 696bf68

@praveshgupta1993
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Sarxos,
First of all thumbs up to your work !
I am a student working on same thing.
I want to contact to you please provide me your email-address.
Thanks in advance !

Please sign in to comment.