posts - 59, comments - 244, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java攝像頭截圖

          Posted on 2012-06-09 01:04 penngo 閱讀(4902) 評論(2)  編輯  收藏 所屬分類: Java
          本來sun有個jmf組件可以很方便的實現(xiàn)攝像頭截圖的,不過這版本后來停止更新了,當前官網(wǎng)最新版本為Java Media Framework (JMF) 2.1.1e,下載回來,在windows 7 32位上使用,居然不能運行,網(wǎng)上另外找了個jmf的替代框架fmj使用,截圖實現(xiàn)代碼:
          package com.pengo.capture;

          import java.awt.BorderLayout;
          import java.awt.Dimension;
          import java.awt.Graphics2D;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.IOException;
          import javax.imageio.ImageIO;
          import javax.media.MediaLocator;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.JTextField;
          import net.sf.fmj.ui.application.CaptureDeviceBrowser;
          import net.sf.fmj.ui.application.ContainerPlayer;
          import net.sf.fmj.ui.application.PlayerPanelPrefs;

          public class CameraFrame extends JFrame{
              
          private static int num = 0;
              
          public CameraFrame() throws Exception{
                  
          this.setTitle("攝像頭截圖應用");
                  
          this.setSize(480500);
                  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  
          final JPanel cameraPanel = new JPanel();
                  
          this.getContentPane().setLayout(new BorderLayout());
                  
          this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
                  ContainerPlayer containerPlayer 
          = new ContainerPlayer(cameraPanel);
                  MediaLocator locator 
          = CaptureDeviceBrowser.run(null);   //彈出攝像頭設備選擇
                  
          //        MediaLocator locator = null;
          //        GlobalCaptureDevicePlugger.addCaptureDevices();
          //        Vector vectorDevices = CaptureDeviceManager.getDeviceList(null);
          //        if (vectorDevices == null || vectorDevices.size() == 0)
          //        {
          //            System.out.println("沒有攝像頭===");
          //            return;
          //        }
          //        //選擇第一個攝像頭設備
          //        for ( int i = 0;  i < vectorDevices.size();  i++ ) 
          //        {
          //            CaptureDeviceInfo infoCaptureDevice = (CaptureDeviceInfo) vectorDevices.get(i);
          //            System.out.println("設備名===============" + infoCaptureDevice.getName());
          //            //選擇第一個設備為程序使用,如果存在多個設備時,則第一個可能不是攝像頭
          //            locator = infoCaptureDevice.getLocator();
          //            break;
          //        }

                  PlayerPanelPrefs prefs 
          = new PlayerPanelPrefs();
                  containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
                  
                  JPanel btnPanel 
          = new JPanel(new BorderLayout());
                  
          final JTextField path = new JTextField("E:\\camera");
                  path.setColumns(
          30);
                  btnPanel.add(path, BorderLayout.WEST);
                  JButton okBtn 
          = new JButton("截圖");
                  okBtn.addActionListener(
          new ActionListener(){
                       
          public void actionPerformed(ActionEvent e){
                           Dimension imageSize 
          = cameraPanel.getSize();
                              BufferedImage image 
          = new BufferedImage(imageSize.width,
                                      imageSize.height, BufferedImage.TYPE_INT_ARGB);
                              Graphics2D g 
          = image.createGraphics();
                              cameraPanel.paint(g);
                              g.dispose();
                              
          try {
                         
                                  String filePath 
          = path.getText();
                                  File file 
          = new File(filePath);
                                  
          if(file.exists() == false){
                                      file.mkdirs();
                                  }

                                  ImageIO.write(image, 
          "png"new File(file.getAbsolutePath() + "/" + num + ".png"));
                                  num
          ++;
                              }
           catch (IOException ex) {
                                  ex.printStackTrace();
                                 
                              }

                       }

                  }
          );
                  btnPanel.add(okBtn, BorderLayout.EAST);
                  
          this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
              }

              
              
          public static void main(String[] args) throws Exception{
                  CameraFrame camera 
          = new CameraFrame();
                  camera.setVisible(
          true);
              }

          }

          附件源碼

          評論

          # re: java攝像頭截圖  回復  更多評論   

          2012-07-05 12:42 by guanzhenxing
          學習了!

          # re: java攝像頭截圖  回復  更多評論   

          2013-04-01 15:54 by 千山鳥飛絕
          你好,我試用了你的示例,發(fā)現(xiàn)截圖還不行,保存的圖片是空白,沒有截圖成功。

          我的攝像頭選擇是:vfw:Mircrosoft WDM Image Capture.
          主站蜘蛛池模板: 光泽县| 大兴区| 乐平市| 同心县| 梧州市| 乡宁县| 同德县| 连江县| 华宁县| 丽江市| 四会市| 惠水县| 枣阳市| 潍坊市| 偏关县| 拉萨市| 县级市| 安丘市| 肇源县| 罗城| 湘潭县| 庐江县| 中宁县| 公安县| 屏东市| 漳平市| 富宁县| 融水| 蕉岭县| 宣汉县| 河西区| 公主岭市| 封开县| 轮台县| 清水县| 乌兰浩特市| 类乌齐县| 津南区| 芜湖县| 连平县| 桓台县|