Skip to content

Commit 0e0ccae

Browse files
committed
Add screen capture driver
1 parent d4a5c8d commit 0e0ccae

File tree

7 files changed

+297
-0
lines changed

7 files changed

+297
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<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">
2+
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.github.sarxos</groupId>
7+
<artifactId>webcam-capture-drivers</artifactId>
8+
<version>0.3.12-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>webcam-capture-driver-screencapture</artifactId>
12+
<packaging>jar</packaging>
13+
14+
<name>Webcam Capture - Screen Capture Driver</name>
15+
<description>Webcam Capture driver for capturing images from screen devices</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.github.sarxos</groupId>
20+
<artifactId>webcam-capture</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>ch.qos.logback</groupId>
25+
<artifactId>logback-classic</artifactId>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-assembly-plugin</artifactId>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
44+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.awt.Dimension;
2+
import java.awt.FlowLayout;
3+
4+
import javax.swing.JFrame;
5+
6+
import com.github.sarxos.webcam.Webcam;
7+
import com.github.sarxos.webcam.WebcamPanel;
8+
import com.github.sarxos.webcam.WebcamPanel.DrawMode;
9+
import com.github.sarxos.webcam.ds.gstreamer.ScreenCaptureDriver;
10+
11+
12+
public class WebcamPanelExample {
13+
14+
static {
15+
Webcam.setDriver(new ScreenCaptureDriver());
16+
}
17+
18+
public static void main(String[] args) {
19+
20+
JFrame window = new JFrame("aaa");
21+
window.setResizable(true);
22+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
window.getContentPane().setLayout(new FlowLayout());
24+
25+
for (Webcam webcam : Webcam.getWebcams()) {
26+
27+
WebcamPanel panel = new WebcamPanel(webcam);
28+
panel.setFPSDisplayed(true);
29+
panel.setDrawMode(DrawMode.FIT);
30+
panel.setImageSizeDisplayed(true);
31+
panel.setPreferredSize(new Dimension(300, 200));
32+
33+
window.getContentPane().add(panel);
34+
}
35+
36+
window.pack();
37+
window.setVisible(true);
38+
}
39+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.awt.Color;
2+
import java.awt.Dimension;
3+
import java.awt.Graphics2D;
4+
import java.awt.image.BufferedImage;
5+
6+
import javax.swing.JFrame;
7+
8+
import com.github.sarxos.webcam.Webcam;
9+
import com.github.sarxos.webcam.WebcamCompositeDriver;
10+
import com.github.sarxos.webcam.WebcamPanel;
11+
import com.github.sarxos.webcam.WebcamPanel.DrawMode;
12+
import com.github.sarxos.webcam.WebcamPanel.Painter;
13+
import com.github.sarxos.webcam.WebcamResolution;
14+
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver;
15+
import com.github.sarxos.webcam.ds.gstreamer.ScreenCaptureDriver;
16+
17+
18+
public class WebcamPanelSubViewExample {
19+
20+
static {
21+
Webcam.setDriver(new WebcamCompositeDriver(new WebcamDefaultDriver(), new ScreenCaptureDriver()));
22+
}
23+
24+
private final Dimension size = WebcamResolution.QQVGA.getSize();
25+
private final Webcam screen = Webcam.getWebcamByName(":0.1");
26+
private final WebcamPanel panel = new WebcamPanel(screen);
27+
private final Painter dp = panel.getDefaultPainter();
28+
private final JFrame window = new JFrame("Test");
29+
30+
private final class SubViewPainter implements Painter {
31+
32+
private final Webcam webcam = Webcam.getDefault();
33+
private final int x = 619;
34+
private final int y = 437;
35+
private final int w = size.width;
36+
private final int h = size.height;
37+
38+
public SubViewPainter() {
39+
webcam.setViewSize(size);
40+
webcam.open();
41+
}
42+
43+
@Override
44+
public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
45+
dp.paintImage(owner, image, g2);
46+
g2.setColor(Color.BLACK);
47+
g2.drawRect(x - 1, y - 1, w + 1, h + 1);
48+
g2.drawImage(webcam.getImage(), x, y, w, h, null);
49+
}
50+
51+
@Override
52+
public void paintPanel(WebcamPanel panel, Graphics2D g2) {
53+
dp.paintPanel(panel, g2);
54+
};
55+
};
56+
57+
public WebcamPanelSubViewExample() {
58+
59+
screen.open(true);
60+
61+
panel.setFPSDisplayed(true);
62+
panel.setDrawMode(DrawMode.FIT);
63+
panel.setImageSizeDisplayed(true);
64+
panel.setFPSDisplayed(true);
65+
panel.setPainter(new SubViewPainter());
66+
panel.setPreferredSize(new Dimension(800, 600));
67+
68+
window.setResizable(true);
69+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70+
window.setContentPane(panel);
71+
window.pack();
72+
window.setVisible(true);
73+
74+
}
75+
76+
public static void main(String[] args) {
77+
new WebcamPanelSubViewExample();
78+
}
79+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<configuration scan="true" scanPeriod="30 seconds">
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<layout class="ch.qos.logback.classic.PatternLayout">
4+
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
5+
</layout>
6+
</appender>
7+
<root level="debug">
8+
<appender-ref ref="STDOUT" />
9+
</root>
10+
</configuration>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.github.sarxos.webcam.ds.gstreamer;
2+
3+
import java.awt.AWTException;
4+
import java.awt.Dimension;
5+
import java.awt.DisplayMode;
6+
import java.awt.GraphicsConfiguration;
7+
import java.awt.GraphicsDevice;
8+
import java.awt.Rectangle;
9+
import java.awt.Robot;
10+
import java.awt.image.BufferedImage;
11+
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import com.github.sarxos.webcam.WebcamDevice;
16+
import com.github.sarxos.webcam.WebcamException;
17+
18+
19+
public class ScreenCaptureDevice implements WebcamDevice {
20+
21+
private static final Logger LOG = LoggerFactory.getLogger(ScreenCaptureDevice.class);
22+
23+
private final GraphicsDevice device;
24+
private final DisplayMode mode;
25+
private final Dimension resolution;
26+
private final Robot robot;
27+
28+
private boolean open = false;
29+
30+
public ScreenCaptureDevice(final GraphicsDevice device) {
31+
32+
this.device = device;
33+
this.mode = device.getDisplayMode();
34+
this.resolution = new Dimension(mode.getWidth(), mode.getHeight());
35+
36+
try {
37+
this.robot = new Robot(device);
38+
} catch (AWTException e) {
39+
throw new WebcamException("Unable to create robot", e);
40+
}
41+
42+
LOG.trace("Screen device {} with resolution {} has been created", getName(), getResolution());
43+
}
44+
45+
@Override
46+
public String getName() {
47+
return device.getIDstring();
48+
}
49+
50+
@Override
51+
public Dimension[] getResolutions() {
52+
return new Dimension[] { resolution };
53+
}
54+
55+
@Override
56+
public Dimension getResolution() {
57+
return resolution;
58+
}
59+
60+
@Override
61+
public void setResolution(Dimension size) {
62+
// do nothings, screen has only one resolution which is already set
63+
}
64+
65+
@Override
66+
public BufferedImage getImage() {
67+
final GraphicsConfiguration gc = device.getDefaultConfiguration();
68+
final Rectangle bounds = gc.getBounds();
69+
return robot.createScreenCapture(bounds);
70+
}
71+
72+
@Override
73+
public void open() {
74+
LOG.debug("Opening screen device {} with resolution {}", getName(), getResolution());
75+
open = true;
76+
}
77+
78+
@Override
79+
public void close() {
80+
open = false;
81+
}
82+
83+
@Override
84+
public void dispose() {
85+
// do nothing, no need to dispose anything here
86+
}
87+
88+
@Override
89+
public boolean isOpen() {
90+
return open;
91+
}
92+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.sarxos.webcam.ds.gstreamer;
2+
3+
import java.awt.GraphicsDevice;
4+
import java.awt.GraphicsEnvironment;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import com.github.sarxos.webcam.WebcamDevice;
9+
import com.github.sarxos.webcam.WebcamDriver;
10+
11+
12+
public class ScreenCaptureDriver implements WebcamDriver {
13+
14+
@Override
15+
public List<WebcamDevice> getDevices() {
16+
17+
final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
18+
final GraphicsDevice[] devices = g.getScreenDevices();
19+
20+
final List<WebcamDevice> list = new ArrayList<>();
21+
for (final GraphicsDevice device : devices) {
22+
list.add(new ScreenCaptureDevice(device));
23+
}
24+
25+
return list;
26+
}
27+
28+
@Override
29+
public boolean isThreadSafe() {
30+
return false;
31+
}
32+
}

webcam-capture-drivers/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<module>driver-jmf</module>
3030
<module>driver-lti-civil</module>
3131
<module>driver-openimaj</module>
32+
<module>driver-screencapture</module>
3233
<module>driver-v4l4j</module>
3334
<module>driver-vlcj</module>
3435
</modules>

0 commit comments

Comments
 (0)