package com.github.sarxos.webcam.ds.openimaj;

import java.awt.Dimension;
import java.awt.image.BufferedImage;

import org.openimaj.image.ImageUtilities;
import org.openimaj.video.capture.Device;
import org.openimaj.video.capture.VideoCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sarxos.webcam.WebcamDevice;


public class OpenImajDevice implements WebcamDevice {

	private static final Logger LOG = LoggerFactory.getLogger(OpenImajDriver.class);

	/**
	 * Artificial view sizes. I'm really not sure if will fit into other webcams
	 * but hope that OpenIMAJ can handle this.
	 */
	private final static Dimension[] DIMENSIONS = new Dimension[] {
		new Dimension(176, 144),
		new Dimension(320, 240),
		new Dimension(352, 288),
		new Dimension(640, 400),
		new Dimension(640, 480),
		new Dimension(1280, 720),
	};

	private Device device = null;
	private VideoCapture capture = null;
	private Dimension size = null;
	private boolean open = false;

	public OpenImajDevice(Device device) {
		this.device = device;
	}

	@Override
	public String getName() {
		return device.getNameStr();
	}

	@Override
	public Dimension[] getSizes() {
		return DIMENSIONS;
	}

	@Override
	public Dimension getSize() {
		return size;
	}

	@Override
	public void setSize(Dimension size) {
		if (open) {
			throw new RuntimeException("Cannot set new size when device is open, please close it first");
		}
		this.size = size;
	}

	@Override
	public BufferedImage getImage() {

		if (!open) {
			throw new RuntimeException("Cannot get image from closed device");
		}

		// TODO scale to dimension if not equal
		return ImageUtilities.createBufferedImage(capture.getNextFrame());
	}

	@Override
	public void open() {

		if (open) {
			return;
		}

		capture = new VideoCapture(size.width, size.height, device);
		open = true;

		// what the hell is that something below? that's ugly w/a for black
		// images at the very capture beginning, if you have some other idea of
		// how to remove them, please share or fix

		int i = 0;
		do {
			capture.getNextFrame();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		} while (i++ < 3);

		LOG.info("OpenIMAJ webcam device has been initialized");
	}

	@Override
	public void close() {

		if (!open) {
			return;
		}

		capture.stopCapture();

		open = false;

		LOG.info("OpenIMAJ webcam device has been closed");
	}
}

@@ -241,24 +241,82 @@
 eoCapture;%0D%0A
+import org.openimaj.video.capture.VideoCaptureException;%0D%0A
 import org.s
@@ -411,16 +411,66 @@
 evice;%0D%0A
+import com.github.sarxos.webcam.WebcamException;%0D%0A
 %0D%0A%0D%0Apubl
@@ -809,17 +809,16 @@
 on%5B%5D %7B%0D%0A
-%09
 %09new Dim
@@ -832,25 +832,24 @@
 176, 144),%0D%0A
-%09
 %09new Dimensi
@@ -859,25 +859,24 @@
 320, 240),%0D%0A
-%09
 %09new Dimensi
@@ -886,25 +886,24 @@
 352, 288),%0D%0A
-%09
 %09new Dimensi
@@ -913,25 +913,24 @@
 640, 400),%0D%0A
-%09
 %09new Dimensi
@@ -944,17 +944,16 @@
  480),%0D%0A
-%09
 %09new Dim
@@ -1928,32 +1928,42 @@
 return;%0D%0A%09%09%7D%0D%0A%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09capture = new 
@@ -2010,16 +2010,129 @@
 vice);%0D%0A
+%09%09%7D catch (VideoCaptureException e) %7B%0D%0A%09%09%09throw new WebcamException(%22Cannot initialize video capture%22, e);%0D%0A%09%09%7D%0D%0A
 %09%09open =

Fix build failing because of outdated SNAPSHOT