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) {
37 * Must be run in the same thread as {@link #prepare}.
39 public Optional<Frame> grabFrame() {
41 byte[] data = new byte[bytesPerFrame];
42 int count = fileStream.read(data);
43 log("# of bytes read = " + count);
44 return Optional.of(Frame.of(data, config));
45 } catch (IOException e) {
49 return Optional.empty();
53 public void close() throws IOException {