小方的Java博客

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            27 隨筆 :: 17 文章 :: 115 評論 :: 0 Trackbacks
          首先到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, 
          nullnull);
          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");
          }
           
          posted on 2006-02-01 12:24 方佳瑋 閱讀(2136) 評論(1)  編輯  收藏 所屬分類: J2se 高級

          評論

          # re: Java中利用JMF編寫攝像頭拍照程序 2007-09-06 16:49 F
          F  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新绛县| 太康县| 金山区| 东乡| 湘潭县| 凯里市| 分宜县| 道孚县| 阿坝| 承德县| 桐庐县| 龙陵县| 茶陵县| 嘉祥县| 旌德县| 东宁县| 苏尼特左旗| 富阳市| 奈曼旗| 图们市| 临江市| 丹寨县| 华坪县| 长岭县| 贵南县| 横山县| 蓬安县| 武陟县| 大庆市| 阳朔县| 视频| 治多县| 梨树县| 万年县| 文安县| 江都市| 福安市| 南昌市| 公安县| 通许县| 梁河县|