001package com.github.sarxos.webcam.ds.dummy;
002
003import java.awt.Color;
004import java.awt.Dimension;
005import java.awt.Font;
006import java.awt.FontMetrics;
007import java.awt.Graphics2D;
008import java.awt.GraphicsConfiguration;
009import java.awt.GraphicsEnvironment;
010import java.awt.RenderingHints;
011import java.awt.image.BufferedImage;
012import java.util.concurrent.atomic.AtomicBoolean;
013
014import com.github.sarxos.webcam.WebcamDevice;
015import com.github.sarxos.webcam.WebcamException;
016import com.github.sarxos.webcam.WebcamResolution;
017
018
019/**
020 * Just a dummy device to be used for test purpose.
021 * 
022 * @author Bartosz Firyn (sarxos)
023 **/
024public class WebcamDummyDevice implements WebcamDevice {
025
026        private final static Dimension[] DIMENSIONS = new Dimension[] {
027                WebcamResolution.QQVGA.getSize(),
028                WebcamResolution.QVGA.getSize(),
029                WebcamResolution.VGA.getSize(),
030        };
031
032        private AtomicBoolean open = new AtomicBoolean(false);
033        private Dimension resolution = DIMENSIONS[0];
034
035        private final String name;
036
037        public WebcamDummyDevice(int number) {
038                this.name = "Dummy Webcam " + number;
039        }
040
041        @Override
042        public String getName() {
043                return name;
044        }
045
046        @Override
047        public Dimension[] getResolutions() {
048                return DIMENSIONS;
049        }
050
051        @Override
052        public Dimension getResolution() {
053                return resolution;
054        }
055
056        @Override
057        public void setResolution(Dimension size) {
058                this.resolution = size;
059        }
060
061        byte r = (byte) (Math.random() * Byte.MAX_VALUE);
062        byte g = (byte) (Math.random() * Byte.MAX_VALUE);
063        byte b = (byte) (Math.random() * Byte.MAX_VALUE);
064
065        private void drawRect(Graphics2D g2, int w, int h) {
066
067                int rx = (int) (w * Math.random() / 1.5);
068                int ry = (int) (h * Math.random() / 1.5);
069                int rw = (int) (w * Math.random() / 1.5);
070                int rh = (int) (w * Math.random() / 1.5);
071
072                g2.setColor(new Color((int) (Integer.MAX_VALUE * Math.random())));
073                g2.fillRect(rx, ry, rw, rh);
074        }
075
076        @Override
077        public BufferedImage getImage() {
078
079                if (!isOpen()) {
080                        throw new WebcamException("Webcam is not open");
081                }
082
083                try {
084                        Thread.sleep(1000 / 30);
085                } catch (InterruptedException e) {
086                        return null;
087                }
088
089                Dimension resolution = getResolution();
090
091                int w = resolution.width;
092                int h = resolution.height;
093
094                String s = getName();
095
096                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
097                GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
098                BufferedImage bi = gc.createCompatibleImage(w, h);
099
100                Graphics2D g2 = ge.createGraphics(bi);
101                g2.setBackground(new Color(Math.abs(r++), Math.abs(g++), Math.abs(b++)));
102                g2.clearRect(0, 0, w, h);
103
104                drawRect(g2, w, h);
105                drawRect(g2, w, h);
106                drawRect(g2, w, h);
107                drawRect(g2, w, h);
108                drawRect(g2, w, h);
109
110                Font font = new Font("sans-serif", Font.BOLD, 16);
111
112                g2.setFont(font);
113
114                FontMetrics metrics = g2.getFontMetrics(font);
115                int sw = (w - metrics.stringWidth(s)) / 2;
116                int sh = (h - metrics.getHeight()) / 2 + metrics.getHeight() / 2;
117
118                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
119                g2.setColor(Color.BLACK);
120                g2.drawString(s, sw + 1, sh + 1);
121                g2.setColor(Color.WHITE);
122                g2.drawString(s, sw, sh);
123
124                g2.dispose();
125                bi.flush();
126
127                return bi;
128        }
129
130        @Override
131        public void open() {
132                if (open.compareAndSet(false, true)) {
133                        // ...
134                }
135        }
136
137        @Override
138        public void close() {
139                if (open.compareAndSet(true, false)) {
140                        // ...
141                }
142        }
143
144        @Override
145        public void dispose() {
146                close();
147        }
148
149        @Override
150        public boolean isOpen() {
151                return open.get();
152        }
153}