// Java Program to Take a Snapshot From System Camera
package myfirstform;
// The goal of this import com.github.sarxos.webcam.Webcam
// is to allow integrated or USB-connected webcams
// to be accessed directly from java
// Using provided libraries users are able to
// read camera images and detect motion
import com.github.sarxos.webcam.Webcam;
// Provides classes for creating and modifying images*/
import java.awt.Image;
// Creates an ImageIcon from an array of bytes
// which were read from an image file containing
// a supported image format, such as GIF, JPG, PNG
import javax.swing.ImageIcon;
// Class
// Main class
public class MyFrame
extends javax.swing.JFrame implements Runnable {
// Creates new form MyFrame
public MyFrame()
{
// Initialising the components
initComponents();
// This is for the closing button
lblclose.setText("");
// Creating an image icon by adding path of image
ImageIcon img = new ImageIcon(
"C:\\Users\\dhannu\\Documents\\NetBeansProjects\\MyFirstForm\\src\\images\\sf.png");
// Creating an object of Image type
Image myimg = img.getImage();
// Creating a new image whose size is same as label
// size using algorithm - SCALE_SMOOTH
Image newimage = myimg.getScaledInstance(
lblclose.getWidth(), lblclose.getHeight(),
Image.SCALE_SMOOTH);
// Creating a image icon from new image
ImageIcon ic = new ImageIcon(newimage);
// Assigning the imageicon to the label
lblclose.setIcon(ic);
// Thread is created and started using
// start() method which begins thread execution
new Thread(this).start();
}
// generated code
// 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")
private void initComponents()
{
jPanel1 = new javax.swing.JPanel();
lblclose = new javax.swing.JLabel();
lblphoto = new javax.swing.JLabel();
btnclick = new javax.swing.JButton();
setDefaultCloseOperation(
javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
// Setting the background by providing
// Custom input bounds as parameters
jPanel1.setBackground(
new java.awt.Color(204, 204, 255));
lblclose.addMouseListener(
new java.awt.event.MouseAdapter() {
public void mouseClicked(
java.awt.event.MouseEvent evt)
{
lblcloseMouseClicked(evt);
}
});
lblphoto.setBorder(
javax.swing.BorderFactory.createLineBorder(
new java.awt.Color(0, 0, 0), 4));
// Click button
btnclick.setText("CLICK");
btnclick.addActionListener(
new java.awt.event.ActionListener() {
public void actionPerformed(
java.awt.event.ActionEvent evt)
{
btnclickActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout
= new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(
jPanel1Layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(
lblphoto,
javax.swing.GroupLayout
.PREFERRED_SIZE,
143,
javax.swing.GroupLayout
.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle
.ComponentPlacement.RELATED,
34, Short.MAX_VALUE)
.addComponent(
lblclose,
javax.swing.GroupLayout
.PREFERRED_SIZE,
28,
javax.swing.GroupLayout
.PREFERRED_SIZE)
.addContainerGap())
.addGroup(
jPanel1Layout.createSequentialGroup()
.addGap(97, 97, 97)
.addComponent(btnclick)
.addContainerGap(
javax.swing.GroupLayout
.DEFAULT_SIZE,
Short.MAX_VALUE)));
jPanel1Layout.setVerticalGroup(
jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(
jPanel1Layout.createSequentialGroup()
.addGroup(
jPanel1Layout
.createParallelGroup(
javax.swing.GroupLayout
.Alignment.LEADING)
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addContainerGap()
.addComponent(
lblclose,
javax.swing
.GroupLayout
.PREFERRED_SIZE,
28,
javax.swing
.GroupLayout
.PREFERRED_SIZE))
.addGroup(
jPanel1Layout
.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(
lblphoto,
javax.swing
.GroupLayout
.PREFERRED_SIZE,
143,
javax.swing
.GroupLayout
.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(btnclick)
.addContainerGap(29,
Short.MAX_VALUE)));
javax.swing.GroupLayout layout
= new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout
.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addComponent(
jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout
.PREFERRED_SIZE));
layout.setVerticalGroup(
layout
.createParallelGroup(javax.swing.GroupLayout
.Alignment.LEADING)
.addComponent(
jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout
.PREFERRED_SIZE));
pack();
setLocationRelativeTo(null);
}
// End of generate code
private void
lblcloseMouseClicked(java.awt.event.MouseEvent evt)
{
// Setting flag as false to stop the thread
// so that you can capture the snapshot
flag = false;
// Destroying and cleaning the JFrame window
// by the operating system\
// using dispose() method
dispose();
}
private void
btnclickActionPerformed(java.awt.event.ActionEvent evt)
{
flag = false;
}
// Main driver method
public static void main(String args[])
{
// Set the Nimbus look and feel
// If Nimbus(Java 6+) is not available
// stay with the default look and feel.
// Try block to check if any exceptions occur
try {
for (javax.swing.UIManager
.LookAndFeelInfo info :
javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(
info.getClassName());
break;
}
}
}
// Catch blocks to handle exceptions
// First catch block to handle exception
// if class is not found
catch (ClassNotFoundException ex) {
java.util.logging.Logger
.getLogger(MyFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null,
ex);
}
// Second catch block to handle for exception
// InstantiationException
// In generic, this exception is thrown
// rarely
catch (InstantiationException ex) {
java.util.logging.Logger
.getLogger(MyFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null,
ex);
}
// 3rd catch block to handle
// IllegalAccessException
catch (IllegalAccessException ex) {
java.util.logging.Logger
.getLogger(MyFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null,
ex);
}
// 4th catch block to handle Swing class
// UnsupportedLookAndFeelException
catch (javax.swing
.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger
.getLogger(MyFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null,
ex);
}
//</editor-fold>
// Create and display the form
java.awt.EventQueue.invokeLater(new Runnable() {
// Method run() which will later on
// over-ridden
public void run()
{
new MyFrame().setVisible(true);
}
});
}
// End of generated code
// Declaring variables
private javax.swing.JButton btnclick;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel lblclose;
private javax.swing.JLabel lblphoto;
Webcam wc;
// Initially setting flag as true
boolean flag = true;
// Overriding the run() method as
// created above already
// @Override
public void run()
{
// Creating a webcam object
wc = Webcam.getDefault();
// Method to open the camera
wc.open();
// Checking condition over flag which
// holds true for boolean true as
// above flag is declared true
while (flag) {
// An image is clicked
Image img = wc.getImage();
// Create a image whose size is same as label
img = img.getScaledInstance(
lblphoto.getWidth(), lblphoto.getHeight(),
Image.SCALE_SMOOTH);
// The clicked image is assigned to a Label
lblphoto.setIcon(new ImageIcon(img));
// Try block to check for thread exception
try {
// Putting the thread to sleap
Thread.sleep(20);
}
// Catch block in there is some
// mishappening while the thread is
// put to sleep
catch (InterruptedException e) {
}
}
}
}