1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
......
public class Test extends Panel implements ActionListener{
public static Player player=null;
public CaptureDeviceInfo di=null;
public MediaLocator ml=null;
public JButton capture=null;
public Buffer buf=null;
public Image img=null;
public VideoFormat vf=null;
public BufferToImage btoi=null;
//public ImagePanel imgpanel=null;
public int capturenumber=0;
public Test() throws IOException{
setLayout(new BorderLayout());
setSize(320,550);
// imgpanel=new ImagePanel();
capture= new JButton("Capture"); // Création d'une bouton
capture.addActionListener(this); // associér les evenement à cet
String str = "vfw:Microsoft WDM Image Capture (Win32):0";
di=CaptureDeviceManager.getDevice(str);
ml = new MediaLocator(str);
// Player player = null;
try {
player=Manager.createRealizedPlayer(ml);
} catch (NoPlayerException ex) {
Logger.getLogger(Test_capture.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(Test_capture.class.getName()).log(Level.SEVERE, null, ex);
}
player.start();
Component comp;
try{
if((comp = player.getVisualComponent())!=null){
add(comp,BorderLayout.NORTH);
add(capture,BorderLayout.CENTER);}
// add(imgpanel,BorderLayout.SOUTH);
}
catch(Exception ex){
ex.printStackTrace();
}
}
public static void playerclose(){
player.close();
player.deallocate();
}
public void saveJPG(String image_name, Image img) throws IOException {
{
/*BuffredImage représente les données de l'image dans la mémoire
Type_INT_RGB veut dire que les composants coloré avec des couleur 8 bits*/
BufferedImage bi= new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2=bi.createGraphics();
g2.drawImage(img, null, null); // drawImage : permet de dessiner dans un endroit précis (coordonnées)
FileOutputStream out=null;
String file_format = image_name.substring(image_name.lastIndexOf('.')+1);
try {
// Date d = new Date();
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy_MM_dd_hh_mm_ss");
boolean success = ImageIO.write(bi, file_format, new File("C:\\Users\\user\\Downloads\\pfe_tof\\PIC_"+ft.format(dNow)+".jpg"));
if (!success) {
JOptionPane.showMessageDialog(this, "Ecriture impossible:"+file_format);
}
}
catch (IOException | HeadlessException e) {
e.printStackTrace();
}// end catch
}//end save
}
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
if(c==capture){
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi= new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
//imgpanel.setImage(img);
Calendar cal= Calendar.getInstance();
@SuppressWarnings("static-access")
int data = (cal.getTime().getYear()+1900)*10000+ (cal.getTime().getMonth()+1)*100+cal.getTime().getDate();
try {
saveJPG("image1.jpg",img);
} catch (IOException ex) {
Logger.getLogger(Test_capture.class.getName()).log(Level.SEVERE, null, ex);
}
capturenumber++;
}
}
public static void main(String[] args) throws IOException{
Frame f=new Frame("Take picture");
Test cf = new Test();
f.add(cf);
f.pack();
f.setSize(320, 550);
f.setVisible(true);
} |
Partager