001package ca.bc.webarts.tools; 002 003import java.awt.BorderLayout; 004import java.awt.Component; 005import java.awt.Frame; 006import java.awt.Graphics2D; 007import java.awt.Image; 008import java.awt.event.WindowAdapter; 009import java.awt.event.WindowEvent; 010import java.awt.image.BufferedImage; 011import java.io.File; 012import java.util.Vector; 013import javax.imageio.ImageIO; 014import javax.media.*; 015import javax.media.control.FrameGrabbingControl; 016import javax.media.format.*; 017import javax.media.format.VideoFormat; 018import javax.media.util.BufferToImage; 019 020/** 021 * 022 * @author BUDDHIMA 023 */ 024public class WebCam 025{ 026 027 CaptureDeviceInfo device; 028 MediaLocator ml; 029 Player player; 030 Component videoScreen; 031 032 public static void main(String [] args) 033 { 034 new WebCam(); // create a new instance of WebCam in main function 035 } 036 037 public WebCam() 038 { 039 try 040 { 041 // gets a list of devices how support the given videoformat 042 Vector deviceList = CaptureDeviceManager.getDeviceList(new JPEGFormat()); 043 deviceList.addAll(CaptureDeviceManager.getDeviceList(new RGBFormat())); 044 deviceList.addAll(CaptureDeviceManager.getDeviceList(new YUVFormat())); 045 deviceList.addAll(CaptureDeviceManager.getDeviceList(new H263Format())); 046 deviceList.addAll(CaptureDeviceManager.getDeviceList(new H261Format())); 047 //deviceList.addAll(CaptureDeviceManager.getDeviceList(new AudioFormat("PCM, 44.1 KHz, Stereo, Signed"))); 048 System.out.println(deviceList.toString()); 049 050 // gets the first device in deviceList 051 device = (CaptureDeviceInfo) deviceList.firstElement(); 052 053 ml = device.getLocator(); 054 055 player = Manager.createRealizedPlayer(ml); 056 057 player.start(); 058 059 videoScreen = player.getVisualComponent(); 060 Frame frm = new Frame(); 061 frm.setBounds(10, 10, 900, 700); // sets the size of the screen 062 063 // setting close operation to the frame 064 frm.addWindowListener(new WindowAdapter() 065 { 066 067 public void windowClosing(WindowEvent we) 068 { 069 System.exit(0); 070 } 071 } ); 072 073 // place player and video screen on the frame 074 frm.add(videoScreen, BorderLayout.CENTER); 075 frm.add(player.getControlPanelComponent(), BorderLayout.SOUTH); 076 frm.setVisible(true); 077 078 // capture image 079 Thread.sleep(10000); // wait 10 seconds before capturing photo 080 081 FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); 082 083 Buffer buf = fgc.grabFrame(); // grab the current frame on video screen 084 085 BufferToImage btoi = new BufferToImage((VideoFormat) buf.getFormat()); 086 087 Image img = btoi.createImage(buf); 088 089 saveImagetoFile(img, "MyPhoto.jpg"); // save the captured image as MyPhoto.jpg 090 091 } 092 catch (Exception e) 093 { 094 System.out.println(e); 095 e.printStackTrace(); 096 } 097 } 098 099 private void saveImagetoFile(Image img, String string) 100 { 101 try 102 { 103 int w = img.getWidth(null); 104 int h = img.getHeight(null); 105 BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 106 Graphics2D g2 = bi.createGraphics(); 107 108 g2.drawImage(img, 0, 0, null); 109 110 g2.dispose(); 111 112 String fileType = string.substring(string.indexOf('.') + 1); 113 114 ImageIO.write(bi, fileType, new File(string)); 115 116 } 117 catch (Exception e) 118 { 119 } 120 } 121}