Taking a Snapshot from System Camera using OpenCV in Java Last Updated : 03 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The OpenCV library in Java contains a class named 'VideoCapture' which provides a method called read (which is predefined) to scan pictures from the webcam. Mat object is passed as a parameter in the read method. Concept: The 'javax.swing' PackageThe Abstract Window Toolkit (AWT) Let's discuss them briefly before diving into the procedure and implementation part. The 'javax.swing' package is used as this package provides classes for Java Swing API such as JButton, JTextArea, JCheckbox, JMenu, etc. Concepts of java involved here are Swing class and abstract windows toolkit as mentioned below:javax.swing.ImageIcon : The class ImageIcon is an implementation of the Icon interface that paints Icons from Images.javax.swing.Jframe : The class JFrame is a type of container which inherits the java. JFrame works like the main window where components like labels, buttons, text fields are added to create a GUI.javax.swing.JLabel : The class JLabel is used to display a short string or an image icon.xThe Abstract Window Toolkit (AWT) is a Java package used for creating graphical user interfaces. AWT features include:A set of native interface components.A robust event-handling model.Graphics and imaging tools, including shape, color, and font classes.Layout managers, for flexible window layouts that do not depend on a particular window size or screen resolution.Data transfer classes, for cut and paste through the native platform clipboard. Procedure: Steps to draw geometric shapes on images in OpenCV Create a project and add an OpenCV library.Create PackageCreate a ClassCreate a folder named “images” to save the captured images.Writing the desired java program to a java file. Implementation: Writing the following java program in the java file to take a snapshot from the system camera. Example Java // Java Program to take a Snapshot from System Camera // using OpenCV // Importing openCV modules package com.opencvcamera; // importing swing and awt classes import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // Importing date class of sql package import java.sql.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; // Importing VideoCapture class // This class is responsible for taking screenshot import org.opencv.videoio.VideoCapture; // Class - Swing Class public class Camera extends JFrame { // Camera screen private JLabel cameraScreen; // Button for image capture private JButton btnCapture; // Start camera private VideoCapture capture; // Store image as 2D matrix private Mat image; private boolean clicked = false; public Camera() { // Designing UI setLayout(null); cameraScreen = new JLabel(); cameraScreen.setBounds(0, 0, 640, 480); add(cameraScreen); btnCapture = new JButton("capture"); btnCapture.setBounds(300, 480, 80, 40); add(btnCapture); btnCapture.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clicked = true; } }); setSize(new Dimension(640, 560)); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } // Creating a camera public void startCamera() { capture = new VideoCapture(0); image = new Mat(); byte[] imageData; ImageIcon icon; while (true) { // read image to matrix capture.read(image); // convert matrix to byte final MatOfByte buf = new MatOfByte(); Imgcodecs.imencode(".jpg", image, buf); imageData = buf.toArray(); // Add to JLabel icon = new ImageIcon(imageData); cameraScreen.setIcon(icon); // Capture and save to file if (clicked) { // prompt for enter image name String name = JOptionPane.showInputDialog( this, "Enter image name"); if (name == null) { name = new SimpleDateFormat( "yyyy-mm-dd-hh-mm-ss") .format(new Date( HEIGHT, WIDTH, getX())); } // Write to file Imgcodecs.imwrite("images/" + name + ".jpg", image); clicked = false; } } } // Main driver method public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); EventQueue.invokeLater(new Runnable() { // Overriding existing run() method @Override public void run() { final Camera camera = new Camera(); // Start camera in thread new Thread(new Runnable() { @Override public void run() { camera.startCamera(); } }).start(); } }); } } Output: After a successful compilation of the program, the execution is as follows as the webcam will open up where you click on the “Capture” button and the rest image will be named. Now, click on the “OK” button to save the image. The output image will be saved in the folder which was created earlier. Comment More infoAdvertise with us Next Article Rotating Images using OpenCV in Java P pawki Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 OpenCV Java-OpenCV +2 More Practice Tags : Java Similar Reads Java Program to Take a Snapshot From System Camera It is better to have a brief knowledge of the Swing class of java as it is applied in the implementation to do so as whenever there arises a need to deal with images or dealing with the two-dimensional matrix as an output or usage of it in generating the output in java, Swing class comes into play i 9 min read Rotating Images using OpenCV in Java Image rotation is a common image processing routine used to rotate images at any desired angle. This helps in image reversal, flipping, and obtaining an intended view of the image. Image rotation has applications in matching, alignment, and other image-based algorithms. OpenCV is a well-known librar 6 min read Java Program to Add Text to an image in OpenCV OpenCV is the cross-platform open-source library for computer vision, image processing, and machine learning. Â It plays a major role nowadays in real-time operations in improving modules provides adequate functions for image manipulation. It has C++, C, Python, and Java interfaces and supports Windo 3 min read Java Program to Copy and Paste an image in OpenCV OpenCV is a Machine Learning and open-source computer vision software library, the main purpose for which it was developed is to enable a common medium to increase the use of machine perception in commercial businesses and accelerate the development of computer vision applications, it is a smooth an 3 min read Java OpenCV Programs - Basic to Advanced Java is a popular programming language that can be used to create various types of applications, such as desktop, web, enterprise, and mobile. Java is also an object-oriented language, which means that it organizes data and behaviour into reusable units called classes and objects. Java is known for 2 min read Java Program to Crop Image Using BufferedImage Class In the Java programming language, we need some classes to crop an image. So these classes are as follows: 1. To read and write an image file we have to import the File class. This class represents file and directory path names in general. import java.io.File 2. To handle errors we use the IOExceptio 2 min read Like