-
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.
Add example for how to use webcam from Akka
- Loading branch information
Showing
3 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
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
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,62 @@ | ||
<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> | ||
<groupId>com.github.sarxos</groupId> | ||
<artifactId>webcam-capture-example-akka</artifactId> | ||
<packaging>jar</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.sarxos</groupId> | ||
<artifactId>webcam-capture</artifactId> | ||
<version>0.3.11</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.javaslang</groupId> | ||
<artifactId>javaslang</artifactId> | ||
<version>2.0.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-cluster-tools_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-cluster-sharding_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-actor_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-cluster_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-persistence_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-distributed-data-experimental_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-slf4j_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.akka</groupId> | ||
<artifactId>akka-slf4j_2.11</artifactId> | ||
<version>2.4.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
75 changes: 75 additions & 0 deletions
75
webcam-capture-examples/webcam-capture-akka/src/main/java/Application.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,75 @@ | ||
import static akka.pattern.Patterns.ask; | ||
import static scala.concurrent.Await.result; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.swing.JOptionPane; | ||
|
||
import scala.concurrent.duration.Duration; | ||
import scala.concurrent.duration.FiniteDuration; | ||
import akka.actor.ActorRef; | ||
import akka.actor.ActorSystem; | ||
import akka.actor.Props; | ||
import akka.actor.UntypedActor; | ||
|
||
import com.github.sarxos.webcam.Webcam; | ||
import com.github.sarxos.webcam.WebcamResolution; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
public class Application { | ||
|
||
static enum GetImageMsg { | ||
OBJECT; | ||
} | ||
|
||
static class WebcamActor extends UntypedActor { | ||
|
||
final Webcam webcam; | ||
|
||
public WebcamActor(Webcam webcam) { | ||
this.webcam = webcam; | ||
} | ||
|
||
@Override | ||
public void preStart() throws Exception { | ||
webcam.setViewSize(WebcamResolution.VGA.getSize()); | ||
webcam.open(); | ||
} | ||
|
||
@Override | ||
public void postStop() throws Exception { | ||
webcam.close(); | ||
} | ||
|
||
@Override | ||
public void onReceive(Object msg) throws Exception { | ||
if (msg instanceof GetImageMsg) { | ||
sender().tell(getImage(), self()); | ||
} else { | ||
unhandled(msg); | ||
} | ||
} | ||
|
||
public BufferedImage getImage() { | ||
return webcam.getImage(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
final ActorSystem system = ActorSystem.create("hello-from-akka", ConfigFactory.defaultApplication()); | ||
final ActorRef ref = system.actorOf(Props.create(WebcamActor.class, Webcam.getDefault())); | ||
|
||
final File file = new File("test.jpg"); | ||
final Duration timeout = FiniteDuration.create("10s"); | ||
final BufferedImage image = (BufferedImage) result(ask(ref, GetImageMsg.OBJECT, timeout.toMillis()), timeout); | ||
|
||
ImageIO.write(image, "JPG", file); | ||
|
||
JOptionPane.showMessageDialog(null, "Image has been saved in file: " + file); | ||
|
||
system.terminate(); | ||
} | ||
} |