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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值