ITeye博客

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

rain_2372 · 2013-09-09 15:34 · java · 编程语言 · Java · Markdown

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

评论(1)

  1. #1 楼 · bobo22 · 2016-06-23 16:25

    importnet.sf.fmj.ui.application.CaptureDeviceBrowser; importnet.sf.fmj.ui.application.ContainerPlayer; importnet.sf.fmj.ui.application.PlayerPanelPrefs;

    去哪里弄这几个jar包

评论功能维护中,当前仅展示历史评论。