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 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();
          ImageView img = new ImageView(Imgw);
          Group r = new Group(img);
          Scene s = new Scene(r, 600, 400);
          stage.setTitle("Capturing an image");
       public WritableImage capureSnapShot() {
          WritableImage WritableImage = null;
          System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
          VideoCapture c = new VideoCapture(0);
          Mat matrix = new Mat();
          if( c.isOpened()) {
               if ( {
                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[]) {


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:

One response to “How to capture image in java using OpenCV with webcam”

  1. samuel says:

    Caused by: java.lang.UnsatisfiedLinkError: org.opencv.videoio.VideoCapture.VideoCapture_5(I)J
    at org.opencv.videoio.VideoCapture.VideoCapture_5(Native Method)
    at org.opencv.videoio.VideoCapture.(
    at jyothi.update.capureSnapShot(
    at jyothi.update.start(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(
    at Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(
    at Method)
    … 1 more
    Exception running application jyothi.update

Leave a Reply

Your email address will not be published.