-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
39 changes: 39 additions & 0 deletions
39
webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
webcam-capture-drivers/driver-screencapture/src/example/java/WebcamPanelSubViewExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
webcam-capture-drivers/driver-screencapture/src/example/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
92 changes: 92 additions & 0 deletions
92
...creencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...creencapture/src/main/java/com/github/sarxos/webcam/ds/gstreamer/ScreenCaptureDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters