How to capture image in java using OpenCV with webcam

Hello everyone! learn how to capture an image with a webcam using OpenCV in java. For this, you need to follow certain steps.

  1. Open your source code.
  2.  Right-click on your java file.
  3.  Select the build path option.
  4.  Click on the configure build path.
  5. Select libraries.
  6.  Click on add external library jars.
  7.  Select the open folder.
  8.  build a native library path.

Learn how to capture an image with a webcam using OpenCV

package jyothi;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
public class update  extends Application {

    // TODO Auto-generated method stub
       Mat matrix = null;
       public void start(Stage stage) throws FileNotFoundException, IOException {
          update o = new update();
          WritableImage Imgw = o.capureSnapShot();
          o.saveImage();
          ImageView img = new ImageView(Imgw);
          img.setFitHeight(400);
          img.setFitWidth(600);
          img.setPreserveRatio(true);
          Group r = new Group(img);
          Scene s = new Scene(r, 600, 400);
          stage.setTitle("Capturing an image");
          stage.setScene(s);
          stage.show();
       }
       public WritableImage capureSnapShot() {
          WritableImage WritableImage = null;
          System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
          VideoCapture c = new VideoCapture(0);
          Mat matrix = new Mat();
          c.read(matrix);
          if( c.isOpened()) {
               if (c.read(matrix)) {
                BufferedImage image = new BufferedImage(matrix.width(), 
                   matrix.height(), BufferedImage.TYPE_3BYTE_BGR);
                
                WritableRaster raster = image.getRaster();
                DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
                byte[] d = dataBuffer.getData();
                matrix.get(0, 0, d);
                this.matrix = matrix;
                WritableImage = SwingFXUtils.toFXImage(image, null);
             }
          }
          return WritableImage;
       }
       public void saveImage() {
          String f = "c:/opencv/i.jpg";
              Imgcodecs imageCodecs = new Imgcodecs();
              imageCodecs.imwrite(f, matrix);
       }
       public static void main(String args[]) {
          launch(args);
        }
}

Output:

capture image in java using OpenCV with webcam

Import all the required packages required. Initialize the VideoCapture and we can scan pictures from the webcam by using a method called read which is a predefined method of videoCapture class. And we can pass the Mat object as a parameter in the read method. First, the method isOpened() tests whether the webcam is opened or not and then the method isread() function returns whether the image captured read or not. If read then stored as a image in bufferedImage class. Then the image converted into a writable image using Writable class. Save the captured image in a file if you want to. I hope the code is clear without any errors and is understandable.

This is how we can capture image in java using OpenCV with webcam.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *