|
1 | | -package com.github.sarxos.webcam; |
2 | | - |
3 | | -import javax.swing.*; |
4 | | -import java.awt.*; |
| 1 | +import java.awt.BasicStroke; |
| 2 | +import java.awt.Color; |
| 3 | +import java.awt.Graphics2D; |
| 4 | +import java.awt.Point; |
| 5 | +import java.awt.Rectangle; |
| 6 | +import java.awt.Stroke; |
5 | 7 | import java.awt.image.BufferedImage; |
6 | 8 | import java.util.ArrayList; |
7 | 9 | import java.util.HashMap; |
8 | 10 | import java.util.Map; |
9 | 11 |
|
10 | | -public class MultipointMotionDetectionExample implements WebcamMotionListener, WebcamPanel.Painter { |
11 | | - |
12 | | - public static void main(String[] args) throws InterruptedException { |
13 | | - new MultipointMotionDetectionExample(); |
14 | | - } |
15 | | - |
16 | | - private static final int INTERVAL = 100; // ms |
17 | | - |
18 | | - public static Webcam webcam; |
19 | | - public static WebcamPanel.Painter painter = null; |
20 | | - |
21 | | - public MultipointMotionDetectionExample(){ |
22 | | - webcam = Webcam.getDefault(); |
23 | | - webcam.setViewSize(WebcamResolution.VGA.getSize()); |
24 | | - |
25 | | - WebcamPanel panel = new WebcamPanel(webcam); |
26 | | - panel.setPreferredSize(WebcamResolution.VGA.getSize()); |
27 | | - panel.setPainter(this); |
28 | | - panel.setFPSDisplayed(true); |
29 | | - panel.setFPSLimited(true); |
30 | | - panel.setFPSLimit(20); |
31 | | - panel.setPainter(this); |
32 | | - panel.start(); |
33 | | - |
34 | | - painter = panel.getDefaultPainter(); |
| 12 | +import javax.swing.JFrame; |
35 | 13 |
|
36 | | - JFrame window = new JFrame("Multipoint-motion detection"); |
37 | | - window.add(panel); |
38 | | - window.setResizable(true); |
39 | | - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
40 | | - window.pack(); |
41 | | - window.setVisible(true); |
| 14 | +import com.github.sarxos.webcam.Webcam; |
| 15 | +import com.github.sarxos.webcam.WebcamMotionDetector; |
| 16 | +import com.github.sarxos.webcam.WebcamMotionEvent; |
| 17 | +import com.github.sarxos.webcam.WebcamMotionListener; |
| 18 | +import com.github.sarxos.webcam.WebcamPanel; |
| 19 | +import com.github.sarxos.webcam.WebcamResolution; |
42 | 20 |
|
43 | 21 |
|
44 | | - WebcamMotionDetector detector = new WebcamMotionDetector(webcam); |
45 | | - |
46 | | - //Sets the max amount of motion points to 300 and the minimum range between them to 40 |
47 | | - detector.setMaxMotionPoints(300); |
48 | | - detector.setPointRange(40); |
49 | | - |
50 | | - detector.setInterval(INTERVAL); |
51 | | - detector.addMotionListener(this); |
52 | | - |
53 | | - detector.start(); |
54 | | - } |
55 | | - |
56 | | - |
57 | | - //A HashMap to store all the current points and the current amount of times it has been rendered |
58 | | - //Time rendered is used to remove the point after a certain amount of time |
59 | | - public static HashMap<Point, Integer> motionPoints = new HashMap<Point, Integer>(); |
60 | | - |
61 | | - //Gets the motion points from the motion detector and adds it to the HashMap |
62 | | - @Override |
63 | | - public void motionDetected(WebcamMotionEvent wme) { |
64 | | - for(Point p : wme.getPoints()){ |
65 | | - motionPoints.put(p, 0); |
66 | | - } |
67 | | - } |
| 22 | +/** |
| 23 | + * This is example demonstrating how to use multipoint motion detection feature. |
| 24 | + * |
| 25 | + * @author tm1990 (https://github.com/tm1990) |
| 26 | + */ |
| 27 | +public class MultipointMotionDetectionExample implements WebcamMotionListener, WebcamPanel.Painter { |
68 | 28 |
|
69 | | - @Override |
70 | | - public void paintPanel(WebcamPanel panel, Graphics2D g2) { |
71 | | - if (painter != null) { |
72 | | - painter.paintPanel(panel, g2); |
73 | | - } |
74 | | - } |
| 29 | + public static void main(String[] args) throws InterruptedException { |
| 30 | + new MultipointMotionDetectionExample(); |
| 31 | + } |
| 32 | + |
| 33 | + private static final int INTERVAL = 100; // ms |
75 | 34 |
|
| 35 | + public static Webcam webcam; |
| 36 | + public static WebcamPanel.Painter painter = null; |
76 | 37 |
|
77 | | - //Used to render the effect for the motion points |
78 | | - private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f); |
| 38 | + public MultipointMotionDetectionExample() { |
| 39 | + |
| 40 | + webcam = Webcam.getDefault(); |
| 41 | + webcam.setViewSize(WebcamResolution.VGA.getSize()); |
79 | 42 |
|
80 | | - //The amount of time each point should be rendered for before being removed |
81 | | - public static final int renderTime = 3; |
| 43 | + WebcamPanel panel = new WebcamPanel(webcam); |
| 44 | + panel.setPreferredSize(WebcamResolution.VGA.getSize()); |
| 45 | + panel.setPainter(this); |
| 46 | + panel.setFPSDisplayed(true); |
| 47 | + panel.setFPSLimited(true); |
| 48 | + panel.setFPSLimit(20); |
| 49 | + panel.setPainter(this); |
| 50 | + panel.start(); |
82 | 51 |
|
83 | | - //The actual size of the rendered effect for each point |
84 | | - public static final int renderSize = 20; |
| 52 | + painter = panel.getDefaultPainter(); |
85 | 53 |
|
86 | | - @Override |
87 | | - public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) { |
| 54 | + JFrame window = new JFrame("Multipoint-motion detection"); |
| 55 | + window.add(panel); |
| 56 | + window.setResizable(true); |
| 57 | + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 58 | + window.pack(); |
| 59 | + window.setVisible(true); |
88 | 60 |
|
| 61 | + WebcamMotionDetector detector = new WebcamMotionDetector(webcam); |
89 | 62 |
|
90 | | - if (painter != null) { |
91 | | - painter.paintImage(panel, image, g2); |
92 | | - } |
| 63 | + // Sets the max amount of motion points to 300 and the minimum range between them to 40 |
| 64 | + detector.setMaxMotionPoints(300); |
| 65 | + detector.setPointRange(40); |
93 | 66 |
|
94 | | - //Gets all the points and updates the amount of time they have been rendered for |
95 | | - //And removes the ones that exceed the renderTime variable |
| 67 | + detector.setInterval(INTERVAL); |
| 68 | + detector.addMotionListener(this); |
96 | 69 |
|
97 | | - ArrayList<Point> rem = new ArrayList<Point>(); |
| 70 | + detector.start(); |
| 71 | + } |
98 | 72 |
|
99 | | - for (Map.Entry<Point, Integer> ent : motionPoints.entrySet()) { |
100 | | - Point p = ent.getKey(); |
| 73 | + /** |
| 74 | + * A HashMap to store all the current points and the current amount of times it has been |
| 75 | + * rendered. Time rendered is used to remove the point after a certain amount of time. |
| 76 | + */ |
| 77 | + public static HashMap<Point, Integer> motionPoints = new HashMap<Point, Integer>(); |
101 | 78 |
|
102 | | - if (ent.getValue() != null) { |
103 | | - int tt = ent.getValue(); |
104 | | - if (tt >= renderTime) { |
105 | | - rem.add(ent.getKey()); |
| 79 | + // Gets the motion points from the motion detector and adds it to the HashMap |
| 80 | + @Override |
| 81 | + public void motionDetected(WebcamMotionEvent wme) { |
| 82 | + for (Point p : wme.getPoints()) { |
| 83 | + motionPoints.put(p, 0); |
| 84 | + } |
| 85 | + } |
106 | 86 |
|
107 | | - } else { |
108 | | - int temp = ent.getValue() + 1; |
109 | | - motionPoints.put(p, temp); |
110 | | - } |
| 87 | + @Override |
| 88 | + public void paintPanel(WebcamPanel panel, Graphics2D g2) { |
| 89 | + if (painter != null) { |
| 90 | + painter.paintPanel(panel, g2); |
| 91 | + } |
| 92 | + } |
111 | 93 |
|
112 | | - } |
113 | | - } |
| 94 | + /** |
| 95 | + * Used to render the effect for the motion points. |
| 96 | + */ |
| 97 | + private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f); |
114 | 98 |
|
115 | | - for(Point p : rem){ |
116 | | - motionPoints.remove(p); |
117 | | - } |
| 99 | + /** |
| 100 | + * The amount of time each point should be rendered for before being removed. |
| 101 | + */ |
| 102 | + public static final int renderTime = 3; |
118 | 103 |
|
| 104 | + /** |
| 105 | + * The actual size of the rendered effect for each point. |
| 106 | + */ |
| 107 | + public static final int renderSize = 20; |
119 | 108 |
|
120 | | - //Gets all the remaining points after removing the exceeded ones and then renders the current ones as a red square |
121 | | - for(Map.Entry<Point, Integer> ent : motionPoints.entrySet()){ |
122 | | - Point p = ent.getKey(); |
| 109 | + @Override |
| 110 | + public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) { |
123 | 111 |
|
124 | | - int xx = p.x - (renderSize / 2), yy = p.y - (renderSize / 2); |
| 112 | + if (painter != null) { |
| 113 | + painter.paintImage(panel, image, g2); |
| 114 | + } |
125 | 115 |
|
126 | | - Rectangle bounds = new Rectangle(xx, yy, renderSize, renderSize); |
| 116 | + // Gets all the points and updates the amount of time they have been rendered for |
| 117 | + // And removes the ones that exceed the renderTime variable |
127 | 118 |
|
128 | | - int dx = (int) (0.1 * bounds.width); |
129 | | - int dy = (int) (0.2 * bounds.height); |
130 | | - int x = (int) bounds.x - dx; |
131 | | - int y = (int) bounds.y - dy; |
132 | | - int w = (int) bounds.width + 2 * dx; |
133 | | - int h = (int) bounds.height + dy; |
| 119 | + ArrayList<Point> rem = new ArrayList<Point>(); |
134 | 120 |
|
135 | | - g2.setStroke(STROKE); |
136 | | - g2.setColor(Color.RED); |
137 | | - g2.drawRect(x, y, w, h); |
| 121 | + for (Map.Entry<Point, Integer> ent : motionPoints.entrySet()) { |
| 122 | + Point p = ent.getKey(); |
138 | 123 |
|
139 | | - } |
| 124 | + if (ent.getValue() != null) { |
| 125 | + int tt = ent.getValue(); |
| 126 | + if (tt >= renderTime) { |
| 127 | + rem.add(ent.getKey()); |
140 | 128 |
|
141 | | - } |
| 129 | + } else { |
| 130 | + int temp = ent.getValue() + 1; |
| 131 | + motionPoints.put(p, temp); |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + for (Point p : rem) { |
| 138 | + motionPoints.remove(p); |
| 139 | + } |
| 140 | + |
| 141 | + // Gets all the remaining points after removing the exceeded ones and then renders the |
| 142 | + // current ones as a red square |
| 143 | + |
| 144 | + for (Map.Entry<Point, Integer> ent : motionPoints.entrySet()) { |
| 145 | + |
| 146 | + Point p = ent.getKey(); |
| 147 | + int xx = p.x - (renderSize / 2), yy = p.y - (renderSize / 2); |
| 148 | + |
| 149 | + Rectangle bounds = new Rectangle(xx, yy, renderSize, renderSize); |
| 150 | + |
| 151 | + int dx = (int) (0.1 * bounds.width); |
| 152 | + int dy = (int) (0.2 * bounds.height); |
| 153 | + int x = bounds.x - dx; |
| 154 | + int y = bounds.y - dy; |
| 155 | + int w = bounds.width + 2 * dx; |
| 156 | + int h = bounds.height + dy; |
| 157 | + |
| 158 | + g2.setStroke(STROKE); |
| 159 | + g2.setColor(Color.RED); |
| 160 | + g2.drawRect(x, y, w, h); |
| 161 | + } |
| 162 | + } |
142 | 163 | } |
0 commit comments