Capture webcam image and save every 1 second?

Asked

Viewed 770 times

1

I’m doing a project where the standard webcam takes photos every 1 second using opencv.
In my code, at the click of the capture button you select the folder where you want and it captures a photo. I need to make a loop for it to 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.");
    }
    }
  • The question is how to run a thread each x seconds?

  • That’s right, besides how to apply this code.

1 answer

1

I implemented a simple class called TimedWebcam using the webcam-capture of sarxes:

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 will be saved in .png. I use the class Timeunit to cause 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 name "webcam-capture-0.3.10-dist" (For example), and you will need to add in your project’s build-path the "webcam-capture-0.3.10.jar", as well as the libs he uses: "bridj-0.6.2.jar" and "slf4j-api-1.7.2.jar".

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

  • The Timedwebcam method in the main class can be allocated in a jButton?

  • @Jonathangaldino, Timedwebcam is not a method, it is a separate class in its own file. java* (*It has a method of the same name, the "constructor" of the class, but I don’t believe it referred to it). Timedwebcam has the "take" method, as I explained, and this can easily be called / run inside an Actionlistener of a Jbutton. In short, it’s very simple to do what you want (Take the photos by clicking a button).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.