Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.swing;
28
 
29
import java.awt.*;
30
import java.io.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
import java.text.*;
34
import java.util.*;
35
import javax.swing.*;
36
 
37
import avr.device.event.*;
38
import avr.lang.*;
39
 
40
public class JTrackingPanel extends JPanel {
41
 
42
   private static final DateFormat DATE_FORMAT;
43
 
44
   static {
45
      DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh.mm.ss");
46
   }
47
 
48
   private ColorBlob[] blobs;
49
   private DataListener dataHandler;
50
 
51
   private Dimension preferredSize;
52
 
53
   private boolean recording;
54
   private boolean sendCameraData;
55
 
56
   private FileOutputStream outStream;
57
   private FileChannel outChannel;
58
 
59
   public JTrackingPanel() {
60
      super(null);
61
 
62
      recording = false;
63
      sendCameraData = false;
64
 
65
      setBackground(Color.BLACK);
66
      dataHandler = new TrackingHandler();
67
 
68
      preferredSize = null;
69
   }
70
 
71
   public void startRecording() throws FileNotFoundException {
72
      File recordFile = new File(DATE_FORMAT.format(new Date()) + ".trk");
73
      outStream = new FileOutputStream(recordFile);
74
      outChannel = outStream.getChannel();
75
 
76
      recording = true;
77
 
78
      AVRSystem.LOG.info("Started Recording");
79
   }
80
 
81
   public void stopRecording() throws IOException {
82
 
83
      recording = false;
84
 
85
      outStream.close();
86
      outChannel.close();
87
 
88
      AVRSystem.LOG.info("Stopped Recording");
89
 
90
   }
91
 
92
   public void startTracking() {
93
      AVRSystem.DEVICE.addDataListener(dataHandler);
94
   }
95
 
96
   public void stopTracking() {
97
      AVRSystem.DEVICE.removeDataListener(dataHandler);
98
   }
99
 
100
   public void startSendingCameraData() {
101
      sendCameraData = true;
102
   }
103
 
104
   public void stopSendingCameraData() {
105
      sendCameraData = false;
106
   }
107
 
108
   public Dimension getMinimumSize() {
109
      return getPreferredSize();
110
   }
111
 
112
   public Dimension getPreferredSize() {
113
      if(preferredSize == null) {
114
         Insets insets = this.getInsets();
115
         preferredSize = new Dimension(insets.left + AVRSystem.IMAGE_WIDTH + insets.right,
116
                                       insets.top + AVRSystem.IMAGE_HEIGHT + insets.bottom);
117
      }
118
 
119
      return preferredSize;
120
   }
121
 
122
   public Dimension getMaximumSize() {
123
      return getPreferredSize();
124
   }
125
 
126
   public void paintComponent(Graphics g) {
127
 
128
      super.paintComponent(g);
129
 
130
      Dimension size = getSize();
131
 
132
      Insets insets = getInsets();
133
 
134
      double xScale = size.width /
135
                      (double)(insets.left + 5 + AVRSystem.IMAGE_WIDTH + 5 +
136
                               insets.right);
137
      double yScale = size.height /
138
                      (double)(insets.top + 5 + AVRSystem.IMAGE_HEIGHT + 5 +
139
                               insets.bottom);
140
      double scale = Math.min(xScale, yScale);
141
 
142
      int imageWidth = (int)(AVRSystem.IMAGE_WIDTH * scale);
143
      int imageHeight = (int)(AVRSystem.IMAGE_HEIGHT * scale);
144
 
145
      // it is possible for the width or height to be 0 when
146
      // the window is resized.  If this occurs, don't try
147
      // to paint anything. just return
148
      if(imageWidth <= 0 || imageHeight <= 0) {
149
         return;
150
      }
151
 
152
      Image bufferedImage = createImage(imageWidth, imageHeight);
153
 
154
      Graphics2D bufferGraphics = (Graphics2D)bufferedImage.getGraphics();
155
 
156
      bufferGraphics.setColor(Color.WHITE);
157
      bufferGraphics.fillRect(0, 0, imageWidth, imageHeight);
158
 
159
      bufferGraphics.scale(scale, scale);
160
 
161
      for(int i = 0; (blobs != null) && (i < blobs.length); i++) {
162
         ColorBlob blob = blobs[i];
163
         if(blob != null) {
164
            bufferGraphics.setColor(AVRSystem.DEVICE.getMapColors()[blob.
165
                                    colorIndex]);
166
            bufferGraphics.fillRect(blob.center.x - 2, blob.center.y - 2, 4, 4);
167
            bufferGraphics.setColor(Color.BLACK);
168
            bufferGraphics.drawRect(blob.bounds.x, blob.bounds.y,
169
                                    blob.bounds.width, blob.bounds.height);
170
         }
171
      }
172
 
173
      g.drawImage(bufferedImage,
174
                  (size.width - imageWidth) / 2, (size.height - imageHeight) / 2,
175
                  imageWidth, imageHeight, this);
176
 
177
 
178
   }
179
 
180
   public void setTrackingData(ByteBuffer data) {
181
      blobs = new ColorBlob[data.get() & 0xFF];
182
 
183
      for(int i = 0; i < blobs.length; i++) {
184
         blobs[i] = new ColorBlob(data);
185
      }
186
 
187
      repaint();
188
 
189
      if(sendCameraData) {
190
         data.position(0);
191
         try {
192
            AVRSystem.DEVICE.sendCameraData(data);
193
         } catch(IOException ioe) {
194
            ioe.printStackTrace(System.err);
195
            AVRSystem.LOG.warning(ioe.getMessage());
196
         }
197
      }
198
 
199
   }
200
 
201
   private final class TrackingHandler extends DataAdapter {
202
 
203
      public void trackingData(ByteBuffer data) {
204
         setTrackingData(data);
205
 
206
         if(recording) {
207
            data.reset();
208
            try {
209
               outChannel.write(data);
210
            } catch(IOException ioe) {
211
               AVRSystem.LOG.warning("TRACKING: " + ioe.getMessage());
212
            }
213
         }
214
 
215
      }
216
 
217
   }
218
 
219
   private final static class ColorBlob {
220
 
221
      public final int colorIndex;
222
      public final Point center;
223
      public final Rectangle bounds;
224
 
225
      public ColorBlob(ByteBuffer data) {
226
         colorIndex = data.get();
227
 
228
         int x = data.get() & 0xFF;
229
         int y = data.get() & 0xFF;
230
         int width = (data.get() & 0xFF) - x;
231
         int height = (data.get() & 0xFF) - y;
232
         bounds = new Rectangle(x, y, width, height);
233
 
234
         center = new Point(x + (width / 2), y + (height / 2));
235
      }
236
 
237
      public String toString() {
238
         return "ColorBlob: " + colorIndex + " (" + center.x + ", " + center.y + ") " +
239
                " [" + bounds.x + ", " + bounds.y + ", " + bounds.width + ", " + bounds.height + "]";
240
      }
241
 
242
 
243
   }
244
 
245
}