-
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.
Move webcam viewer from examples to core, closes #89
- Loading branch information
Showing
1 changed file
with
177 additions
and
0 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamViewer.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,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(); | ||
|
||
} | ||
} |