Permalink
Browse files

Applet example

  • Loading branch information...
sarxos committed Nov 18, 2012
1 parent ccc9105 commit 439fd5fd3d516c8980bbeb2a1f8bcc3b428efa4e
View
@@ -2,3 +2,6 @@
target
*/.settings
*/target
+**/*/.settings
+**/*/target
+**/*/dependency-reduced-pom.xml
View
@@ -23,7 +23,6 @@
<module>webcam-capture</module>
<module>webcam-capture-driver-ipcam</module>
<module>webcam-capture-driver-jmf</module>
- <module>webcam-capture-pages</module>
<!-- below ones cannot be released since all of them require external 3rd-party repos -->
<!--
<module>webcam-capture-driver-civil</module>
@@ -246,7 +245,7 @@
<simpleHTMLChangeLogFilename>CHANGELOG.htm</simpleHTMLChangeLogFilename>
<generateHTMLTableOnlyChangeLog>false</generateHTMLTableOnlyChangeLog>
</configuration>
- </plugin>
+ </plugin>
</plugins>
</build>
</profile>
@@ -284,7 +283,7 @@
</configuration>
</execution>
</executions>
- </plugin>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
@@ -0,0 +1,26 @@
+<?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>
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>webcam-capture-example-applet</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.jdt.core.javanature</nature>
+ <nature>org.eclipse.m2e.core.maven2Nature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,78 @@
+# Webcam Capture Example Applet
+
+This example presents how to create Java Applet using Webcam Capture project.
+
+## Generate Java Keystore
+
+Since our Applet will be using users hardware, we have to either sign it, or
+specify appropriate permissions. Without doing this user will get ```SecurityPermissionException```
+when trying to use Applet. This is because Java restrict access to various system elements
+(filesystem, hardware, etc). But when you sign application, Java assume it is safe (since
+you signed it with your personal certificate), and grant access to the system hardware.
+
+Therefore, before you start build you have to generate Java keystore which will be used
+later by Maven to sign Applet JAR. To do that you have to use ```keytool``` program
+available within JDK ```bin``` directory.
+
+```
+$ keytool -genkey -alias example -keyalg RSA -keystore keystore.jks -keysize 2048
+```
+
+After executing this command, ```keytool``` will ask you to provide all required information,
+such as:
+
+* Keystore password,
+* First and last name,
+* Your organizational unit name,
+* Your organization name,
+* City or Locality,
+* State or Province name,
+* Two-letter country code for this unit,
+* Key password for alias (in this case alias is _example_).
+
+In this example, in root directory, I included predefined keystore with alias
+_example_ with password _test1234_.
+
+## How To Build
+
+Like in all my other projects you have to have Maven 3.0 or later available in ```PATH```.
+You can download Maven binaries from **[here](http://maventest.apache.org/download.html)**.
+
+When you have Maven on your PC, it's enough to run this command to build project:
+
+```
+$ mvn clean install
+```
+
+This will caus ```target``` directory to be created. You will find output JAR there together
+with ```index.html``` which should be deployed along with this JAR.
+
+## Where Are All Required JARs?
+
+None is required. That's because I used _maven-shade-plugin_ to merge all JARs together so
+you will get only one JAR with all required classes inside (known as shaded JAR).
+
+For more details I suggest to check ```pom.xml```.
+
+## How To Sign JAR?
+
+You don't have to. Maven will do that for you. This has been done by using _maven-jarsigner-plugin_
+which is using ```keystore.jks``` file to get self-signed certificate and use it to sign the JAR.
+
+## How To Upload Applet To Server?
+
+Use FTP client of your choice and upload these files to the remote site:
+
+* ```webcam-capture-example-applet-[version].jar```
+* ```index.html```
+
+You can also modify ```pom.xml``` and add [maven-upload-plugin](http://docs.atlassian.com/maven-upload-plugin/1.1/usage.html)
+to automatically upload specific files to the server when running ```mvn deploy```.
+
+## How To Run
+
+Simply access remote site URL. Follow the security confirmation dialog and enjoy
+this example.
+
+
+
Binary file not shown.
@@ -0,0 +1,103 @@
+<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-parent</artifactId>
+ <version>0.3.5</version>
+ </parent>
+
+ <artifactId>webcam-capture-example-applet</artifactId>
+ <packaging>jar</packaging>
+
+ <name>Webcam Capture - Applet Example</name>
+ <description>Example of how to use Webcam Capture within Java Applet</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.github.sarxos</groupId>
+ <artifactId>webcam-capture</artifactId>
+ <version>${project.version}</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-resources-plugin</artifactId>
+ <version>2.6</version>
+ <executions>
+ <execution>
+ <phase>prepare-package</phase>
+ <goals>
+ <goal>copy-resources</goal>
+ </goals>
+ <configuration>
+ <outputDirectory>${project.build.directory}</outputDirectory>
+ <overwrite>true</overwrite>
+ <resources>
+ <resource>
+ <directory>src/main/html</directory>
+ <includes>
+ <include>**/*.html</include>
+ </includes>
+ </resource>
+ </resources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>2.0</version>
+ <configuration>
+ <artifactSet>
+ <includes>
+ <include>*:*</include>
+ </includes>
+ </artifactSet>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jarsigner-plugin</artifactId>
+ <configuration>
+ <keystore>keystore.jks</keystore>
+ <alias>example</alias>
+ <storepass>test1234</storepass>
+ <keypass>test1234</keypass>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>sign</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+
+ </build>
+
+</project>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,13 @@
+<html>
+<head>
+ <title>Webcam Capture Example Applet</title>
+</head>
+<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">
+ </applet>
+</body>
+</html>
@@ -0,0 +1,26 @@
+package com.github.sarxos.webcam;
+
+import javax.swing.JApplet;
+
+
+public class WebcamAppletExample extends JApplet {
+
+ private static final long serialVersionUID = 3517366452510566924L;
+
+ private Webcam webcam = null;
+
+ public WebcamAppletExample() {
+ super();
+ }
+
+ @Override
+ public void start() {
+ webcam = Webcam.getDefault();
+ add(new WebcamPanel(webcam));
+ }
+
+ @Override
+ public void destroy() {
+ webcam.close();
+ }
+}
@@ -2,6 +2,7 @@
import java.awt.Graphics;
import java.awt.image.BufferedImage;
+import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JPanel;
@@ -62,7 +63,7 @@ public void run() {
private BufferedImage image = null;
private Repainter repainter = null;
- public WebcamPanel(Webcam webcam) {
+ public WebcamPanel(Webcam webcam, boolean start) {
this.webcam = webcam;
this.webcam.addWebcamListener(this);
@@ -73,12 +74,17 @@ public WebcamPanel(Webcam webcam) {
setPreferredSize(webcam.getViewSize());
- if (repainter == null) {
- repainter = new Repainter();
+ repainter = new Repainter();
+
+ if (start) {
repainter.start();
}
}
+ public WebcamPanel(Webcam webcam) {
+ this(webcam, true);
+ }
+
@Override
protected void paintComponent(Graphics g) {
@@ -114,8 +120,20 @@ public void webcamClosed(WebcamEvent we) {
}
}
+ private AtomicBoolean started = new AtomicBoolean(false);
+
private volatile boolean paused = false;
+ public void start() {
+ if (started.compareAndSet(false, true)) {
+ repainter.start();
+ }
+ }
+
+ public void stop() {
+ webcam.close();
+ }
+
/**
* Pause rendering.
*/
@@ -133,9 +151,6 @@ public void resume() {
if (!paused) {
return;
}
- synchronized (repainter) {
- repainter.notifyAll();
- }
paused = false;
}

0 comments on commit 439fd5f

Please sign in to comment.