Skip to content

Commit

Permalink
Grabber initialization failure in multithreaded environment
Browse files Browse the repository at this point in the history
fixes #4

a
  • Loading branch information
sarxos committed Nov 21, 2012
1 parent 8b8d86c commit ec025a9
Show file tree
Hide file tree
Showing 22 changed files with 616 additions and 58 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Expand Up @@ -313,6 +313,19 @@
<ignore />
</action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<versionRange>[1.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
Expand Down
27 changes: 27 additions & 0 deletions webcam-capture-examples/webcam-capture-executable/.classpath
@@ -0,0 +1,27 @@
<?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/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>
23 changes: 23 additions & 0 deletions webcam-capture-examples/webcam-capture-executable/.project
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>webcam-capture-example-executable</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
131 changes: 131 additions & 0 deletions webcam-capture-examples/webcam-capture-executable/pom.xml
@@ -0,0 +1,131 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture-examples</artifactId>
<version>0.3.5</version>
</parent>

<artifactId>webcam-capture-example-executable</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jclarion</groupId>
<artifactId>image4j</artifactId>
<version>0.7</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-libs</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<id>l4j-gui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<outfile>${project.build.directory}/capture.exe</outfile>
<jar>${project.build.directory}/${project.artifactId}-${project.version}.jar</jar>
<errTitle>Error</errTitle>
<icon>src/main/resources/Security-Camera.ico</icon>
<classPath>
<mainClass>com.github.sarxos.webcam.WebcamExecutableExample</mainClass>
<addDependencies>true</addDependencies>
<jarLocation>libs</jarLocation>
</classPath>
<jre>
<minVersion>1.5.0</minVersion>
<maxVersion>1.6.0</maxVersion>
</jre>
<singleInstance>
<mutexName>WebcamCapture</mutexName>
<windowTitle>${project.name}</windowTitle>
</singleInstance>
<versionInfo>
<fileVersion>1.2.3.4</fileVersion>
<txtFileVersion>v1.2.3.4</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<copyright>Bartosz Firyn</copyright>
<productVersion>1.2.3.4</productVersion>
<txtProductVersion>v1.2.3.4</txtProductVersion>
<productName>${project.name}</productName>
<internalName>${project.name}</internalName>
<originalFilename>capture.exe</originalFilename>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.5.1,)</versionRange>
<goals>
<goal>
copy-dependencies
</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@@ -0,0 +1,85 @@
package com.github.sarxos.webcam;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

import net.sf.image4j.codec.ico.ICODecoder;


public class WebcamExecutableExample extends JFrame implements ActionListener {

private static final long serialVersionUID = -1368783325310232511L;

private Executor executor = Executors.newSingleThreadExecutor();
private AtomicBoolean initialized = new AtomicBoolean(false);
private Webcam webcam = null;
private WebcamPanel panel = null;
private JButton button = null;

public WebcamExecutableExample() {
super();

setTitle("Webcam Executable Example");
setLayout(new FlowLayout(FlowLayout.CENTER));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

webcam = Webcam.getDefault();
webcam.setViewSize(webcam.getViewSizes()[0]);

panel = new WebcamPanel(webcam, false);
panel.setPreferredSize(webcam.getViewSize());
panel.setOpaque(true);
panel.setBackground(Color.BLACK);

ImageIcon icon = null;
try {
List<BufferedImage> icons = ICODecoder.read(getClass().getResourceAsStream("/Security-Camera.ico"));
icon = new ImageIcon(icons.get(1));
} catch (IOException e) {
e.printStackTrace();
}

button = new JButton(icon);
button.addActionListener(this);
button.setFocusable(false);
button.setPreferredSize(webcam.getViewSize());

add(panel);
add(button);

pack();
setVisible(true);
}

boolean running = false;

public static void main(String[] args) throws IOException {
new WebcamExecutableExample();
}

@Override
public void actionPerformed(ActionEvent e) {
if (initialized.compareAndSet(false, true)) {
executor.execute(new Runnable() {

@Override
public void run() {
panel.start();
}
});
}
}

}
Binary file not shown.
@@ -0,0 +1,6 @@
Icon from http://www.iconarchive.com/show/vista-hardware-devices-icons-by-icons-land/Security-Camera-icon.html

Artist: Icons-Land (Available for custom work)
Iconset: Vista Hardware Devices Icons (28 icons)
License: Free for non-commercial use.
Commercial usage: Not allowed
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions webcam-capture-pages/.classpath
@@ -1,6 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/*" kind="src" path="src/site"/>
<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"/>
Expand Down
2 changes: 1 addition & 1 deletion webcam-capture/src/example/resources/logback.xml
Expand Up @@ -6,7 +6,7 @@
</layout>
</appender>

<root level="error">
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Expand Up @@ -34,6 +34,10 @@ public Repainter() {
public void run() {
super.run();

if (!webcam.isOpen()) {
webcam.open();
}

while (webcam.isOpen()) {

image = webcam.getImage();
Expand Down Expand Up @@ -68,8 +72,10 @@ public WebcamPanel(Webcam webcam, boolean start) {
this.webcam = webcam;
this.webcam.addWebcamListener(this);

if (!webcam.isOpen()) {
webcam.open();
if (start) {
if (!webcam.isOpen()) {
webcam.open();
}
}

setPreferredSize(webcam.getViewSize());
Expand All @@ -91,6 +97,9 @@ protected void paintComponent(Graphics g) {
super.paintComponent(g);

if (image == null) {
g.setColor(getForeground());
g.drawLine(0, 0, getWidth(), getHeight());
g.drawLine(0, getHeight(), getWidth(), 0);
return;
}

Expand Down Expand Up @@ -126,6 +135,7 @@ public void webcamClosed(WebcamEvent we) {

public void start() {
if (started.compareAndSet(false, true)) {
webcam.open();
repainter.start();
}
}
Expand Down

0 comments on commit ec025a9

Please sign in to comment.