1 package kaka.cakelight;
4 import java.util.Optional;
6 import static kaka.cakelight.Main.log;
8 public class FrameGrabber implements Closeable {
9 private Configuration config;
11 private int bytesPerFrame;
12 private InputStream fileStream;
14 private FrameGrabber() {
17 public static FrameGrabber from(File videoDevice, Configuration config) {
18 FrameGrabber fg = new FrameGrabber();
20 fg.file = videoDevice;
21 fg.bytesPerFrame = config.video.width * config.video.height * config.video.bpp;
26 private boolean prepare() {
28 fileStream = new FileInputStream(file);
30 } catch (FileNotFoundException e) {
31 // TODO: handle java.io.FileNotFoundException: /dev/video1 (Permission denied)
38 * Must be run in the same thread as {@link #prepare}.
40 public Optional<VideoFrame> grabFrame() {
42 byte[] data = new byte[bytesPerFrame];
43 int count = fileStream.read(data);
44 log("# of bytes read = " + count);
45 return Optional.of(VideoFrame.of(data, config));
46 } catch (IOException e) {
50 return Optional.empty();
54 public void close() throws IOException {