Results 1 to 2 of 2
  1. #1
    poliekos is offline Member
    Join Date
    Oct 2016
    Posts
    1
    Rep Power
    0

    Post Take snapshots in netbeans every second?

    Hi!
    I developed program in Netbeans who take a default webcam snapshot with OpenCV library.
    In this code, if you press "snap" button, you select a directory that will receive the file and after take a picture from webcam.
    But now i want to take snaps every second, only stopping when clicking the stop button.





    package opencvwebcam2;

    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import javax.imageio.ImageIO;
    import javax.swing.JFileChooser;
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.MatOfByte;
    import org.opencv.highgui.Highgui;
    import org.opencv.highgui.VideoCapture;


    public class jfmPrincipal extends javax.swing.JFrame {

    VideoCaptura webCam;
    ExibeQuadro exibeQuadro;
    Thread executor;
    private DaemonThread myThread = null;
    int count = 0;
    VideoCapture webSource = null;

    Mat frame = new Mat();
    MatOfByte mem = new MatOfByte();


    class DaemonThread implements Runnable
    {
    protected volatile boolean runnable = false;

    @Override
    public void run()
    {
    synchronized(this)
    {
    while(runnable)
    {
    if(webSource.grab())
    {
    try
    {
    webSource.retrieve(frame);
    Highgui.imencode(".bmp", frame, mem);
    Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));

    BufferedImage buff = (BufferedImage) im;
    Graphics g=jlbCaptura.getGraphics();

    if (g.drawImage(buff, 0, 0, getWidth(), getHeight() -150 , 0, 0, buff.getWidth(), buff.getHeight(), null))

    if(runnable == false)
    {
    System.out.println("Going to wait()");
    this.wait();
    }
    }
    catch(Exception ex)
    {
    System.out.println("Error");
    }
    }
    }
    }
    }
    }
    /////////////////////////////////////////////////////////
    public jfmPrincipal() {

    initComponents();

    }

    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jFileChooser1 = new javax.swing.JFileChooser();
    jbtIniciar = new javax.swing.JButton();
    jbtParar = new javax.swing.JButton();
    jspCaptura = new javax.swing.JScrollPane();
    jlbCaptura = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setTitle("www.dfilitto.com.br (WebCam X OpenCV)");

    jbtIniciar.setText("Iniciar");
    jbtIniciar.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jbtIniciarActionPerformed(evt);
    }
    });

    jbtParar.setText("Parar");
    jbtParar.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jbtPararActionPerformed(evt);
    }
    });

    jspCaptura.setPreferredSize(new java.awt.Dimension(646, 485));

    jlbCaptura.setPreferredSize(new java.awt.Dimension(640, 480));
    jspCaptura.setViewportView(jlbCaptura);

    jButton1.setText("Capturar");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jspCaptura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jbtIniciar)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(jButton1)
    .addGap(4, 4, 4)
    .addComponent(jbtParar)))
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jspCaptura, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jbtIniciar)
    .addComponent(jbtParar)
    .addComponent(jButton1))
    .addContainerGap())
    );

    pack();
    }// </editor-fold>

    private void jbtIniciarActionPerformed(java.awt.event.ActionEve nt evt) {

    webSource =new VideoCapture(0); //captura a partir da camera
    myThread = new DaemonThread();
    Thread t = new Thread(myThread);
    t.setDaemon(true);
    myThread.runnable = true;
    t.start();
    jbtIniciar.setEnabled(false); //start button
    jbtParar.setEnabled(true); // stop button
    }

    private void jbtPararActionPerformed(java.awt.event.ActionEvent evt) {
    myThread.runnable = false;
    jbtParar.setEnabled(false);
    jbtIniciar.setEnabled(true);

    webSource.release();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    int returnVal = jFileChooser1.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = jFileChooser1.getSelectedFile();
    Highgui.imwrite(file.getPath(), frame);
    } else {
    System.out.println("Acesso negado.");
    }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(jfmPrincipal.cl ass.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(jfmPrincipal.cl ass.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(jfmPrincipal.cl ass.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(jfmPrincipal.cl ass.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new jfmPrincipal().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JButton jbtIniciar;
    private javax.swing.JButton jbtParar;
    private javax.swing.JLabel jlbCaptura;
    private javax.swing.JScrollPane jspCaptura;
    // End of variables declaration
    }




    package opencvwebcam2;

    import java.awt.image.BufferedImage;
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.highgui.VideoCapture;

    /**
    *
    * @author Danilo Filitto
    */
    public class VideoCaptura {

    VideoCapture video;
    Mat imageMat = new Mat();
    BufferedImage imageBuffer;
    byte[] dat;

    public VideoCaptura(){
    this.video = new VideoCapture();
    this.video.open(0);
    }

    public BufferedImage capturaQuadroBufferedImage(){
    this.video.read(this.imageMat); //Captura o quadro
    imageBuffer = this.matToBufferedImage(this.imageMat); //Converte para imageBuffer
    return imageBuffer;
    }

    public Mat capturaQuadroMat(){
    this.video.read(this.imageMat); //Captura o quadro
    return imageMat;
    }



    public BufferedImage matToBufferedImage(Mat matrix) {
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int)matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;
    matrix.get(0, 0, data);
    switch (matrix.channels()) {
    case 1:
    type = BufferedImage.TYPE_BYTE_GRAY;
    break;
    case 3:
    type = BufferedImage.TYPE_3BYTE_BGR;
    // bgr to rgb
    byte b;
    for(int i=0; i<data.length; i=i+3) {
    b = data[i];
    data[i] = data[i+2];
    data[i+2] = b;
    }
    break;
    default:
    return null;
    }
    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);
    return image;
    }

    static{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }



    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Take snapshots in netbeans every second?

    Read up on the Swing Timer. It will allow you to make an ActionListener that fires every second.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 16
    Last Post: 07-02-2014, 01:02 PM
  2. some sql and netbeans help
    By derek123 in forum NetBeans
    Replies: 2
    Last Post: 07-20-2010, 09:13 AM
  3. Using SGT API in netbeans
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 10:20 AM
  4. help with netBeans
    By the godfather01 in forum NetBeans
    Replies: 11
    Last Post: 02-01-2009, 04:48 PM
  5. Hi Am New To Use Netbeans
    By arunkumarinfo in forum NetBeans
    Replies: 1
    Last Post: 01-23-2009, 04:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •