備注學(xué)院

          LuLu

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            5 隨筆 :: 50 文章 :: 16 評論 :: 0 Trackbacks

          首先到SUN下載最新的JMF,然后安裝。http://java.sun.com/products/java-media/jmf/index.jsp

          然后,說一下需求

          1. 用攝像頭拍照

          2. 在文本框輸入文件名

          3. 按下拍照按鈕,獲取攝像頭內(nèi)的圖像

          4. 在拍下的照片上有一紅框截取固定大小的照片。

          5. 保存為本地圖像為jpg格式,不得壓縮畫質(zhì)

          技術(shù)關(guān)鍵,相信也是大家最感興趣的部分也就是如何讓一個攝像頭工作,并拍下一張照片了。

          利用JMF,代碼很簡單:

          //利用這三個類分別獲取攝像頭驅(qū)動,和獲取攝像頭內(nèi)的圖像流,獲取到的圖像流是一個Swing的Component組件類

           

           1public static Player player = null;
           2              private CaptureDeviceInfo di = null;
           3              private MediaLocator ml = null;
           4              String str1 = "vfw:Logitech USB Video Camera:0";
           5              String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
           6              di = CaptureDeviceManager.getDevice(str2);
           7              ml = di.getLocator();
           8              try
           9              {
          10              player = Manager.createRealizedPlayer(ml);
          11              player.start();
          12              Component comp;
          13              if ((comp = player.getVisualComponent()) != null)
          14              {
          15              add(comp, BorderLayout.NORTH);
          16              }

          17              }

          18              catch (Exception e)
          19              {
          20              e.printStackTrace();
          21              }


          接下來就是點擊拍照,獲取攝像頭內(nèi)的當(dāng)前圖像。

          代碼也是很簡單:

           1private JButton capture;
           2              private Buffer buf = null;
           3              private BufferToImage btoi = null;
           4              private ImagePanel imgpanel = null;
           5              private Image img = null;
           6              private ImagePanel imgpanel = null;
           7              JComponent c = (JComponent) e.getSource();
           8              if (c == capture)//如果按下的是拍照按鈕 http://www.5a520.cn
           9              {
          10              FrameGrabbingControl fgc =(FrameGrabbingControl)player.getControl
          11            ("javax.media.control.FrameGrabbingControl");
          12              buf = fgc.grabFrame(); // 獲取當(dāng)前禎并存入Buffer類 http://www.bt285.cn
          13              btoi = new BufferToImage((VideoFormat) buf.getFormat());
          14              img = btoi.createImage(buf); // show the image
          15              imgpanel.setImage(img);
          16              }

          保存圖像的就不多說了,以下為示例代碼

           1BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
           2              Graphics2D g2 = bi.createGraphics();
           3              g2.drawImage(img, nullnull);
           4              FileOutputStream out = null;
           5              try
           6              {
           7              out = new FileOutputStream(s);
           8              }

           9              catch (java.io.FileNotFoundException io)
          10              {
          11              System.out.println("File Not Found");
          12              }

          13              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
          14              JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
          15              param.setQuality(1f, false);//不壓縮圖像 http://www.bt285.cn
          16              encoder.setJPEGEncodeParam(param);
          17              try
          18              {
          19              encoder.encode(bi);
          20              out.close();
          21              }

          22              catch (java.io.IOException io)
          23              {
          24              System.out.println("IOException");
          25              }

          From:http://www.aygfsteel.com/fundei/archive/2009/05/21/271147.html
          posted on 2009-05-27 09:40 smildlzj 閱讀(147) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 楚雄市| 阜新| 定州市| 天门市| 营山县| 宁国市| 青铜峡市| 湘潭市| 蓝田县| 东城区| 达拉特旗| 安顺市| 岑溪市| 章丘市| 扎兰屯市| 清远市| 林甸县| 盐边县| 梁山县| 桃园县| 汉川市| 区。| 洮南市| 名山县| 广宗县| 衡南县| 洛浦县| 柞水县| 突泉县| 增城市| 满城县| 泊头市| 大洼县| 宜兴市| 宁陵县| 盱眙县| 介休市| 股票| 蓝田县| 上思县| 余姚市|