Page 198 - The Definitive Guide to Building Java Robots
P. 198

Preston_5564C06.fm  Page 179  Friday, September 23, 2005  5:13 AM



                                                                                   CHAPTER 6  ■  VISION  179



                        public class WebCamViewer extends SimpleSwing {
                            private Timer timer = new Timer();
                            private GetFrame getFrame;
                            private ImagePanel imagePanel;
                            private int fps = 15;
                            public static final String DEFAULT_CAMERA = "vfw://0";

                            public WebCamViewer() throws Exception {
                                init(DEFAULT_CAMERA);
                            }


                            public WebCamViewer(String camera) throws Exception{
                                init(camera);
                            }

                            private void init(String camera) throws Exception{
                                setTitle("WebCamViewer");
                                // creates frame grabber
                                getFrame = new GetFrame(camera);
                                int w = 320;
                                int h = 240;
                                imagePanel =  new ImagePanel(w,h);
                                // set size of the window
                                setSize(w + 8, h+35);
                                // add imagePanel
                                getContentPane().add(imagePanel,BorderLayout.CENTER);
                                // make visible
                                setVisible(true);
                            }

                            // start the camera frame capture
                            public void start() {

                                timer.schedule(new TimerTask() {
                                    public void run() {
                                        getPic();
                                    }
                                }, 200, (int)(1000 / fps));
                            }

                            // stop the camera frame capture
                            public void stop() throws Exception{
                                timer.cancel();
                            }
   193   194   195   196   197   198   199   200   201   202   203