/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testdepouillement.ttmt; import au.edu.jcu.v4l4j.FrameGrabber; import au.edu.jcu.v4l4j.ImageFormat; import au.edu.jcu.v4l4j.ImageFormatList; import au.edu.jcu.v4l4j.V4L4JConstants; import au.edu.jcu.v4l4j.VideoDevice; import au.edu.jcu.v4l4j.exceptions.V4L4JException; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author florent */ public class Webcam extends Thread { private boolean actif; private String device; private Number fps; private Integer height; private Integer width; private String work_path; private String lbl_recto_verso; private VideoDevice vd; private int standard; private int channel; private int input; private int num_frame; private FrameGrabber fg; private ByteBuffer bb; private ImageFormat imf; public Webcam(String device, Number fps, Integer height, Integer width, String work_path, String lbl_recto_verso) throws Exception { if (device.isEmpty() || fps == null || height == null) { Exception e = new Exception("Mauvaise initialisation des paramètres"); throw e; } this.lbl_recto_verso = lbl_recto_verso; if (this.lbl_recto_verso.equals("Recto")) { this.work_path = work_path + "/recto"; } else if (this.lbl_recto_verso.equals("Verso")) { this.work_path = work_path + "/verso"; } else { System.out.println("Recto / Verso mal personnalisé"); throw new Exception("Recto / Verso mal personnalisé"); } File work_folder = new File(this.work_path); if (!work_folder.exists()) { work_folder.mkdirs(); } //Pour le stockage this.bb = null; //Pour l'acquisition this.height = height; this.width = width; this.standard = V4L4JConstants.STANDARD_PAL; this.channel = V4L4JConstants.STANDARD_WEBCAM; this.input = 0; this.device = device; this.fps = fps; //width = 640, height = 480, input = 0, , ; try { if (vd == null) { vd = new VideoDevice(device); } else { vd.release(); vd = new VideoDevice(device); } } catch (V4L4JException e) { System.out.println(e.getMessage()); } if (!vd.supportYUVConversion() && !vd.supportJPEGConversion()) { // use the other supportXXXConversion() for other formats vd.release(); try { throw new Exception("The video device does support neither JPEG nor YUV encoding"); } catch (Exception ex) { Logger.getLogger(Webcam.class.getName()).log(Level.SEVERE, null, ex); } } else if (vd.supportJPEGConversion()) { imf = vd.getDeviceInfo().getFormatList().getJPEGEncodableFormats().get(0); fg = vd.getJPEGFrameGrabber(width, height, input, channel, 100, imf); } else if (vd.supportYUVConversion()) { imf = vd.getDeviceInfo().getFormatList().getYUVEncodableFormats().get(0); fg = vd.getYUVFrameGrabber(width, height, input, channel, imf); } } public void stop_webcam() { this.actif = false; this.vd.releaseFrameGrabber(); this.vd.release(); } public void start_Webcam() { this.actif = true; this.start(); } synchronized void capture_frame() throws V4L4JException { this.fg.startCapture(); this.bb = fg.getFrame(); this.fg.stopCapture(); } void save_frame() { try { byte[] b = new byte[bb.limit()]; this.bb.get(b); ByteArrayOutputStream baos = new ByteArrayOutputStream(b.length); baos.write(b, 0, b.length); File dest_File = new File(work_path, lbl_recto_verso + "_" + num_frame + ".jpg"); if (!dest_File.exists()) { dest_File.createNewFile(); } else { dest_File.delete(); dest_File.createNewFile(); } java.io.OutputStream destinationStream = new java.io.FileOutputStream(dest_File); baos.writeTo(destinationStream); bb = null; baos.close(); num_frame = num_frame + 1; //write_message_panel("Fichier " + work_path+"/"+lbl_recto_verso+"_"+num_frame+".jpg" + // " Sauvegardé!"); } catch (IOException ex) { System.out.println(); Logger.getLogger(Webcam.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void run() { while (this.actif) { try { capture_frame(); save_frame(); this.yield(); //actif = Thread.isAlive(); // } catch (InterruptedException ex) { // Logger.getLogger(Webcam.class.getName()).log(Level.SEVERE, null, ex); } catch (V4L4JException ex) { Logger.getLogger(Webcam.class.getName()).log(Level.SEVERE, null, ex); } } this.actif = false; } }