Web Cam Capture Program


Hello everyone, in this post I am going to publish a web cam capture program using Java.
1.      First, you have to download OpenCV library using this link. (https://opencv.org/releases/)
2.      Create a new NetBeans project and add OpenCV library.
3.      You can create your GUI now.






4.      This is the code for project

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 java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgcodecs.Imgcodecs;

public class Camera extends javax.swing.JFrame {

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);
Imgcodecs.imencode(".bmp", frame, mem);

Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));

BufferedImage buff = (BufferedImage) im;
Graphics g=jPanel1.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 Camera() {
initComponents();
}
//Generated Code
//Start Web Cam Button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
webSource =new VideoCapture(0);
myThread = new DaemonThread();
Thread t = new Thread(myThread);
t.setDaemon(true);
myThread.runnable = true;
t.start();
jButton1.setEnabled(false);
jButton2.setEnabled(true;
}

//Pause Button
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
myThread.runnable = false;
jButton2.setEnabled(false);
jButton1.setEnabled(true);
webSource.release();

}

// Capture Button
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try
{

String  file ="C:/Users/user/Desktop/mypic.jpg";
Imgcodecs.imwrite(file, frame);
}
catch(Exception ex)
{
System.out.println(ex);
}

}

public static void main(String args[]) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

//Generated Code

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Camera().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JPanel jPanel1;
}

5.      After Capturing the image, it will be saved in the desktop.

Comments

Popular posts from this blog

C# Analog Clock

Basic Prolog (List)

SOLID Principles