java摄像头截图

  • 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);  
  •     }  
  • }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值