江蘇520

          江蘇520

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          java利用jmf實(shí)現(xiàn)拍照功能

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

          然后,說一下需求

          1. 用攝像頭拍照

          2. 在文本框輸入文件名

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

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

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

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

          利用JMF,代碼很簡單:

          //利用這三個(gè)類分別獲取攝像頭驅(qū)動(dòng),和獲取攝像頭內(nèi)的圖像流,獲取到的圖像流是一個(gè)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();
                        }

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

          代碼也是很簡單:

           

           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)//如果按下的是拍照按鈕 http://www.5a520.cn
                        {
                        FrameGrabbingControl fgc =(FrameGrabbingControl)player.getControl
                      ("javax.media.control.FrameGrabbingControl");
                        buf = fgc.grabFrame(); // 獲取當(dāng)前禎并存入Buffer類 http://www.bt285.cn
                        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);//不壓縮圖像 http://www.bt285.cn
                        encoder.setJPEGEncodeParam(param);
                        try
                        {
                        encoder.encode(bi);
                        out.close();
                        }
                        catch (java.io.IOException io)
                        {
                        System.out.println("IOException");
                        }

          已經(jīng)申請(qǐng)將JWebCam建立為一個(gè)開源項(xiàng)目,放到GRO,大家發(fā)揮自己的想象力加入自己的代碼吧,比如拍攝視頻,添加圖像處理功能,等等

          posted on 2009-05-21 21:03 江蘇520 閱讀(1683) 評(píng)論(0)  編輯  收藏

          主站蜘蛛池模板: 泗洪县| 进贤县| 汝阳县| 辽源市| 吐鲁番市| 中阳县| 彰武县| 屏边| 白河县| 钦州市| 平度市| 嵊泗县| 房产| 澄城县| 平湖市| 宝丰县| 连山| 郁南县| 依兰县| 绵竹市| 兴海县| 宜春市| 宿州市| 大渡口区| 吉木乃县| 无为县| 荣成市| 龙门县| 武胜县| 通州区| 镇沅| 岗巴县| 博客| 康马县| 和田市| 深圳市| 九寨沟县| 九江县| 霸州市| 军事| 清远市|