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

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamException;


public class ScreenCaptureDevice implements WebcamDevice {

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

	private final GraphicsDevice device;
	private final DisplayMode mode;
	private final Dimension resolution;
	private final Robot robot;

	private boolean open = false;

	public ScreenCaptureDevice(final GraphicsDevice device) {

		this.device = device;
		this.mode = device.getDisplayMode();
		this.resolution = new Dimension(mode.getWidth(), mode.getHeight());

		try {
			this.robot = new Robot(device);
		} catch (AWTException e) {
			throw new WebcamException("Unable to create robot", e);
		}

		LOG.trace("Screen device {} with resolution {} has been created", getName(), getResolution());
	}

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

	@Override
	public Dimension[] getResolutions() {
		return new Dimension[] { resolution };
	}

	@Override
	public Dimension getResolution() {
		return resolution;
	}

	@Override
	public void setResolution(Dimension size) {
		// do nothings, screen has only one resolution which is already set
	}

	@Override
	public BufferedImage getImage() {
		final GraphicsConfiguration gc = device.getDefaultConfiguration();
		final Rectangle bounds = gc.getBounds();
		return robot.createScreenCapture(bounds);
	}

	@Override
	public void open() {
		LOG.debug("Opening screen device {} with resolution {}", getName(), getResolution());
		open = true;
	}

	@Override
	public void close() {
		open = false;
	}

	@Override
	public void dispose() {
		// do nothing, no need to dispose anything here
	}

	@Override
	public boolean isOpen() {
		return open;
	}
}

@@ -127,16 +127,44 @@
 ayMode;%0A
+import java.awt.Graphics2D;%0A
 import j
@@ -242,25 +242,80 @@
 ava.awt.
-Rectangle
+Image;%0Aimport java.awt.Rectangle;%0Aimport java.awt.RenderingHints
 ;%0Aimport
@@ -1573,76 +1573,66 @@
  %7B%0A%09
-%09// do nothings, screen has only one resolution which is already set
+    resolution.setSize(size.getWidth(), size.getHeight());
 %0A%09%7D%0A
@@ -1792,22 +1792,38 @@
 ds();%0A%09%09
-return
+BufferedImage screen =
  robot.c
@@ -1850,16 +1850,486 @@
 ounds);%0A
+%09%09int width = resolution.width;%0A%09%09int height = resolution.height;%0A%09%09BufferedImage img = new BufferedImage(width, height, screen.getType());%0A%09%09Graphics2D g = img.createGraphics();%0A%09%09g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,%0A%09%09                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);%0A%09%09g.drawImage(screen, 0, 0, width, height,%0A%09%09                    0, 0, screen.getWidth(), screen.getHeight(),%0A%09%09                    null);%0A%09%09g.dispose();%0A%09%09return img;%0A
 %09%7D%0A%0A%09@Ov

Allow resolution adaption of the captured screen