网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Java编程 >> 文章正文
  jmf摄像头applet            【字体:
jmf摄像头applet
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    
正在装载数据……

  import   javax.swing.*;  
  import   java.io.*;  
  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   com.sun.image.codec.jpeg.*;  
   
   
  public   class   WebCang   extends   JApplet   implements   ActionListener  
  {  
          public   static   Player   player   =   null;  
          private   CaptureDeviceInfo   di   =   null;  
          private   MediaLocator   ml   =   null;  
          /**  
            *   瞻钮  
            */  
          private   JButton   capture   =   null;  
          /**  
            *   姘磁?  
            */  
          private   JButton   save   =   null;  
          private   JTextField   num   =   null;  
          private   Buffer   buf   =   null;  
          private   Image   img   =   null;  
          //private   VideoFormat   vf   =   null;  
          private   BufferToImage   btoi   =   null;  
          private   ImagePanel   imgpanel   =   null;  
   
          /**  
            *   选取x,y,width,height默值  
            */  
          private   int   rectX;  
          private   int   rectY;  
          private   int   rectWidth   =   150;  
          private   int   rectHeight   =   200;  
          private   int   imgWidth   =   320;  
          private   int   imgHeight   =   240;  
          /**  
            *   默媳募  
            */  
          private   String   fname   =   "test";  
   
          public   WebCang()  
          {  
                  setLayout(new   BorderLayout());  
                  setSize(320,   550);  
   
                  imgpanel   =   new   ImagePanel();  
                  imgpanel.addMouseMotionListener(imgpanel);  
                  capture   =   new   JButton("");  
                  capture.addActionListener(this);  
                  save   =   new   JButton("");  
                  save.addActionListener(this);  
                  num   =   new   JTextField();  
   
                  String   str1   =   "vfw:Logitech   USB   Video   Camera:0";  
                  String   str2   =   "vfw:Microsoft   WDM   Image   Capture   (Win32):0";  
                  di   =   CaptureDeviceManager.getDevice(str2);  
                  ml   =   di.getLocator();  
                  try  
                  {  
                          player   =   Manager.createRealizedPlayer(ml);  
                          player.start();  
                          Component   comp;  
                          if   ((comp   =   player.getVisualComponent())   !=   null)  
                          {  
                                  add(comp,   BorderLayout.NORTH);  
                          }  
                          Panel   panel1   =   new   Panel(new   BorderLayout());  
                          panel1.add(capture,   BorderLayout.NORTH);  
                          panel1.add(new   Label("应募:"),   BorderLayout.WEST);  
                          panel1.add(num,   BorderLayout.CENTER);  
                          panel1.add(save,   BorderLayout.SOUTH);  
                          add(panel1,   BorderLayout.CENTER);  
                          add(imgpanel,   BorderLayout.SOUTH);  
                  }  
                  catch   (Exception   e)  
                  {  
                          e.printStackTrace();  
                  }  
          }  
   
          public   static   void   main(String[]   args)  
          {  
                  JFrame   f   =   new   JFrame("--窕埃");  
              //     Frame   f   =   new   Frame("殖粘   BCU   WebCam   Application");  
                  WebCang   cf   =   new   WebCang();  
                  f.addWindowListener(new   WindowAdapter()  
                  {  
                          public   void   windowClosing(WindowEvent   e)  
                          {  
                                  playerclose();  
                                  System.exit(0);  
                          }  
                  });  
                  f.add("Center",   cf);  
                  f.pack();  
                  f.setSize(new   Dimension(320,   590));  
                  f.setVisible(true);  
          }  
          /**  
            *   乇头  
            *  
            */  
          public   static   void   playerclose()  
          {  
                  player.close();  
                  player.deallocate();  
          }  
          /**  
            *    
            */  
          public   void   actionPerformed(ActionEvent   e)  
          {  
                  JComponent   c   =   (JComponent)   e.getSource();  
                  if   (c   ==   capture)  
                  {   //   Grab   a   frame              
                          FrameGrabbingControl   fgc   =  
                                  (FrameGrabbingControl)   player.getControl(  
                                          "javax.media.control.FrameGrabbingControl");  
                          buf   =   fgc.grabFrame();   //   Convert   it   to   an   image              
                          btoi   =   new   BufferToImage((VideoFormat)   buf.getFormat());  
                          img   =   btoi.createImage(buf);   //   show   the   image            
                          imgpanel.setImage(img);   //   save   image  
                  }  
                  else   if   (c   ==   save)  
                  {  
                          if   (img   !=   null)  
                          {  
                                  fname   =   !num.getText().equals("")   ?   num.getText()   :   "test";  
                                  saveJPG(img,   "Photo/"   +   fname   +   ".jpg");  
                          }  
                  }  
          }  
          /**  
            *   珊示片隙围选要取牟  
            */  
          class   ImagePanel   extends   Panel   implements   MouseMotionListener  
          {  
                  private   Image   myimg   =   null;  
   
                  public   ImagePanel()  
                  {  
                          setLayout(null);  
                          setSize(imgWidth,   imgHeight);  
                  }  
                  public   void   setImage(Image   img)  
                  {  
                          this.myimg   =   img;  
                          repaint();  
                  }  
                  public   void   update(Graphics   g)  
                  {  
                          g.clearRect(0,   0,   getWidth(),   getHeight());  
                          if   (myimg   !=   null)  
                          {  
                                  g.drawImage(myimg,   0,   0,   this);  
                                  g.setColor(Color.RED);  
                                  g.drawRect(rectX,   rectY,   rectWidth,   rectHeight);  
                          }  
                  }  
                  public   void   paint(Graphics   g)  
                  {  
                          update(g);  
                  }  
   
                  public   void   mouseDragged(MouseEvent   e)  
                  {  
                          rectX   =   e.getX()   -   50;  
                          rectY   =   e.getY()   -   50;  
                          repaint();  
                  }  
   
                  public   void   mouseMoved(MouseEvent   e)  
                  {  
                  }  
          }  
          /**  
            *   图  
            *   @param   img  
            *   @param   s  
            */  
          public   void   saveJPG(Image   img,   String   s)  
          {  
                  BufferedImage   bi   =   (BufferedImage)   createImage(imgWidth,   imgHeight);  
                  /*BufferedImage   bi   =  
                          new   BufferedImage(  
                                  img.getWidth(null),  
                                  img.getHeight(null),  
                                  BufferedImage.TYPE_INT_RGB);*/  
   
                  Graphics2D   g2   =   bi.createGraphics();  
                  g2.clipRect(rectX,   rectY,   rectWidth,   rectHeight);  
                  g2.drawImage(img,   null,   null);  
                  int   moveX   =   rectX   >   0   ?   rectX   :   0;  
                  int   moveY   =   rectY   >   0   ?   rectY   :   0;  
                  int   cutWidth   =  
                          rectX   +   rectWidth   >   imgWidth  
                                  ?   rectWidth   -   ((rectX   +   rectWidth)   -   imgWidth)  
                                  :   rectWidth;  
                  int   cutHeight   =  
                          rectY   +   rectHeight   >   imgHeight  
                                  ?   rectHeight   -   ((rectY   +   rectHeight)   -   imgHeight)  
                                  :   rectHeight;  
                  bi   =   bi.getSubimage(moveX,   moveY,   cutWidth,   cutHeight);  
   
                  FileOutputStream   out   =   null;  
                  try  
                  {  
                          out   =   new   FileOutputStream(s);  
                  }  
                  catch   (java.io.FileNotFoundException   io)  
                  {  
                          System.out.println("File   Not   Found");  
                  }  
                  JPEGImageEncoder   encoder   =   JPEGCodec.createJPEGEncoder(out);  
                  JPEGEncodeParam   param   =   encoder.getDefaultJPEGEncodeParam(bi);  
                  param.setQuality(1f,   false);  
                  encoder.setJPEGEncodeParam(param);  
                  try  
                  {  
                          encoder.encode(bi);  
                          out.close();  
                  }  
                  catch   (java.io.IOException   io)  
                  {  
                          System.out.println("IOException");  
                  }  
          }  
  }  




本文来源:http://blog.csdn.net/xwchen/archive/2007/09/11/1781311.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     用java实现web服务器
     用java快速开发linux gu…
     正则表达式分解siemens交…
     [portal参考手册]目录
     jsp中调用oracle存储过程…
  • VC/MFC.CString操作指南

  • VC++ MFC DLL动态链接库编写…

  • 使用RMFormReport

  • JMS简介

  • Understanding JMF翻译

  • java.sql.SQLException: XA …

  • VC-用MFC + ADO 把jpg图象文…

  • 解决扩展MFC DLL与主程序资源…

  • J2ME中用MMAPI开发手机摄像头…

  • MFC消息详解

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David