Skip to content

Commit

Permalink
Rework applet example, relax discovery sync, fixes #128, refs #65
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jul 23, 2013
1 parent 8373146 commit f9c5012
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 62 deletions.
54 changes: 28 additions & 26 deletions webcam-capture-examples/webcam-capture-applet/.classpath
@@ -1,26 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/html"/>
<classpathentry kind="src" path="src/etc/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions webcam-capture-examples/webcam-capture-applet/pom.xml
Expand Up @@ -51,6 +51,7 @@
<includes>
<include>**/*.html</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
Expand Down
Expand Up @@ -5,9 +5,9 @@
<body>
<p>This is my page, below you see an Webcam Capture applet</p>
<applet codebase="."
archive="webcam-capture-example-applet-0.3.5.jar"
code="com/github/sarxos/webcam/WebcamAppletExample.class"
width="400" height="400" alt="Applet">
archive="${project.artifactId}-${project.version}.jar"
code="WebcamAppletExample.class"
width="176" height="144" alt="Applet">
</applet>
</body>
</html>
@@ -0,0 +1,89 @@
import java.awt.Dimension;

import javax.swing.JApplet;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver;


public class WebcamAppletExample extends JApplet {

private static final long serialVersionUID = 3517366452510566924L;

private Dimension size = WebcamResolution.QQVGA.getSize();
private Webcam webcam = null;
private WebcamPanel panel = null;

public WebcamAppletExample() {
super();
System.out.println("Construct");
}

@Override
public void start() {

System.out.println("Start");

super.start();

try {
Webcam.setDriver(new WebcamDefaultDriver());
} catch (Exception e) {
e.printStackTrace();
}

webcam = Webcam.getDefault();
webcam.setViewSize(size);

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

add(panel);

if (webcam.isOpen()) {
webcam.close();
}

int i = 0;
do {
if (webcam.getLock().isLocked()) {
System.out.println("Waiting for lock to be released " + i);
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
return;
}
} else {
break;
}
} while (i++ < 3);

webcam.open();
panel.start();
}

@Override
public void destroy() {
System.out.println("Destroy");
super.destroy();
webcam.close();
webcam.close();
}

@Override
public void stop() {
System.out.println("Stop");
super.stop();
webcam.close();
webcam.getLock().unlock();
panel.stop();
}

@Override
public void init() {
System.out.println("Init");
super.init();
}
}

This file was deleted.

Expand Up @@ -870,7 +870,7 @@ public static synchronized WebcamDriver getDriver() {
* @param wd new webcam driver to be used (e.g. LtiCivil, JFM, FMJ, QTJ)
* @throws IllegalArgumentException when argument is null
*/
public static synchronized void setDriver(WebcamDriver wd) {
public static void setDriver(WebcamDriver wd) {

if (wd == null) {
throw new IllegalArgumentException("Webcam driver cannot be null!");
Expand All @@ -893,7 +893,7 @@ public static synchronized void setDriver(WebcamDriver wd) {
* @param driverClass new video driver class to use
* @throws IllegalArgumentException when argument is null
*/
public static synchronized void setDriver(Class<? extends WebcamDriver> driverClass) {
public static void setDriver(Class<? extends WebcamDriver> driverClass) {

if (driverClass == null) {
throw new IllegalArgumentException("Webcam driver class cannot be null!");
Expand All @@ -915,9 +915,11 @@ public static synchronized void setDriver(Class<? extends WebcamDriver> driverCl
* <br>
* <b>This method is not thread-safe!</b>
*/
public static synchronized void resetDriver() {
public static void resetDriver() {

DRIVERS_LIST.clear();
synchronized (DRIVERS_LIST) {
DRIVERS_LIST.clear();
}

if (discovery != null) {
discovery.shutdown();
Expand Down
Expand Up @@ -84,7 +84,7 @@ private static List<WebcamDevice> getDevices(List<Webcam> webcams) {
return devices;
}

public synchronized List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException {
public List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException {

if (timeout < 0) {
throw new IllegalArgumentException("Timeout cannot be negative");
Expand Down Expand Up @@ -238,10 +238,11 @@ public void run() {
// discovered

Object monitor = new Object();

int i = 0;
do {

synchronized (monitor) {
System.out.println("in monitor wait " + i++);
try {
monitor.wait(support.getScanInterval());
} catch (InterruptedException e) {
Expand All @@ -257,6 +258,8 @@ public void run() {
scan();

} while (running);

LOG.debug("Webcam discovery service loop has been stopped");
}

private void setCurrentWebcams(List<WebcamDevice> devices) {
Expand Down Expand Up @@ -306,6 +309,8 @@ public synchronized void stop() {
throw new WebcamException("Joint interrupted");
}

LOG.debug("Discovery service has been stopped");

runner = null;
}

Expand Down
Expand Up @@ -30,7 +30,7 @@ public class WebcamLock {
/**
* Update interval (ms).
*/
private static final long INTERVAL = 2000;
public static final long INTERVAL = 2000;

/**
* Used to update lock state.
Expand Down

0 comments on commit f9c5012

Please sign in to comment.