Skip to content

Commit

Permalink
Move webcam viewer from examples to core, closes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Apr 27, 2013
1 parent f2e9db1 commit a7ba4d4
Showing 1 changed file with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.github.sarxos.webcam;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.lang.Thread.UncaughtExceptionHandler;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Just a simple webcam viewer.
*
* @author Bartosz Firyn (SarXos)
*/
public class WebcamViewer extends JFrame implements Runnable, WebcamListener, WindowListener, UncaughtExceptionHandler, ItemListener {

private static final long serialVersionUID = 1L;

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

private Webcam webcam = null;
private WebcamPanel panel = null;
private WebcamPicker picker = null;

public WebcamViewer() {
SwingUtilities.invokeLater(this);
}

@Override
public void run() {

setTitle("Webcam Capture Viewer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

addWindowListener(this);

picker = new WebcamPicker();
picker.addItemListener(this);

webcam = picker.getSelectedWebcam();

if (webcam == null) {
LOG.error("No webcams found");
System.exit(1);
}

webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(WebcamViewer.this);

panel = new WebcamPanel(webcam, false);
panel.setFPSDisplayed(true);

add(picker, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);

pack();
setVisible(true);

Thread t = new Thread() {

@Override
public void run() {
panel.start();
}
};
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}

@Override
public void webcamOpen(WebcamEvent we) {
LOG.info("Webcam open");
}

@Override
public void webcamClosed(WebcamEvent we) {
LOG.info("Webcam closed");
}

@Override
public void webcamDisposed(WebcamEvent we) {
LOG.info("Webcam disposed");
}

@Override
public void webcamImageObtained(WebcamEvent we) {
// do nothing
}

@Override
public void windowActivated(WindowEvent e) {
}

@Override
public void windowClosed(WindowEvent e) {
webcam.close();
}

@Override
public void windowClosing(WindowEvent e) {
}

@Override
public void windowOpened(WindowEvent e) {
}

@Override
public void windowDeactivated(WindowEvent e) {
}

@Override
public void windowDeiconified(WindowEvent e) {
LOG.info("Webcam viewer resumed");
panel.resume();
}

@Override
public void windowIconified(WindowEvent e) {
LOG.info("Webcam viewer paused");
panel.pause();
}

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
}

@Override
public void itemStateChanged(ItemEvent e) {

if (e.getItem() == webcam) {
return;
}

if (webcam == null) {
return;
}

final WebcamPanel tmp = panel;

remove(panel);

webcam.removeWebcamListener(this);

webcam = (Webcam) e.getItem();
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(this);

System.out.println("selected " + webcam.getName());

panel = new WebcamPanel(webcam, false);

add(panel, BorderLayout.CENTER);

Thread t = new Thread() {

@Override
public void run() {
tmp.stop();
panel.start();
}
};
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();

}
}

0 comments on commit a7ba4d4

Please sign in to comment.