Show video from camera with OpenCV and Java

Before you start make sure you have

For IDE I suggest you use Intellij IDEA, because it’s fast, really smart and will solve most of your trivial problems

Create a class called CameraVideo:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;

import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

public class CameraVideo {

    static {
        // load the OpenCV library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
        // initialize and assign the basic image container
        Mat mat = new Mat();
        // initialize and assign which camera to use, on most laptops index - 0 is the main laptop camera
        VideoCapture camera = new VideoCapture(0);
        // get the resolution of the the camera
        final Size frameSize = new Size(camera.get(Videoio.CAP_PROP_FRAME_WIDTH), camera.get(Videoio.CAP_PROP_FRAME_HEIGHT));

        JFrame frame = new JFrame();
        // Make X close the window
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel panel = new JLabel();
        frame.setContentPane(panel);
        frame.setSize((int) frameSize.width, (int) frameSize.height);
        frame.setVisible(true);

        // when the program stops (Pressed X button, closed from the IDE and etc) makes sure that the image container and the video capture are released
        Runtime.getRuntime().addShutdownHook(new ShutdownThread(mat, camera));

        // while the camera is opened and is "reading" video
        while (camera.isOpened() && camera.read(mat)) {
            // converts the basic image container to image
            ImageIcon image = new ImageIcon(mat2BufferedImage(mat));
            // sets the image
            panel.setIcon(image);
            // refreshes the panel in the frame
            panel.repaint();
        }
    }

    private static BufferedImage mat2BufferedImage(Mat m) {
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (m.channels() > 1) {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = m.channels() * m.cols() * m.rows();
        byte[] b = new byte[bufferSize];
        m.get(0, 0, b); // get all the pixels
        BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        return img;
    }

    private static class ShutdownThread extends Thread {

        private Mat mat;
        private VideoCapture videoCapture;

        public ShutdownThread(Mat mat, VideoCapture videoCapture) {
            this.mat = mat;
            this.videoCapture = videoCapture;
        }

        @Override
        public void run() {
            super.run();
            mat.release();
            videoCapture.release();
        }
    }
}

 

If you have any questions or suggestions, feel free to leave a comment.
Become a Patron!