Capture webcam image and save every 1 second?

1

I'm doing a project where the default webcam takes photos every 1 second using opencv. In my code, to the click of the capture button you select the folder where you want and it captures a photo. I need to loop him so he can capture photos and save.

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");
                         }
                }
            }
        }
     }
   }
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.");
    }
    }
    
asked by anonymous 05.10.2016 / 13:51

1 answer

1

I implemented a simple class called TimedWebcam using webcam-capture from sarxos:

TimedWebcam.java:

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.Webcam;
import java.util.concurrent.TimeUnit;

public class TimedWebcam {

    private final Webcam webcam = Webcam.getDefault();

    public TimedWebcam() {
        this.webcam.setViewSize(new Dimension(640, 480));
    }

    public void take(int times, int internalInSeconds, String folder) {
        this.webcam.open();
        for (Integer i = 0; i < times; i++) {
            try {
                System.out.println("Saving image " + i.toString() + '.');
                TimeUnit.SECONDS.sleep(internalInSeconds);
                ImageIO.write(webcam.getImage(), "PNG", new File(folder + "\image" + i.toString() + ".png"));
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
        }
        this.webcam.close();
    }

}

Main.java:

public class Main {

    public static void main(String[] args) {
        TimedWebcam t = new TimedWebcam();
        t.take(5, 1, "."); // Salva 5 imagens na pasta do projeto a cada 1 segundo.
    }

}

As you can see, the class has a method called "take" that takes 3 arguments: one for the number of takes, another for the interval in seconds between each, and a destination folder where they are saved in .png. I use the TimeUnit class to cause the delay. I have no experience with opencv, but I guarantee that webcam-capture is one of the best libs to work with the webcam available for Java. The lib will come in a file named " webcam-capture-0.3.10-dist " (for example), and you will need to add in the build-path of your project the " webcam- 0.3.10.jar ", as well as the libs it uses:" bridj-0.6.2.jar "and" slf4j-api-1.7.2.jar ".

I did not use Swing / wrapped graphical interface because it is not necessary for the example, but it is quite simple to do so by checking the examples in github.

    
05.10.2016 / 17:13