-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Application.java
75 lines (56 loc) · 1.81 KB
/
Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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();
}
}