# Webcam video for Linux workaround

**URL:** https://discourse.processing.org/t/webcam-video-for-linux-workaround/45784
**Category:** Libraries
**Created:** [February 16, 2025, 4:03pm UTC](https://discourse.processing.org/t/webcam-video-for-linux-workaround/45784 "2025-02-16T16:03:58Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Seamiki](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@Seamiki](https://discourse.processing.org/u/Seamiki)
#### Post date: [February 16, 2025, 4:03pm UTC](https://discourse.processing.org/t/webcam-video-for-linux-workaround/45784/1 "2025-02-16T16:03:58Z")

</div>

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](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.

```Processing
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();
}

```
