import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Panel; import java.awt.Rectangle; import java.awt.Robot; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.util.logging.Level; import java.util.logging.Logger; import javax.media.Buffer; import javax.media.CaptureDeviceInfo; import javax.media.CaptureDeviceManager; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.Player; import javax.media.control.FrameGrabbingControl; import javax.media.format.VideoFormat; import javax.media.util.BufferToImage; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import java.io.*; import java.sql.SQLException; import java.util.Random; import javax.swing.JPanel; public class Main extends JPanel implements Runnable { private static final long serialVersionUID = 1L; public static Player player = null; public CaptureDeviceInfo di = null; public MediaLocator ml = null; public static Buffer buf = null; public static Image img = null; public VideoFormat vf = null; public static BufferToImage btoi = null; private Thread thread; public Main(int width, int height) { this.setSize(width, height); String str1 = "vfw:Logitech USB Video Camera:0"; String str2 = "vfw:Microsoft WDM Image Capture (Win32):0"; di = CaptureDeviceManager.getDevice(str2); ml = new MediaLocator("vfw://0"); try { player = Manager.createRealizedPlayer(ml); player.start(); Component comp; if ((comp = player.getVisualComponent()) != null) { add(comp,BorderLayout.NORTH); } } catch (Exception e) { e.printStackTrace(); } thread= new Thread(this); } public void startEngine() { thread.start(); } public void run() { startCapture(); } public void startCapture() { String fileName = "C:\\Documents and Settings\\DHIB AKRAM\\Bureau\\akram.txt"; //String fileName = "akram.txt"; String line; StringBuffer sb = new StringBuffer(); int nbLinesRead = 0; try { //InputStream ips=this.getClass().getResourceAsStream(fileName); //InputStreamReader ipsr=new InputStreamReader(ips); //BufferedReader reader = new BufferedReader(ipsr); FileInputStream fis = new FileInputStream(fileName); BufferedReader reader = new BufferedReader(new InputStreamReader( fis)); while ((line = reader.readLine()) != null) { nbLinesRead++; line = line.toLowerCase(); if (nbLinesRead == 1) { FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); buf = fgc.grabFrame(); // Convert it to an image //btoi = new BufferToImage((VideoFormat)buf.getFormat()); //img = btoi.createImage(buf); Thread.currentThread(); try { Thread.sleep(10000); //sleep for 5000 Ms } catch (InterruptedException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } Robot robot = null; try { robot = new Robot(); } catch (AWTException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } BufferedImage image = robot.createScreenCapture( new Rectangle(java.awt.Toolkit.getDefaultToolkit().getScreenSize()) ); BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(image, null, null); FileOutputStream out = null; Random r = new Random(); int nombre = (r.nextInt(70)+80); String N = "ph"+nombre; AccesBd Abd = new AccesBd(); try { Abd.MAJ_pt(N, line); } catch (SQLException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } String s = "C:\\wamp/www/projet_pwr_akram/espace administrateur/image/"+N+".jpg"; try { out = new FileOutputStream(s); } catch (java.io.FileNotFoundException io) { System.out.println("File Not Found"); } JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.setQuality(0.5f,false); encoder.setJPEGEncodeParam(param); try { encoder.encode(bi); out.close(); } catch (java.io.IOException io) { System.out.println("IOException"); } } line = line.replaceFirst(line,""); } reader.close(); BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); out.write(sb.toString()); out.close(); } catch (IOException exception) { exception.printStackTrace(); } } }