Subversion Repositories svnkaklik

Rev

Go to most recent revision | 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.awt.event.*;
31
import java.io.*;
32
import java.nio.*;
33
import java.text.*;
34
import java.util.*;
35
import javax.swing.*;
36
import javax.swing.border.*;
37
 
38
import avr.device.event.*;
39
import avr.lang.*;
40
import avr.swing.filechooser.LogFileFilter;
41
 
42
public class JCaptureInternalFrame extends JInternalFrame {
43
 
44
   private static final Format DATE_FORMAT;
45
   private static final Format DATE_FILE_NAME_FORMAT;
46
 
47
   static {
48
      DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
49
      DATE_FILE_NAME_FORMAT = new SimpleDateFormat("yyyyMMdd hhmmss");
50
   }
51
 
52
   private JMessagePanel messageP;
53
   private JColorMapInterface colorMapP;
54
   private JCapturePanel captureP;
55
   private DataListener dataHandler;
56
 
57
   private String filename;
58
 
59
   private JLabel redValueL;
60
   private JLabel greenValueL;
61
   private JLabel blueValueL;
62
 
63
   private boolean fromCamera;
64
 
65
   public JCaptureInternalFrame(JMessagePanel messageP) {
66
      this(messageP, null);
67
   }
68
 
69
   public JCaptureInternalFrame(JMessagePanel messageP, JColorMapInterface colorMapP) {
70
      this(messageP, colorMapP, null);
71
   }
72
 
73
   public JCaptureInternalFrame(JMessagePanel messageP, JColorMapInterface colorMapP, File file) {
74
      super("Capture Frame" + ((file == null) ? ": " + DATE_FORMAT.format(new Date())
75
                                              : " " + file.toString()),
76
            true, true, true, true);
77
 
78
      this.filename = DATE_FILE_NAME_FORMAT.format(new Date()) + ".byr";
79
      this.fromCamera = file == null;
80
 
81
      this.messageP = messageP;
82
      this.colorMapP = colorMapP;
83
 
84
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
85
      setJMenuBar(createMenuBar());
86
 
87
      redValueL = new JLabel("");
88
      greenValueL = new JLabel("");
89
      blueValueL = new JLabel("");
90
 
91
      captureP = new JCapturePanel();
92
 
93
      if(file != null) {
94
         try {
95
            captureP.openBayer(file);
96
         } catch(IOException ioe) {
97
            AVRSystem.LOG.severe(ioe.getMessage());
98
            ioe.printStackTrace();
99
         }
100
      }
101
 
102
      captureP.addMouseMotionListener(new MouseMotionHandler());
103
 
104
      Box southBox = new Box(BoxLayout.X_AXIS);
105
      southBox.setBorder(new EmptyBorder(5, 5, 5, 5));
106
 
107
      southBox.add(Box.createHorizontalGlue());
108
      southBox.add(new JLabel("Red: "));
109
      southBox.add(redValueL);
110
      southBox.add(Box.createHorizontalStrut(5));
111
      southBox.add(new JLabel("Green: "));
112
      southBox.add(greenValueL);
113
      southBox.add(Box.createHorizontalStrut(5));
114
      southBox.add(new JLabel("Blue: "));
115
      southBox.add(blueValueL);
116
      southBox.add(Box.createHorizontalGlue());
117
 
118
      getContentPane().add(captureP, BorderLayout.CENTER);
119
      getContentPane().add(southBox, BorderLayout.SOUTH);
120
 
121
      if(colorMapP != null) {
122
         captureP.addMouseListener(new MouseHandler());
123
      }
124
 
125
   }
126
 
127
   private JMenuBar createMenuBar() {
128
      JMenuBar menubar = new JMenuBar();
129
 
130
      JMenu fileM = new JMenu("File");
131
 
132
      fileM.add(new ProxyAction(this, "save", "Save", 's'));
133
      fileM.add(new ProxyAction(this, "pack", "Reset Size", 'r'));
134
      fileM.addSeparator();
135
      fileM.add(new ProxyAction(this, "dispose", "Exit", 'x'));
136
 
137
      menubar.add(fileM);
138
 
139
      return menubar;
140
   }
141
 
142
   public void save() {
143
 
144
      javax.swing.filechooser.FileFilter[] filters = AVRSystem.FILE_CHOOSER.getChoosableFileFilters();
145
      for(int i = 0; i < filters.length; i++) {
146
         AVRSystem.FILE_CHOOSER.removeChoosableFileFilter(filters[i]);
147
      }
148
 
149
      AVRSystem.FILE_CHOOSER.addChoosableFileFilter(
150
         new LogFileFilter("Bayer Image File (*." + AVRSystem.BAYER_FILE_EXT + ")",
151
                           "." + AVRSystem.BAYER_FILE_EXT));
152
 
153
      AVRSystem.FILE_CHOOSER.setSelectedFile(new File(filename));
154
      int option = AVRSystem.FILE_CHOOSER.showSaveDialog(getDesktopPane().getRootPane());
155
      if(option == JFileChooser.APPROVE_OPTION) {
156
         try {
157
            File file = AVRSystem.FILE_CHOOSER.getSelectedFile();
158
            if(!file.getName().toLowerCase().endsWith(AVRSystem.BAYER_FILE_EXT)) {
159
               file = new File(file.getName() + "." + AVRSystem.BAYER_FILE_EXT);
160
            }
161
            captureP.saveBayer(file);
162
         } catch(IOException ioe) {
163
            ioe.printStackTrace();
164
            AVRSystem.LOG.severe(ioe.getMessage());
165
         }
166
      }
167
   }
168
 
169
   public void setVisible(boolean visible) {
170
      if(fromCamera) {
171
         if(visible) {
172
            try {
173
               dataHandler = new DumpFrameHandler();
174
               AVRSystem.DEVICE.addDataListener(dataHandler);
175
               getDesktopPane().getRootPane().getGlassPane().setVisible(true);
176
               AVRSystem.DEVICE.sendDumpFrame();
177
               messageP.append("Capture Snapshot");
178
            } catch(IOException ioe) {
179
               AVRSystem.DEVICE.removeDataListener(dataHandler);
180
               AVRSystem.LOG.severe(ioe.getMessage());
181
               messageP.append("Capture not sent");
182
            }
183
         } else {
184
            AVRSystem.DEVICE.removeDataListener(dataHandler);
185
         }
186
      }
187
 
188
      super.setVisible(visible);
189
   }
190
 
191
   private Point translatePointToImage(Point mouse) {
192
 
193
      Dimension size = captureP.getSize();
194
      Insets insets = captureP.getInsets();
195
      Dimension preferredSize = captureP.getPreferredSize();
196
 
197
      double scaleX = size.width / (double)preferredSize.width;
198
      double scaleY = size.height / (double)preferredSize.height;
199
      double scale = Math.min(scaleX, scaleY);
200
 
201
      int imageX = (int)(insets.left + ((5 + AVRSystem.IMAGE_WIDTH + 10) * scale));
202
      int imageY = (int)(insets.top + (5 * scale));
203
      int imageWidth = (int)(AVRSystem.IMAGE_WIDTH * scale);
204
      int imageHeight = (int)(AVRSystem.IMAGE_HEIGHT * scale);
205
 
206
      Point imagePoint = null;
207
 
208
      if(((mouse.x >= imageX) && (mouse.x < (imageX + imageWidth))) &&
209
         ((mouse.y >= imageY) && (mouse.y < (imageY + imageHeight)))) {
210
 
211
         scale = 1 / scale;
212
 
213
         int x = (int)((mouse.x * scale) - (insets.left + 5 + AVRSystem.IMAGE_WIDTH + 10));
214
         int y = (int)((mouse.y * scale) - (insets.top + 5));
215
 
216
         imagePoint = new Point(x, y);
217
      }
218
 
219
      return imagePoint;
220
 
221
   }
222
 
223
   private final class DumpFrameHandler extends DataAdapter {
224
 
225
      private int frameCount;
226
 
227
      public DumpFrameHandler() {
228
         frameCount = 0;
229
      }
230
 
231
      public void ack() {
232
         frameCount = 0;
233
      }
234
 
235
      public void nck() {
236
         getDesktopPane().getRootPane().getGlassPane().setVisible(false);
237
         AVRSystem.DEVICE.removeDataListener(this);
238
         JOptionPane.showMessageDialog(messageP.getRootPane(), "Capture NCK Received", "NCK Received", JOptionPane.ERROR_MESSAGE);
239
      }
240
 
241
      public void responseTimerExpired() {
242
         getDesktopPane().getRootPane().getGlassPane().setVisible(false);
243
         AVRSystem.DEVICE.removeDataListener(this);
244
         JOptionPane.showMessageDialog(messageP.getRootPane(), "Response Timer Expired", "Timer Expired", JOptionPane.ERROR_MESSAGE);
245
      }
246
 
247
      public void frameData(ByteBuffer data) {
248
 
249
         frameCount++;
250
         captureP.setRow(data.get() & 0xFF, data, frameCount == 0x48);
251
 
252
         if(frameCount == 0x48) {
253
            AVRSystem.DEVICE.removeDataListener(this);
254
            getDesktopPane().getRootPane().getGlassPane().setVisible(false);
255
         }
256
 
257
      }
258
 
259
   }
260
 
261
   private final class MouseHandler extends MouseAdapter {
262
 
263
      private JPopupMenu popupM;
264
      private int color;
265
 
266
      public void mouseReleased(MouseEvent me) {
267
         if(SwingUtilities.isRightMouseButton(me)) {
268
            Point imagePoint = translatePointToImage(me.getPoint());
269
 
270
            if(imagePoint != null) {
271
 
272
               color = captureP.getRGB(imagePoint.x, imagePoint.y);
273
 
274
               if(popupM == null) {
275
                  popupM = new JPopupMenu();
276
                  try {
277
 
278
                     JMenuItem colorMapMI = new JMenuItem("Add to Color Map");
279
                     colorMapMI.addActionListener(new ActionHandler());
280
                     popupM.add(colorMapMI);
281
                  } catch(Exception e) {
282
                  }
283
               }
284
 
285
               popupM.show((Component)me.getSource(), me.getX(), me.getY());
286
 
287
            }
288
 
289
         }
290
 
291
      }
292
 
293
      public final class ActionHandler implements ActionListener {
294
 
295
         private JPanel displayP = null;
296
         private JRadioButton[] colRB = null;
297
 
298
         public void actionPerformed(ActionEvent ae) {
299
 
300
            int column = 0;
301
            int option = JOptionPane.YES_OPTION;
302
 
303
            do {
304
 
305
               option = JOptionPane.YES_OPTION;
306
               column = getColorMapColumn();
307
 
308
               if(column != -1) {
309
 
310
                  if(!colorMapP.isColumnClear(column)) {
311
 
312
                     option = JOptionPane.showConfirmDialog(getRootPane(),
313
                        "Index " + column + " is already set. Overwrite current value?",
314
                        "Overwrite current index value?",
315
                        JOptionPane.
316
                        YES_NO_CANCEL_OPTION,
317
                        JOptionPane.QUESTION_MESSAGE);
318
 
319
                  }
320
               } else {
321
                  option = JOptionPane.CANCEL_OPTION;
322
               }
323
 
324
            } while(option == JOptionPane.NO_OPTION);
325
 
326
            if(option == JOptionPane.YES_OPTION) {
327
               SwingUtilities.getWindowAncestor(colorMapP).setVisible(true);
328
               colorMapP.setColor(column, color);
329
            }
330
         }
331
 
332
         private int getColorMapColumn() {
333
            if(displayP == null) {
334
               displayP = new JPanel(new BorderLayout());
335
 
336
               JPanel selectColP = new JPanel();
337
 
338
               ButtonGroup bg = new ButtonGroup();
339
 
340
               colRB = new JRadioButton[8];
341
               for(int i = 0; i < colRB.length; i++) {
342
                  colRB[i] = new JRadioButton((i + 1) + "");
343
                  bg.add(colRB[i]);
344
                  selectColP.add(colRB[i]);
345
               }
346
 
347
               colRB[0].setSelected(true);
348
 
349
               displayP.add(new JLabel("Select Color Map Column:"), BorderLayout.NORTH);
350
               displayP.add(selectColP, BorderLayout.SOUTH);
351
            }
352
 
353
            int option = JOptionPane.showConfirmDialog(getRootPane(),
354
                                                       displayP,
355
                                                       "Add to Color Map Column",
356
                                                       JOptionPane.OK_CANCEL_OPTION,
357
                                                       JOptionPane.QUESTION_MESSAGE);
358
 
359
            int selected = -1;
360
 
361
            if(option == JOptionPane.OK_OPTION) {
362
               for(int i = 0; selected == -1 && i < colRB.length; i++) {
363
                  if(colRB[i].isSelected()) {
364
                     selected = i;
365
                  }
366
               }
367
 
368
            }
369
 
370
            return selected;
371
 
372
         }
373
 
374
      }
375
 
376
   }
377
 
378
   private final class MouseMotionHandler implements MouseMotionListener {
379
 
380
      public void mouseMoved(MouseEvent me) {
381
 
382
         Point imagePoint = translatePointToImage(me.getPoint());
383
 
384
         if(imagePoint != null) {
385
 
386
            int color = captureP.getRGB(imagePoint.x, imagePoint.y);
387
 
388
            redValueL.setText(((color & 0xFF0000) >> 16) + "");
389
            greenValueL.setText(((color & 0x00FF00) >> 8) + "");
390
            blueValueL.setText(((color & 0x0000FF) >> 0) + "");
391
 
392
         } else {
393
 
394
            redValueL.setText("");
395
            greenValueL.setText("");
396
            blueValueL.setText("");
397
 
398
         }
399
 
400
      }
401
 
402
      public void mouseDragged(MouseEvent me) {
403
      }
404
   }
405
 
406
}