Skip to content

Commit

Permalink
Add example for how to use webcam from Akka
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Aug 8, 2017
1 parent 5b985bf commit fb581d3
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
3 changes: 2 additions & 1 deletion webcam-capture-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<module>webcam-capture-swt-awt</module>
<module>webcam-capture-video-recording</module>
<module>webcam-capture-onejar</module>
<module>webcam-capture-websockets</module>
<module>webcam-capture-websockets</module>
<module>webcam-capture-akka</module>
</modules>

<build>
Expand Down
62 changes: 62 additions & 0 deletions webcam-capture-examples/webcam-capture-akka/pom.xml
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>
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();
}
}

0 comments on commit fb581d3

Please sign in to comment.