Paint on Webcam Stream

Hello @Atelierista,

A simple example that will draw with the mouse on an offscreen buffer with a transparent background and superimpose it on the webcam image:

import processing.video.*;

Capture cam;

PGraphics pg;
boolean clear;

void setup() 
  {
  size(640, 480);
  pg = createGraphics(640, 480);

  String[] cameras = Capture.list();
  
  if (cameras.length == 0) 
    {
    println("There are no cameras available for capture.");
    exit();
    } 
  else 
    {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) 
      {
      println(cameras[i]);
      }
    
    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[0]);
    cam.start();     
    }      
    
  pg.beginDraw();
  pg.clear();   // Transparent background
  pg.endDraw();      
  }

void draw() 
  {
  if (cam.available() == true) 
    {
    cam.read();
    }
  
  pg.beginDraw();
  pg.strokeWeight(10);
  pg.stroke(mouseX*255.0/640, mouseY*255/480, 0);
  pg.point(mouseX, mouseY);
  pg.endDraw();
  
  // The following does the same, and is faster when just drawing the image
  // without any additional resizing, transformations, or tint.
  set(0, 0, cam);
  image(pg, 0, 0);
  }
  
void mousePressed()
  {
  pg.clear();
  }

References:

Drawing pad:

:)

1 Like