/* * @(#) WebCam.java * * COMP 150-TUI * Michael Horn (michael.horn@tufts.edu) * Tufts University, Department of Computer Science */ import java.awt.Image; import java.awt.event.*; import javax.swing.Timer; import javax.media.*; import javax.media.format.*; import javax.media.util.BufferToImage; import javax.media.control.FormatControl; import javax.media.control.FrameGrabbingControl; public class WebCam implements ActionListener { public static final String DEVICE = "vfw:Microsoft WDM Image Capture (Win32):0"; protected Timer timer; protected Player player; protected Scanner scanner; protected FrameGrabbingControl fgc; public WebCam() throws Exception { CaptureDeviceInfo cdi = CaptureDeviceManager.getDevice(DEVICE); this.timer = new Timer(500, this); this.player = Manager.createRealizedPlayer(cdi.getLocator()); this.fgc = (FrameGrabbingControl)player.getControl( "javax.media.control.FrameGrabbingControl"); //------------------------------------- // Change to 640x480 format //------------------------------------- //FormatControl fc = (FormatControl) //player.getControl("javax.media.control.FormatControl"); //Format[] formats = fc.getSupportedFormats(); //fc.setFormat(formats[4]); } public void start() { this.player.start(); this.timer.start(); } public void stop() { this.timer.stop(); this.player.stop(); } public void close() { this.player.close(); this.player.deallocate(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == timer) { //---------------------------------- // Grab a frame //---------------------------------- Buffer buf = fgc.grabFrame(); //---------------------------------- // Convert it to an image //---------------------------------- BufferToImage btoi = new BufferToImage( (VideoFormat)buf.getFormat()); Image img = btoi.createImage(buf); //---------------------------------- // Process image here... //---------------------------------- } } }