Taking a Snapshot from System Camera using OpenCV in Java
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.
- The ‘javax.swing’ Package
- The 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.x
- The 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 Package
- Create a Class
- Create 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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.