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.");
}
}