Webcam capture and PGraphics

import processing.video.*;

Capture cam;

PGraphics pg1;

void setup() {
  size(950, 950);
  frameRate(10);
  pg1 = createGraphics(475, 475);

    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]);
    }
    
    cam = new Capture(this, cameras[0]);
      print(cam.width +"  "+cam.height); 
    cam.start();     
  }
}

void draw() {
  background(255);
    if (cam.available() == true) {
    cam.read();
  }
  
   //image(cam, 0, 475);  // check commented vs. uncommented
   pg1.beginDraw();
   pg1.image(cam, 0, 0);
   pg1.loadPixels();
        for (int y = 0; y < cam.height; y++) {
            for (int x = 0; x < cam.width; x++) {
              int index = (x + y * 467);
              color c = pg1.pixels[index]; 
              pg1.pixels[index] = color(red(c), int(random(100,200)), blue(c));

        }
      }
   pg1.updatePixels();
   pg1.endDraw();

   image(pg1,0,0); 
   image(cam, 475, 475); 
 
}

please check the code above, with line image(cam, 0, 475); either commented or uncommented.
In the latter case, the image(pg1,0,0); is rendered as a noise green box, and I get two webcam captures.
In the former case, the image(pg1,0,0); is rendered ok as a filtered webcam capture, but image(cam, 475, 475); is rendered as a black box.

Either case, why so? I don’t get how it works.

Hello,

Your code seems to behave with the P2D renderer:

  size(950, 950, P2D);
  pg1 = createGraphics(475, 475, P2D);

I do not have an immediate answer.
I find myself exploring a lot and working around issues from experience.

:)

Hello @mario60,

Give this a try:

import processing.video.*;

Capture cam;

PGraphics pg1;

PImage img;

void setup() {
  size(950, 950, JAVA2D);
  frameRate(10);
  pg1 = createGraphics(475, 475, JAVA2D);
  img = createImage(475, 475, RGB);

  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]);
    }

    cam = new Capture(this, cameras[0]);
    print(cam.width +"  "+cam.height);
    cam.start();
  }
}

void draw() {
  background(255);
  if (cam.available() == true) {
    cam.read();
  }

  image(cam, 0, 475);  // check commented vs. uncommented
  
  img = cam.copy();
  
  pg1.beginDraw();
  //pg1.image(cam, 0, 0); // < Try this
  pg1.image(img, 0, 0);   // < Or this
  pg1.loadPixels();
  for (int y = 0; y < pg1.height; y++) {
    for (int x = 0; x < pg1.width; x++) {
      int index = (x + y * pg1.width);
      color c = pg1.pixels[index];
      pg1.pixels[index] = color(red(c), int(random(100, 200)), blue(c));
      //pg1.pixels[index] = color(red(c), blue(c), green(c));  
    }   
  }
  pg1.updatePixels();
  pg1.textAlign(CENTER, CENTER);
  pg1.textSize(96);
  pg1.text("TEST", pg1.width/2, pg1.height/2); 
  pg1.endDraw();

  image(pg1, 0, 0);
  image(cam, 475, 475);
}

I made a copy of cam to img to work with.
You could also edit the img.pixels array directly after you make a copy.
You used PGraphics so I took advantage of this and added some text.

JAVA2D is the default renderer.

:)

1 Like

Helo glv

It works. Thanks so much.
I work from experience too.
Take care
m.

2 Likes