Page 197 - The Definitive Guide to Building Java Robots
P. 197
Preston_5564C06.fm Page 178 Friday, September 23, 2005 5:13 AM
178 CHAPTER 6 ■ VISION
Figure 6-7. GetFrame with RGB samples (Coke, 7-Up, and Pepsi)
Displaying one frame isn’t of much use. In order to do some processing, it would be nice
to get more than one image, and it would be even better to get a few images per second.
Code Objective
The objective here is to display more than one frame and see live video in a JFrame.
Code Discussion
In this class, I’ll use our GetFrame class, but I’ll need to get those frames at an interval using
a timer.
The class extends SimpleSwing, where the fields are the timer, getFrame, imagePanel, and
a constant for the default URL for your webcam called DEFAULT_CAMERA. The constructors
populate the DEFAULT_CAMERA or one sent in the constructor, and then call the init()
method.
The init() method does the same as the ImageViewer, except it schedules the getPic() task
to run. The method getPic() sets the image from the getFrame.getBufferedImage() method into
the imagePanel.
The next two methods are start() and stop(). I added these for greater control regarding
when the camera is processing and when it’s not.
Later, I’ll extend this class to include some image processing. To do this, I’ll add two
assessors—getFrame() and getFps()—and one setter setFps(). See Example 6-8.
Example 6-8. WebCamViewer.java
package com.scottpreston.javarobot.chapter6;
import java.awt.BorderLayout;
import java.util.Timer;
import java.util.TimerTask;
import com.scottpreston.javarobot.chapter2.Utils;
97022d2480fe4a63cfdfa123a6e70098