Webcam video for Linux workaround

I share below a workaround for whoever got issues on linux with the video library.
I was trying to get the video feed from the webcam but only got errors related to libsoup and gstreamer.

the workaround that i found uses javacv.jar

  1. downolad the file javacv-platform-1.5.11-bin.zip from
    https://github.com/bytedeco/javacv/releases

  2. in the downloaded archive find the file javacv.jar and add it to your processing sketch (in the processing IDE click Sketch → Add file… → select javacv.jar).

  3. the following code worked for me, hope it can be useful to others.

import org.bytedeco.javacv.*;
import org.bytedeco.opencv.opencv_core.*;

Mat frameMat;
OpenCVFrameGrabber grabber;
OpenCVFrameConverter.ToMat converter;
PImage img;

void setup() {
  size(640, 480); // Set the size of the window
  grabber = new OpenCVFrameGrabber(0); // Initialize the frame grabber (0 is for the default webcam)
  
  try {
    grabber.start(); // Start the webcam
  } catch (FrameGrabber.Exception e) {
    e.printStackTrace();
  }
  
  converter = new OpenCVFrameConverter.ToMat(); // Create a frame converter from JavaCV to OpenCV Mat
}

void draw() {
  background(0); // Clear the screen
  
  // Capture a frame from the webcam
  try {
    Frame frame = grabber.grab(); // Get the current frame
    frameMat = converter.convert(frame); // Convert the frame to OpenCV Mat
  } catch (FrameGrabber.Exception e) {
    e.printStackTrace();
  }
  
  if (frameMat != null) {
    // Convert the Mat to PImage for Processing
    img = matToPImage(frameMat);
    
    // Display the image
    image(img, 0, 0);
  }
}

PImage matToPImage(Mat mat) {
  // Get the width and height of the Mat
  int width = mat.cols();
  int height = mat.rows();
  
  // Create a new PImage to store the image
  PImage pImage = new PImage(width, height);
  
  // Convert Mat to byte array and load the pixels into the PImage
  byte[] data = new byte[width * height * 3];
  mat.data().get(data);
  
  int idx = 0;
  for (int i = 0; i < width * height; i++) {
    byte b = data[idx++];
    byte g = data[idx++];
    byte r = data[idx++];
    
    // Set the pixel in the PImage using the RGB values
    int colour = color(r & 0xFF, g & 0xFF, b & 0xFF);
    pImage.pixels[i] = colour;
  }
  
  pImage.updatePixels();
  return pImage;
}

void stop() {
  try {
    grabber.stop(); // Stop the webcam when the sketch is closed
  } catch (FrameGrabber.Exception e) {
    e.printStackTrace();
  }
  super.stop();
}