当前位置:首页
开发技术指南» 文章正文
    引言:

 ·关于连接字串的问题.    »显示摘要«
    摘要: 我使用oledbconnection连接一个access文件,本地使用绝对路径开发和使用都没有问题.但是到了服务器上就不能使用绝对路径了,这时如果使用相对路径如:data\data.mdb之类的也不行,服务器上使用http://www.xxx.com/data/data.mdb这样的路径也不行.到底该怎么写这个连接字串呢? ......
    摘要: 表结构如下: student表------------------------------------ |字段名 | 类 型 | 长度 | ------------------------------------ | id | integer | | ------------------------------------ | name | varchar | 10 | ---......


Java中利用JMF编写摄像头拍照程序(转贴精华贴,详解)

(下面这个帖子相信很多人研究过,但是搞了两天都还没有把它做出来,也看了JWebCam开源项目,我既编译不了也运行不了它。超郁闷,求大虾急救,在线等,QQ:18401759)  
   
  我把程序分为两种,有趣的和无趣的,最近做了几个有趣的项目,其中一个,应当就算是摄像头拍照程序了。用于现场拍照,生成照片,主要用到Java   Media   Framework(JMF)。    
   
  首先到SUN下载最新的JMF,然后安装。http://java.sun.com/products/java-media/jmf/index.jsp  
   
  然后,说一下需求  
   
  1.   用摄像头拍照  
   
  2.   在文本框输入文件名  
   
  3.   按下拍照按钮,获取摄像头内的图像  
   
  4.   在拍下的照片上有一红框截取固定大小的照片。  
   
  5.   保存为本地图像为jpg格式,不得压缩画质  
   
  技术关键,相信也是大家最感兴趣的部分也就是如何让一个摄像头工作,并拍下一张照片了。  
   
  利用JMF,代码很简单:  
   
  //利用这三个类分别获取摄像头驱动,和获取摄像头内的图像流,获取到的图像流是一个Swing的Component组件类  
   
  public   static   Player   player   =   null;  
  private   CaptureDeviceInfo   di   =   null;  
  private   MediaLocator   ml   =   null;  
   
  //文档中提供的驱动写法,为何这么写我也不知:)  
   
  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);  
  }  
  }  
  catch   (Exception   e)  
  {  
  e.printStackTrace();  
  }  
  接下来就是点击拍照,获取摄像头内的当前图像。  
   
  代码也是很简单:  
   
  private   JButton   capture;  
  private   Buffer   buf   =   null;  
  private   BufferToImage   btoi   =   null;  
  private   ImagePanel   imgpanel   =   null;  
  private   Image   img   =   null;  
  private   ImagePanel   imgpanel   =   null;  
   
  JComponent   c   =   (JComponent)   e.getSource();  
  if   (c   ==   capture)//如果按下的是拍照按钮    
  {    
  FrameGrabbingControl   fgc   =(FrameGrabbingControl)   player.getControl("javax.media.control.FrameGrabbingControl");  
  buf   =   fgc.grabFrame();   //   获取当前祯并存入Buffer类  
  btoi   =   new   BufferToImage((VideoFormat)   buf.getFormat());  
  img   =   btoi.createImage(buf);   //   show   the   image    
  imgpanel.setImage(img);  
  }  
  保存图像的就不多说了,以下为示例代码  
   
  BufferedImage   bi   =   (BufferedImage)   createImage(imgWidth,   imgHeight);  
  Graphics2D   g2   =   bi.createGraphics();  
  g2.drawImage(img,   null,   null);  
  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");  
  }  
  已经申请将JWebCam建立为一个开源项目,放到GRO,大家发挥自己的想象力加入自己的代码吧,比如拍摄视频,添加图像处理功能,等等。

NO.1   作者: troyzhang

