Skip to content

Commit ae85738

Browse files
committed
Small changes and javadocs in screen capture driver
1 parent a353467 commit ae85738

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class WebcamPanelExample {
1717

1818
public static void main(String[] args) {
1919

20-
JFrame window = new JFrame("aaa");
20+
final JFrame window = new JFrame("Screen Capture Example");
2121
window.setResizable(true);
2222
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2323
window.getContentPane().setLayout(new FlowLayout());

webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelSubViewExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class WebcamPanelSubViewExample {
2222
}
2323

2424
private final Dimension size = WebcamResolution.QQVGA.getSize();
25-
private final Webcam screen = Webcam.getWebcamByName(":0.1");
25+
private final Webcam screen = Webcam.getWebcams().get(1);
2626
private final WebcamPanel panel = new WebcamPanel(screen);
2727
private final Painter dp = panel.getDefaultPainter();
2828
private final JFrame window = new JFrame("Test");

webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDevice.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.awt.Dimension;
55
import java.awt.DisplayMode;
66
import java.awt.Graphics2D;
7-
import java.awt.GraphicsConfiguration;
87
import java.awt.GraphicsDevice;
98
import java.awt.Rectangle;
109
import java.awt.RenderingHints;
@@ -18,22 +17,31 @@
1817
import com.github.sarxos.webcam.WebcamException;
1918

2019

20+
/**
21+
* This class abstract screen device (display) which can be used to capture image from.
22+
*
23+
* @author Bartosz Firyn (sarxos)
24+
* @author Hagen Stanek (gubjack)
25+
*/
2126
public class ScreenCaptureDevice implements WebcamDevice {
2227

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

2530
private final GraphicsDevice device;
2631
private final DisplayMode mode;
2732
private final Dimension resolution;
33+
private final Dimension[] resolutions;
2834
private final Robot robot;
2935

3036
private boolean open = false;
37+
private Rectangle bounds;
3138

3239
public ScreenCaptureDevice(final GraphicsDevice device) {
3340

3441
this.device = device;
3542
this.mode = device.getDisplayMode();
3643
this.resolution = new Dimension(mode.getWidth(), mode.getHeight());
44+
this.resolutions = new Dimension[] { this.resolution };
3745

3846
try {
3947
this.robot = new Robot(device);
@@ -51,7 +59,7 @@ public String getName() {
5159

5260
@Override
5361
public Dimension[] getResolutions() {
54-
return new Dimension[] { resolution };
62+
return resolutions;
5563
}
5664

5765
@Override
@@ -61,33 +69,42 @@ public Dimension getResolution() {
6169

6270
@Override
6371
public void setResolution(Dimension size) {
64-
resolution.setSize(size.getWidth(), size.getHeight());
72+
resolution.setSize(size.getWidth(), size.getHeight());
6573
}
6674

6775
@Override
6876
public BufferedImage getImage() {
69-
final GraphicsConfiguration gc = device.getDefaultConfiguration();
70-
final Rectangle bounds = gc.getBounds();
71-
BufferedImage screen = robot.createScreenCapture(bounds);
72-
int width = resolution.width;
73-
int height = resolution.height;
77+
78+
final BufferedImage screen = robot.createScreenCapture(bounds);
79+
final int width = resolution.width;
80+
final int height = resolution.height;
81+
7482
if (screen.getWidth() == width && screen.getHeight() == height) {
75-
return screen; // No need for adaption
83+
return screen; // No need for adaption
7684
}
77-
BufferedImage img = new BufferedImage(width, height, screen.getType());
78-
Graphics2D g = img.createGraphics();
79-
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
80-
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
81-
g.drawImage(screen, 0, 0, width, height,
82-
0, 0, screen.getWidth(), screen.getHeight(),
83-
null);
85+
86+
final BufferedImage img = new BufferedImage(width, height, screen.getType());
87+
final Graphics2D g = img.createGraphics();
88+
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
89+
g.drawImage(screen, 0, 0, width, height, 0, 0, screen.getWidth(), screen.getHeight(), null);
8490
g.dispose();
91+
8592
return img;
8693
}
8794

8895
@Override
8996
public void open() {
97+
98+
if (open) {
99+
return;
100+
}
101+
90102
LOG.debug("Opening screen device {} with resolution {}", getName(), getResolution());
103+
104+
this.bounds = device
105+
.getDefaultConfiguration()
106+
.getBounds();
107+
91108
open = true;
92109
}
93110

webcam-capture-drivers/driver-screencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDriver.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
import com.github.sarxos.webcam.Webcam;
89
import com.github.sarxos.webcam.WebcamDevice;
910
import com.github.sarxos.webcam.WebcamDriver;
1011

1112

13+
/**
14+
* This is capture driver which returns a list of {@link ScreenCaptureDevice} instances which can be
15+
* used by {@link Webcam} to stream images feed from.
16+
*
17+
* @author Bartosz Firyn (sarxos)
18+
*/
1219
public class ScreenCaptureDriver implements WebcamDriver {
1320

1421
@Override
1522
public List<WebcamDevice> getDevices() {
1623

17-
final GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
18-
final GraphicsDevice[] devices = g.getScreenDevices();
24+
final GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
25+
final GraphicsDevice[] devices = environment.getScreenDevices();
1926

20-
final List<WebcamDevice> list = new ArrayList<>();
27+
final List<WebcamDevice> list = new ArrayList<>(devices.length);
2128
for (final GraphicsDevice device : devices) {
2229
list.add(new ScreenCaptureDevice(device));
2330
}

0 commit comments

Comments
 (0)