jsp 摄像头处理实例教程



B/S架构的摄像头控制是完全可以的,小弟做过这个工作。附上源代码,有很多地方没有修改,不过绝对可以运行的,如果需要保存可以使用数字证书打包,  
  
 package   com.etong.util.webVideo;  
  
 import   javax.swing.*;  
 import   java.io.*;  
 import   java.net.MalformedURLException;  
 import   java.net.URL;  
 import   java.net.URLConnection;  
 import   java.text.SimpleDateFormat;  
 import   java.util.ArrayList;  
  
 import   javax.media.*;  
 import   javax.media.format.*;  
 import   javax.media.util.*;  
 import   javax.media.control.*;  
  
 import   java.applet.Applet;  
 import   java.awt.*;  
 import   java.awt.image.*;  
 import   java.awt.event.*;  
  
 import   jmapps.util.*;  
  
 import   com.sun.image.codec.jpeg.*;  
 import   javax.media.protocol.DataSource;  
 import   java.applet.AppletContext;  
  
 public   class   WebCang   extends   JApplet   implements   ActionListener   {  
         public   static   Player   player   =   null;  
  
         private   CaptureDeviceInfo   di   =   null;  
  
         private   MediaLocator   ml   =   null;  
  
         String   insid   =   null;  
  
         String   taxpayerid   =   null;  
  
         String   affID   =   null;  
  
         String   affname   =   null;  
  
         String   wfid   =   null;  
  
         String   username   =   null;  
  
         String   host   =   null;  
  
         String   aid   =   null;  
  
         String   tid   =   null;  
  
         int   xx   =   0;  
  
          
         private   JButton   capture   =   null;  
  
          
         private   JButton   save   =   null;  
  
         private   JButton   open   =   null;  
  
         private   JButton   close   =   null;  
  
         private   JTextField   num   =   null;  
  
         private   Buffer   buf   =   null;  
  
         private   Image   img   =   null;  
  
           private   Image   img2   =   null;  
  
         //   private   VideoFormat   vf   =   null;  
         private   BufferToImage   btoi   =   null;  
  
         private   ImagePanel   imgpanel   =   null;  
  
          
         private   int   rectX;  
  
         private   int   rectY;  
  
         private   int   rectWidth   =   320;  
  
         private   int   rectHeight   =   240;  
  
         private   int   imgWidth   =   320;  
  
         private   int   imgHeight   =   240;  
  
  
          
         private   String   fname   =   ”工作流附件程序”;  
  
         public   void   init()   {  
                 insid   =   this.getParameter(“insid”);  
                 taxpayerid   =   this.getParameter(“taxpayerid”);  
                 affID   =   this.getParameter(“affID”);  
                 affname   =   this.getParameter(“affname”);  
                 wfid   =   this.getParameter(“wfid”);  
                 username   =   this.getParameter(“username”);  
                 host   =   this.getParameter(“host”);  
                 aid   =   this.getParameter(“aid”);  
                 tid   =   this.getParameter(“tid”);  
  
  
                 setLayout(new   BorderLayout());  
                 setSize(320,   240);  
  
                 imgpanel   =   new   ImagePanel();  
                 imgpanel.addMouseMotionListener(imgpanel);  
                 capture   =   new   JButton(“拍照”);  
                 capture.addActionListener(this);  
                 save   =   new   JButton(“上传该附件”);  
                 save.addActionListener(this);  
                 open   =   new   JButton(“打开拍照”);  
                 open.addActionListener(this);  
                 close   =   new   JButton(“关闭拍照”);  
                 close.addActionListener(this);  
                 //num   =   new   JTextField();  
  
                 String   str1   =   ”vfw:Logitech   USB   Video   Camera:0″;  
                 String   str2   =   ”vfw:Microsoft   WDM   Image   Capture   (Win32):0″;  
                 //从驱动管理器中获取驱动,一般使用MicroOS默认的驱动,str1为罗技的摄像头驱动  
                 di   =   CaptureDeviceManager.getDevice(str2);  
                 //获得本地媒体源  
                 ml   =   di.getLocator();  
                 try   {  
                         //通过获取的本地源建立播放者对象  
                         DataSource   dataSource;  
                         //player对象可以使用Manager.createRealizedPlayer(di.getLocator())直接构造,  
                         //player   =   Manager.createRealizedPlayer(di.getLocator());  
                         dataSource   =   JMFUtils.createCaptureDataSource(null,   null,   str2,  
                                         di.getFormats()[4]);  
                         //如果自行设置格式应当使用DataSource对象构建,其中DataSource对象位置为javax.media.protocol.DataSource;  
                         player   =   Manager.createRealizedPlayer(dataSource);  
                         //播放者开始播放  
                         player.start();  
                         Panel   panelx2   =   new   Panel(new   GridLayout(1,   2));  
                         Component   comp;  
                         if   ((comp   =   player.getVisualComponent())   !=   null)   {  
                                 //增加播放源在窗口上  
                                 panelx2.add(comp);  
                                 comp.setSize(new   Dimension(640,   480));  
                         }  
                         Panel   panelx   =   new   Panel(new   GridLayout(1,   7));  
                         Panel   panel1   =   new   Panel(new   BorderLayout());  
                         panelx.add(new   Panel());  
                         panelx.add(capture);  
                         panelx.add(new   Panel());  
                         panelx.add(save);  
                         panelx.add(new   Panel());  
                         panel1.add(new   Label(),   BorderLayout.WEST);  
                         //panel1.add(num,   BorderLayout.CENTER);  
                         //panelx.add(open);  
                         panelx.add(close);  
                         panelx.add(new   Panel());  
                         panel1.add(panelx2,   BorderLayout.NORTH);  
                         panel1.add(panelx,   BorderLayout.SOUTH);  
                         add(panel1,   BorderLayout.CENTER);  
                         panelx2.add(imgpanel);  
                         //add(panelx2,BorderLayout.NORTH);  
                 }   catch   (Exception   e)   {  
                         e.printStackTrace();  
                 }  
         }  
  
         public   static   void   main(String[]   args)   throws   ClassNotFoundException   {  
  
                 try   {  
                         URL   url   =   new   URL(  
                                         ”http://webserver:7001/Workflow/servlet/WebCang”);  
  
                         URLConnection   urlConn   =   url.openConnection();  
                         urlConn.setUseCaches(false);  
                         urlConn.setDefaultUseCaches(false);  
  
                 }   catch   (MalformedURLException   e1)   {  
  
                         e1.printStackTrace();  
                 }   catch   (IOException   e)   {  
  
                         e.printStackTrace();  
                 }  
  
         }  
          
         public   static   void   playerclose()   {  
                 player.close();  
                 player.deallocate();  
         }  
  
          
         public   void   actionPerformed(ActionEvent   e)   {  
                 JComponent   c   =   (JComponent)   e.getSource();  
                 if   (c   ==   capture)   {   //   抓取当前frame  
                         FrameGrabbingControl   fgc   =   (FrameGrabbingControl)   player  
                                                                               .getControl(  
                                         ”javax.media.control.FrameGrabbingControl”);  
                         //public   Control   getControl(java.lang.String   forName)获得由forName类型的control或接口interface  
                          
                          
                         buf   =   fgc.grabFrame();  
                         //public   Buffer   grabFrame(),获取当前祯  
                          
                          
                         btoi   =   new   BufferToImage((VideoFormat)   buf.getFormat());//转化流格式  
                         //public   BufferToImage(VideoFormat   format),为指定格式的图象变换对象实例化一个缓冲区  
                          
                         img   =   btoi.createImage(buf);   //   显示抓取图片  
                         //public   java.awt.Image   createImage(Buffer   buffer),  
                         //将输入流转换成一个标准的AWT格式的图象,并且返回这个图象。  
                          
                         imgpanel.setImage(img);   //   设置imgpanel图片属性  
                         img2   =   img;  
                         //new   File(“c:/load/WorkflowPhoto/temp.jpg”).delete();  
                         JOptionPane.showMessageDialog(this,”拍照成功”,”操作提示”,0);  
                 }   else   if   (c   ==   save)   {  
                         if   (img   !=   null)   {  
                                 //fname   =   !num.getText().equals(“”)   ?   num.getText()   :   ”temp”;  
                                 fname   =   ”temp”;  
                                 xx++;  
                                 saveJPG(img2,   ”c:/WorkflowPhoto/”   ,   fname+   xx   +  “.jpg”);  
                                 //WebCangFile.saveJPG(img,”f:/”+   fname   +   ”.jpg”);  
                         }  
                 }else   if   (c   ==   close){  
                         player.close();  
                         JOptionPane.showMessageDialog(this,”拍照程序已经关闭,如需再次使用请关闭窗口,重新开启!”,”操作提示”,0);  
                 }