Skip to content

Commit

Permalink
Small changes and javadocs in screen capture driver
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 16, 2017
1 parent a353467 commit ae85738
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WebcamPanelExample {

public static void main(String[] args) {

JFrame window = new JFrame("aaa");
final JFrame window = new JFrame("Screen Capture Example");
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setLayout(new FlowLayout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class WebcamPanelSubViewExample {
}

private final Dimension size = WebcamResolution.QQVGA.getSize();
private final Webcam screen = Webcam.getWebcamByName(":0.1");
private final Webcam screen = Webcam.getWebcams().get(1);
private final WebcamPanel panel = new WebcamPanel(screen);
private final Painter dp = panel.getDefaultPainter();
private final JFrame window = new JFrame("Test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.Rectangle;
import java.awt.RenderingHints;
Expand All @@ -18,22 +17,31 @@
import com.github.sarxos.webcam.WebcamException;


/**
* This class abstract screen device (display) which can be used to capture image from.
*
* @author Bartosz Firyn (sarxos)
* @author Hagen Stanek (gubjack)
*/
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 Dimension[] resolutions;
private final Robot robot;

private boolean open = false;
private Rectangle bounds;

public ScreenCaptureDevice(final GraphicsDevice device) {

this.device = device;
this.mode = device.getDisplayMode();
this.resolution = new Dimension(mode.getWidth(), mode.getHeight());
this.resolutions = new Dimension[] { this.resolution };

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

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

@Override
Expand All @@ -61,33 +69,42 @@ public Dimension getResolution() {

@Override
public void setResolution(Dimension size) {
resolution.setSize(size.getWidth(), size.getHeight());
resolution.setSize(size.getWidth(), size.getHeight());
}

@Override
public BufferedImage getImage() {
final GraphicsConfiguration gc = device.getDefaultConfiguration();
final Rectangle bounds = gc.getBounds();
BufferedImage screen = robot.createScreenCapture(bounds);
int width = resolution.width;
int height = resolution.height;

final BufferedImage screen = robot.createScreenCapture(bounds);
final int width = resolution.width;
final int height = resolution.height;

if (screen.getWidth() == width && screen.getHeight() == height) {
return screen; // No need for adaption
return screen; // No need for adaption
}
BufferedImage img = new BufferedImage(width, height, screen.getType());
Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(screen, 0, 0, width, height,
0, 0, screen.getWidth(), screen.getHeight(),
null);

final BufferedImage img = new BufferedImage(width, height, screen.getType());
final Graphics2D g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(screen, 0, 0, width, height, 0, 0, screen.getWidth(), screen.getHeight(), null);
g.dispose();

return img;
}

@Override
public void open() {

if (open) {
return;
}

LOG.debug("Opening screen device {} with resolution {}", getName(), getResolution());

this.bounds = device
.getDefaultConfiguration()
.getBounds();

open = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
import java.util.ArrayList;
import java.util.List;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamDriver;


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

@Override
public List<WebcamDevice> getDevices() {

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

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

0 comments on commit ae85738

Please sign in to comment.