From 0e0ccae252ea5b45479abf1c36d73c76a999c792 Mon Sep 17 00:00:00 2001 From: Bartosz Firyn Date: Fri, 29 Sep 2017 16:59:29 +0200 Subject: [PATCH] Add screen capture driver --- .../driver-screencapture/pom.xml | 44 +++++++++ .../src/example/java/WebcamPanelExample.java | 39 ++++++++ .../java/WebcamPanelSubViewExample.java | 79 ++++++++++++++++ .../src/example/resources/logback.xml | 10 ++ .../ds/gstreamer/ScreenCaptureDevice.java | 92 +++++++++++++++++++ .../ds/gstreamer/ScreenCaptureDriver.java | 32 +++++++ webcam-capture-drivers/pom.xml | 1 + 7 files changed, 297 insertions(+) create mode 100644 webcam-capture-drivers/driver-screencapture/pom.xml create mode 100644 webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelExample.java create mode 100644 webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelSubViewExample.java create mode 100644 webcam-capture-drivers/driver-screencapture/src/example/resources/logback.xml create mode 100644 webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDevice.java create mode 100644 webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDriver.java diff --git a/webcam-capture-drivers/driver-screencapture/pom.xml b/webcam-capture-drivers/driver-screencapture/pom.xml new file mode 100644 index 00000000..b04f128f --- /dev/null +++ b/webcam-capture-drivers/driver-screencapture/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + + com.github.sarxos + webcam-capture-drivers + 0.3.12-SNAPSHOT + + + webcam-capture-driver-screencapture + jar + + Webcam Capture - Screen Capture Driver + Webcam Capture driver for capturing images from screen devices + + + + com.github.sarxos + webcam-capture + ${project.version} + + + ch.qos.logback + logback-classic + provided + + + junit + junit + test + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + diff --git a/webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelExample.java b/webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelExample.java new file mode 100644 index 00000000..5ebe630f --- /dev/null +++ b/webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelExample.java @@ -0,0 +1,39 @@ +import java.awt.Dimension; +import java.awt.FlowLayout; + +import javax.swing.JFrame; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamPanel; +import com.github.sarxos.webcam.WebcamPanel.DrawMode; +import com.github.sarxos.webcam.ds.gstreamer.ScreenCaptureDriver; + + +public class WebcamPanelExample { + + static { + Webcam.setDriver(new ScreenCaptureDriver()); + } + + public static void main(String[] args) { + + JFrame window = new JFrame("aaa"); + window.setResizable(true); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.getContentPane().setLayout(new FlowLayout()); + + for (Webcam webcam : Webcam.getWebcams()) { + + WebcamPanel panel = new WebcamPanel(webcam); + panel.setFPSDisplayed(true); + panel.setDrawMode(DrawMode.FIT); + panel.setImageSizeDisplayed(true); + panel.setPreferredSize(new Dimension(300, 200)); + + window.getContentPane().add(panel); + } + + window.pack(); + window.setVisible(true); + } +} diff --git a/webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelSubViewExample.java b/webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelSubViewExample.java new file mode 100644 index 00000000..201343f6 --- /dev/null +++ b/webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelSubViewExample.java @@ -0,0 +1,79 @@ +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import javax.swing.JFrame; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamCompositeDriver; +import com.github.sarxos.webcam.WebcamPanel; +import com.github.sarxos.webcam.WebcamPanel.DrawMode; +import com.github.sarxos.webcam.WebcamPanel.Painter; +import com.github.sarxos.webcam.WebcamResolution; +import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver; +import com.github.sarxos.webcam.ds.gstreamer.ScreenCaptureDriver; + + +public class WebcamPanelSubViewExample { + + static { + Webcam.setDriver(new WebcamCompositeDriver(new WebcamDefaultDriver(), new ScreenCaptureDriver())); + } + + private final Dimension size = WebcamResolution.QQVGA.getSize(); + private final Webcam screen = Webcam.getWebcamByName(":0.1"); + private final WebcamPanel panel = new WebcamPanel(screen); + private final Painter dp = panel.getDefaultPainter(); + private final JFrame window = new JFrame("Test"); + + private final class SubViewPainter implements Painter { + + private final Webcam webcam = Webcam.getDefault(); + private final int x = 619; + private final int y = 437; + private final int w = size.width; + private final int h = size.height; + + public SubViewPainter() { + webcam.setViewSize(size); + webcam.open(); + } + + @Override + public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) { + dp.paintImage(owner, image, g2); + g2.setColor(Color.BLACK); + g2.drawRect(x - 1, y - 1, w + 1, h + 1); + g2.drawImage(webcam.getImage(), x, y, w, h, null); + } + + @Override + public void paintPanel(WebcamPanel panel, Graphics2D g2) { + dp.paintPanel(panel, g2); + }; + }; + + public WebcamPanelSubViewExample() { + + screen.open(true); + + panel.setFPSDisplayed(true); + panel.setDrawMode(DrawMode.FIT); + panel.setImageSizeDisplayed(true); + panel.setFPSDisplayed(true); + panel.setPainter(new SubViewPainter()); + panel.setPreferredSize(new Dimension(800, 600)); + + window.setResizable(true); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.setContentPane(panel); + window.pack(); + window.setVisible(true); + + } + + public static void main(String[] args) { + new WebcamPanelSubViewExample(); + } +} diff --git a/webcam-capture-drivers/driver-screencapture/src/example/resources/logback.xml b/webcam-capture-drivers/driver-screencapture/src/example/resources/logback.xml new file mode 100644 index 00000000..30ec27f8 --- /dev/null +++ b/webcam-capture-drivers/driver-screencapture/src/example/resources/logback.xml @@ -0,0 +1,10 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + diff --git a/webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDevice.java b/webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDevice.java new file mode 100644 index 00000000..3652c47b --- /dev/null +++ b/webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDevice.java @@ -0,0 +1,92 @@ +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; + } +} diff --git a/webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDriver.java b/webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDriver.java new file mode 100644 index 00000000..d32251a8 --- /dev/null +++ b/webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDriver.java @@ -0,0 +1,32 @@ +package com.github.sarxos.webcam.ds.gstreamer; + +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.util.ArrayList; +import java.util.List; + +import com.github.sarxos.webcam.WebcamDevice; +import com.github.sarxos.webcam.WebcamDriver; + + +public class ScreenCaptureDriver implements WebcamDriver { + + @Override + public List getDevices() { + + final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment(); + final GraphicsDevice[] devices = g.getScreenDevices(); + + final List list = new ArrayList<>(); + for (final GraphicsDevice device : devices) { + list.add(new ScreenCaptureDevice(device)); + } + + return list; + } + + @Override + public boolean isThreadSafe() { + return false; + } +} diff --git a/webcam-capture-drivers/pom.xml b/webcam-capture-drivers/pom.xml index 7b697f62..f5f9dbf7 100644 --- a/webcam-capture-drivers/pom.xml +++ b/webcam-capture-drivers/pom.xml @@ -29,6 +29,7 @@ driver-jmf driver-lti-civil driver-openimaj + driver-screencapture driver-v4l4j driver-vlcj