java来调用电脑视频摄像头拍照进行截图_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java来调用电脑视频摄像头拍照进行截图

java来调用电脑视频摄像头拍照进行截图

 2013/9/9 19:06:55  rain_2372  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.pengo.capture;importjava.awt.BorderLayout;importjava.awt.Dimension;importjava.awt.Graphics2D;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io
  • 标签:Java 摄像头
packagecom.pengo.capture;
  
importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.Graphics2D;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.media.MediaLocator;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importnet.sf.fmj.ui.application.CaptureDeviceBrowser;
importnet.sf.fmj.ui.application.ContainerPlayer;
importnet.sf.fmj.ui.application.PlayerPanelPrefs;
  
publicclassCameraFrameextendsJFrame{
    privatestaticintnum =0;
    publicCameraFrame()throwsException{
        this.setTitle("摄像头截图应用");
        this.setSize(480,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        finalJPanel cameraPanel =newJPanel();
        this.getContentPane().setLayout(newBorderLayout());
        this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
        ContainerPlayer containerPlayer =newContainerPlayer(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 =newPlayerPanelPrefs();
        containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
          
        JPanel btnPanel =newJPanel(newBorderLayout());
        finalJTextField path =newJTextField("E:\\camera");
        path.setColumns(30);
        btnPanel.add(path, BorderLayout.WEST);
        JButton okBtn =newJButton("截图");
        okBtn.addActionListener(newActionListener(){
             publicvoidactionPerformed(ActionEvent e){
                 Dimension imageSize = cameraPanel.getSize();
                    BufferedImage image =newBufferedImage(imageSize.width,
                            imageSize.height, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g = image.createGraphics();
                    cameraPanel.paint(g);
                    g.dispose();
                    try{
                 
                        String filePath = path.getText();
                        File file =newFile(filePath);
                        if(file.exists() ==false){
                            file.mkdirs();
                        }
                        ImageIO.write(image,"png",newFile(file.getAbsolutePath() +"/"+ num +".png"));
                        num++;
                    }catch(IOException ex) {
                        ex.printStackTrace();
                         
                    }
             }
        });
        btnPanel.add(okBtn, BorderLayout.EAST);
        this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
    }
      
    publicstaticvoidmain(String[] args)throwsException{
        CameraFrame camera =newCameraFrame();
        camera.setVisible(true);
    }
}
转:
http://www.ablanxue.com/prone_3338_1.html
发表评论
用户名: 匿名