How to capture webcam images in Opencv in Java?

Asked

Viewed 1,248 times

1

Hello, I’m a beginner in opencv with java, and my problem is this, I want to capture the real-time image of my webcam and stream it on a Jlabel, but I’m having a problem running the file, netbeans points out the following error:

Exception in thread "AWT-Eventqueue-0" java.lang.Unsatisfiedlinkerror: org.opencv.core.Mat.n_Mat()J

I am using the Videocapture Function from the opencv library, below the code snippet:

private void BotaoStartActionPerformed(java.awt.event.ActionEvent evt){                                           
    Mat imageMat = new Mat(); // matriz de imagem //

    VideoCapture capture = new VideoCapture();
    capture.open(0); // abre dispositivo de video de índice 0 //

    // especifica altura e largura do video //
    capture.set(org.opencv.highgui.Highgui.CV_CAP_PROP_FRAME_HEIGHT, 320);
    capture.set(org.opencv.highgui.Highgui.CV_CAP_PROP_FRAME_WIDTH, 640);

    BufferedImage image;

    capture.read(imageMat); //Captura o quadro
    image = this.matToBufferedImage(imageMat); //Converte para imageBuffer

    while(capture.isOpened()){
        Icon icon = new ImageIcon(image);
        this.TelaImagem.setIcon(icon);
        this.TelaImagem.repaint();

        try {
            Thread.sleep(150);
        } catch (InterruptedException ex) {

        }
    }

}                         

this is the function that converts the type Mat to Bufferedimage:

public BufferedImage matToBufferedImage(Mat matrix) {  
 int cols = matrix.cols();  
 int rows = matrix.rows();  
 int elemSize = (int)matrix.elemSize();  
 byte[] data = new byte[cols * rows * elemSize];  
 int type;  
 matrix.get(0, 0, data);  
 switch (matrix.channels()) {  
   case 1:  
     type = BufferedImage.TYPE_BYTE_GRAY;  
     break;  
   case 3:  
     type = BufferedImage.TYPE_3BYTE_BGR;  
     // bgr to rgb  
     byte b;  
     for(int i=0; i<data.length; i=i+3) {  
       b = data[i];  
       data[i] = data[i+2];  
       data[i+2] = b;  
     }  
     break;  
   default:  
     return null;  
 }  
 BufferedImage image = new BufferedImage(cols, rows, type);  
 image.getRaster().setDataElements(0, 0, cols, rows, data);  
 return image;  

}

the error points to the statement line of Mat imageMat = new Mat(); would anyone know what is causing this error? I thank you already.

  • After a few hours I discovered the error message problem, I failed to put this line in the main class: System.loadLibrary(Core.NATIVE_LIBRARY_NAME); when executing, and when the button starts the image is not generated in jLabel and the window hangs. Would anyone know how to make it work? Thanks in advance.

  • Yuri, do not ask new questions in the comments. There is little chance of anyone helping you. Since you’ve managed to resolve this on your own, delete this question (or answer it yourself) and open a new question with your new question, ok?

  • The same mistake happened to me. The only thing I did was take the webcam USB extender cable and connect the webcam without the extender. Then I ran jFrame again and it worked. I hope I helped.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.