-
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.
Add dummy driver to be later used in tests
- Loading branch information
Showing
3 changed files
with
228 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,32 @@ | ||
import javax.swing.JFrame; | ||
|
||
import com.github.sarxos.webcam.Webcam; | ||
import com.github.sarxos.webcam.WebcamPanel; | ||
import com.github.sarxos.webcam.ds.dummy.WebcamDummyDriver; | ||
|
||
|
||
public class DummyDeviceExample { | ||
|
||
static { | ||
Webcam.setDriver(new WebcamDummyDriver(3)); | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
Webcam webcam = Webcam.getDefault(); | ||
// webcam.setViewSize(WebcamResolution.VGA.getSize()); | ||
|
||
WebcamPanel panel = new WebcamPanel(webcam); | ||
panel.setFPSDisplayed(true); | ||
panel.setImageSizeDisplayed(true); | ||
panel.setMirrored(false); | ||
|
||
JFrame window = new JFrame("Dummy Device Example"); | ||
window.add(panel); | ||
window.setResizable(true); | ||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
window.pack(); | ||
window.setVisible(true); | ||
} | ||
|
||
} |
153 changes: 153 additions & 0 deletions
153
webcam-capture/src/main/java/com/github/sarxos/webcam/ds/dummy/WebcamDummyDevice.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,153 @@ | ||
package com.github.sarxos.webcam.ds.dummy; | ||
|
||
import java.awt.Color; | ||
import java.awt.Dimension; | ||
import java.awt.Font; | ||
import java.awt.FontMetrics; | ||
import java.awt.Graphics2D; | ||
import java.awt.GraphicsConfiguration; | ||
import java.awt.GraphicsEnvironment; | ||
import java.awt.RenderingHints; | ||
import java.awt.image.BufferedImage; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import com.github.sarxos.webcam.WebcamDevice; | ||
import com.github.sarxos.webcam.WebcamException; | ||
import com.github.sarxos.webcam.WebcamResolution; | ||
|
||
|
||
/** | ||
* Just a dummy device to be used for test purpose. | ||
* | ||
* @author Bartosz Firyn (sarxos) | ||
**/ | ||
public class WebcamDummyDevice implements WebcamDevice { | ||
|
||
private final static Dimension[] DIMENSIONS = new Dimension[] { | ||
WebcamResolution.QQVGA.getSize(), | ||
WebcamResolution.QVGA.getSize(), | ||
WebcamResolution.VGA.getSize(), | ||
}; | ||
|
||
private AtomicBoolean open = new AtomicBoolean(false); | ||
private Dimension resolution = DIMENSIONS[0]; | ||
|
||
private final String name; | ||
|
||
public WebcamDummyDevice(int number) { | ||
this.name = "Dummy Webcam " + number; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Dimension[] getResolutions() { | ||
return DIMENSIONS; | ||
} | ||
|
||
@Override | ||
public Dimension getResolution() { | ||
return resolution; | ||
} | ||
|
||
@Override | ||
public void setResolution(Dimension size) { | ||
this.resolution = size; | ||
} | ||
|
||
byte r = (byte) (Math.random() * Byte.MAX_VALUE); | ||
byte g = (byte) (Math.random() * Byte.MAX_VALUE); | ||
byte b = (byte) (Math.random() * Byte.MAX_VALUE); | ||
|
||
private void drawRect(Graphics2D g2, int w, int h) { | ||
|
||
int rx = (int) (w * Math.random() / 1.5); | ||
int ry = (int) (h * Math.random() / 1.5); | ||
int rw = (int) (w * Math.random() / 1.5); | ||
int rh = (int) (w * Math.random() / 1.5); | ||
|
||
g2.setColor(new Color((int) (Integer.MAX_VALUE * Math.random()))); | ||
g2.fillRect(rx, ry, rw, rh); | ||
} | ||
|
||
@Override | ||
public BufferedImage getImage() { | ||
|
||
if (!isOpen()) { | ||
throw new WebcamException("Webcam is not open"); | ||
} | ||
|
||
try { | ||
Thread.sleep(1000 / 30); | ||
} catch (InterruptedException e) { | ||
return null; | ||
} | ||
|
||
Dimension resolution = getResolution(); | ||
|
||
int w = resolution.width; | ||
int h = resolution.height; | ||
|
||
String s = getName(); | ||
|
||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); | ||
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); | ||
BufferedImage bi = gc.createCompatibleImage(w, h); | ||
|
||
Graphics2D g2 = ge.createGraphics(bi); | ||
g2.setBackground(new Color(Math.abs(r++), Math.abs(g++), Math.abs(b++))); | ||
g2.clearRect(0, 0, w, h); | ||
|
||
drawRect(g2, w, h); | ||
drawRect(g2, w, h); | ||
drawRect(g2, w, h); | ||
drawRect(g2, w, h); | ||
drawRect(g2, w, h); | ||
|
||
Font font = new Font("sans-serif", Font.BOLD, 16); | ||
|
||
g2.setFont(font); | ||
|
||
FontMetrics metrics = g2.getFontMetrics(font); | ||
int sw = (w - metrics.stringWidth(s)) / 2; | ||
int sh = (h - metrics.getHeight()) / 2 + metrics.getHeight() / 2; | ||
|
||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | ||
g2.setColor(Color.BLACK); | ||
g2.drawString(s, sw + 1, sh + 1); | ||
g2.setColor(Color.WHITE); | ||
g2.drawString(s, sw, sh); | ||
|
||
g2.dispose(); | ||
bi.flush(); | ||
|
||
return bi; | ||
} | ||
|
||
@Override | ||
public void open() { | ||
if (open.compareAndSet(false, true)) { | ||
// ... | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (open.compareAndSet(true, false)) { | ||
// ... | ||
} | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
close(); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return open.get(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
webcam-capture/src/main/java/com/github/sarxos/webcam/ds/dummy/WebcamDummyDriver.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,43 @@ | ||
package com.github.sarxos.webcam.ds.dummy; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import com.github.sarxos.webcam.WebcamDevice; | ||
import com.github.sarxos.webcam.WebcamDiscoverySupport; | ||
import com.github.sarxos.webcam.WebcamDriver; | ||
|
||
|
||
public class WebcamDummyDriver implements WebcamDriver, WebcamDiscoverySupport { | ||
|
||
private int count; | ||
|
||
public WebcamDummyDriver(int count) { | ||
this.count = count; | ||
} | ||
|
||
@Override | ||
public long getScanInterval() { | ||
return 10000; | ||
} | ||
|
||
@Override | ||
public boolean isScanPossible() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<WebcamDevice> getDevices() { | ||
List<WebcamDevice> devices = new ArrayList<WebcamDevice>(); | ||
for (int i = 0; i < count; i++) { | ||
devices.add(new WebcamDummyDevice(i)); | ||
} | ||
return Collections.unmodifiableList(devices); | ||
} | ||
|
||
@Override | ||
public boolean isThreadSafe() { | ||
return false; | ||
} | ||
} |