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;
public
classCameraFrameextendsJFrame{
privatestaticintnum =0;
publicCameraFrame()throwsException{
this.setTitle("
摄像头截图应用");
this.setSize(480,500);
this.set
DefaultClose
Operation(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