package   com.redtroy;  
   
  import   javax.media.Player;  
  import   javax.media.CaptureDeviceInfo;  
  import   javax.media.MediaLocator;  
  import   javax.swing.*;  
  import   java.awt.*;  
  import   java.awt.image.*;  
  import   java.awt.event.*;  
  import   javax.media.control.FrameGrabbingControl;  
  import   javax.media.Buffer;  
  import   javax.media.util.BufferToImage;  
  import   javax.media.format.VideoFormat;  
  import   java.io.*;  
  import   com.sun.image.codec.jpeg.*;  
  import   javax.media.CaptureDeviceManager;  
  import   javax.media.Manager;  
  import   java.net.*;  
  import   java.applet.*;  
   
  //import   javax.swing.im  
  /**  
    *   <p>Title:   </p>  
    *   <p>Description:   </p>  
    *   <p>Copyright:   Copyright   (c)   2005</p>  
    *   <p>Company:   </p>  
    *   @author   not   attributable  
    *   @version   1.0  
    */  
   
  public   class   Camera  
          extends   JFrame   {  
      private   static   Player   player   =   null;  
      private   CaptureDeviceInfo   device   =   null;  
      private   MediaLocator   locator   =   null;  
      private   Buffer   buffer   =   null;  
      private   BufferToImage   b2i   =   null;  
      private   Image   image;  
      private   ImageIcon   iicon   =   new   ImageIcon();  
      boolean   proportion   =   true;  
      String   str1   =   "vfw:Logitech   USB   Video   Camera:0";  
      String   str2   =   "vfw:Microsoft   WDM   Image   Capture   (Win32):0";  
      JButton   jButton1   =   new   JButton();  
      JButton   jButton2   =   new   JButton();  
      Component   component1;  
      JLabel   jLabel1   =   new   JLabel();  
      public   Camera()   {  
          super("troy的摄像机(无保存功能)");  
          try   {  
              jbInit();  
          }  
          catch   (Exception   e)   {  
              e.printStackTrace();  
          }  
      }  
   
      public   Image   resize(int   width,   int   height,   Image   source,   boolean   flag)   {  
          this.proportion   =   flag;  
          int   new_w;  
          int   new_h;  
          Toolkit   tk   =   Toolkit.getDefaultToolkit();  
          Applet   app   =   new   Applet();  
          MediaTracker   mt   =   new   MediaTracker(app);  
          Image   img   =   source;  
          try   {  
              mt.addImage(img,   0);  
              mt.waitForID(0);  
          }  
          catch   (Exception   e)   {  
              e.printStackTrace();  
          }  
          if   (img.getWidth(null)   ==   -1)   {  
              System.out.println("   cant   read,retry!"   +   "<BR>");  
              return   null;  
          }  
          else   {  
   
              if   (this.proportion   ==   true)   {   //判断是否是等比缩放.  
  //为等比缩放计算输出的图片宽度及高度  
                  double   rate1   =   (   (double)   img.getWidth(null))   /   (double)   width   +  
                          0.1;  
                  double   rate2   =   (   (double)   img.getHeight(null))   /   (double)   height   +  
                          0.1;  
                  double   rate   =   rate1   >   rate2   ?   rate1   :   rate2;  
                  new_w   =   (int)   (   (   (double)   img.getWidth(null))   /   rate);  
                  new_h   =   (int)   (   (   (double)   img.getHeight(null))   /   rate);  
              }  
              else   {  
                  new_w   =   width;   //输出的图片宽度  
                  new_h   =   height;   //输出的图片高度  
              }  
          }  
          BufferedImage   buffImg   =   new   BufferedImage(new_w,   new_h,  
                                                                                              BufferedImage.TYPE_INT_RGB);  
   
          Graphics   g   =   buffImg.createGraphics();  
   
          g.setColor(Color.white);  
          g.fillRect(0,   0,   new_w,   new_h);  
   
          g.drawImage(img,   0,   0,   new_w,   new_h,   null);  
   
          g.dispose();  
          try   {  
              OutputStream   tempout   =   new  
                      FileOutputStream("C:\\temp.jpg");  
              JPEGImageEncoder   encoder   =   JPEGCodec.createJPEGEncoder(tempout);  
              encoder.encode(buffImg);  
              tempout.close();  
   
          }  
          catch   (Exception   e)   {  
              e.printStackTrace();  
          }  
          return   tk.createImage("C:\\temp.jpg");  
      }  
   
      public   static   void   main(String[]   args)   {  
          Camera   camera1   =   new   Camera();  
      }  
   
      private   void   jbInit()   throws   Exception   {  
   
          component1   =   Box.createGlue();  
          //=====================初始化设备===================//  
          component1.addNotify();  
          device   =   CaptureDeviceManager.getDevice(str2);  
          locator   =   device.getLocator();  
          try   {  
              player   =   Manager.createRealizedPlayer(locator);  
              player.start();  
   
              if   (   (component1   =   player.getVisualComponent())   !=   null)   {  
                  this.getContentPane().add(component1,   null);  
              }  
          }  
          catch   (Exception   e)   {  
              e.printStackTrace();  
          }  
   
          jButton1.setBounds(new   Rectangle(294,   28,   73,   25));  
          jButton1.setText("拍照");  
          jButton1.addActionListener(new   Camera_jButton1_actionAdapter(this));  
          this.getContentPane().setLayout(null);  
          jButton2.setBounds(new   Rectangle(295,   82,   73,   25));  
          jButton2.setText("保存");  
          jButton2.addActionListener(new   Camera_jButton2_actionAdapter(this));  
          component1.setBounds(new   Rectangle(27,   23,   243,   235));  
          jLabel1.setIconTextGap(4);  
          jLabel1.setText("空");  
          jLabel1.setVerticalTextPosition(SwingConstants.CENTER);  
   
          jLabel1.setBounds(new   Rectangle(293,   139,   80,   95));  
   
          this.getContentPane().add(jButton1,   null);  
          this.getContentPane().add(jButton2,   null);  
          this.getContentPane().add(jLabel1,   null);  
          this.setSize(400,   300);  
          this.setVisible(true);  
      }  
   
      void   jButton1_actionPerformed(ActionEvent   e)   {  
          iicon   =   new   ImageIcon();  
          FrameGrabbingControl   fgc   =   (FrameGrabbingControl)   player.getControl(  
                  "javax.media.control.FrameGrabbingControl");  
          buffer   =   fgc.grabFrame();   //   获取当前祯并存入Buffer类  
          b2i   =   new   BufferToImage(   (VideoFormat)   buffer.getFormat());  
          image   =   b2i.createImage(buffer);   //   show   the   image  
          iicon   =   new   ImageIcon();  
          /*   Toolkit   tk   =   Toolkit.getDefaultToolkit();  
            Applet   app   =   new   Applet();  
            MediaTracker   mt   =   new   MediaTracker(app);*/  
          iicon.setImage(this.resize(85,   95,   image,   true));  
          jLabel1.setIcon(iicon);  
   
      }  
   
      void   jButton2_actionPerformed(ActionEvent   e)   {  
   
          JFileChooser   filechooser   =   new   JFileChooser();  
          filechooser.setFileHidingEnabled(true);  
   
          filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
          int   result   =   filechooser.showSaveDialog(null);  
          if   (result   ==   JFileChooser.CANCEL_OPTION)   {  
              return;  
          }  
          File   file   =   filechooser.getSelectedFile();  
          if   (file   ==   null   ||   file.getName().equals(""))   {  
              JOptionPane.showMessageDialog(null,   "无效的文件名",   "警告",  
                                                                          JOptionPane.ERROR_MESSAGE);  
          }  
          else   {  
   
              String   s   =   file.getAbsolutePath();  
   
              BufferedImage   bi   =   (BufferedImage)   createImage(image.getWidth(null),  
                      image.getHeight(null));  
              Graphics2D   g2   =   bi.createGraphics();  
              g2.drawImage(image,   null,   null);  
              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");  
              }  
          }  
   
      }  
   
  }  
   
  class   Camera_jButton1_actionAdapter  
          implements   java.awt.event.ActionListener   {  
      Camera   adaptee;  
   
      Camera_jButton1_actionAdapter(Camera   adaptee)   {  
          this.adaptee   =   adaptee;  
      }  
   
      public   void   actionPerformed(ActionEvent   e)   {  
          adaptee.jButton1_actionPerformed(e);  
      }  
  }  
   
  class   Camera_jButton2_actionAdapter  
          implements   java.awt.event.ActionListener   {  
      Camera   adaptee;  
   
      Camera_jButton2_actionAdapter(Camera   adaptee)   {  
          this.adaptee   =   adaptee;  
      }  
   
      public   void   actionPerformed(ActionEvent   e)   {  
          adaptee.jButton2_actionPerformed(e);  
      }  
  }  
   
  给分啊,楼主


 ·web中的水晶报表能打印吗    »显示摘要«
    摘要: rt ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE