Skip to content

Commit

Permalink
Add screen capture driver
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Sep 29, 2017
1 parent d4a5c8d commit 0e0ccae
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 0 deletions.
44 changes: 44 additions & 0 deletions webcam-capture-drivers/driver-screencapture/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture-drivers</artifactId>
<version>0.3.12-SNAPSHOT</version>
</parent>

<artifactId>webcam-capture-driver-screencapture</artifactId>
<packaging>jar</packaging>

<name>Webcam Capture - Screen Capture Driver</name>
<description>Webcam Capture driver for capturing images from screen devices</description>

<dependencies>
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>webcam-capture</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configuration scan="true" scanPeriod="30 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<WebcamDevice> getDevices() {

final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice[] devices = g.getScreenDevices();

final List<WebcamDevice> list = new ArrayList<>();
for (final GraphicsDevice device : devices) {
list.add(new ScreenCaptureDevice(device));
}

return list;
}

@Override
public boolean isThreadSafe() {
return false;
}
}
1 change: 1 addition & 0 deletions webcam-capture-drivers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<module>driver-jmf</module>
<module>driver-lti-civil</module>
<module>driver-openimaj</module>
<module>driver-screencapture</module>
<module>driver-v4l4j</module>
<module>driver-vlcj</module>
</modules>
Expand Down

0 comments on commit 0e0ccae

Please sign in to comment.