Trying to mimic a simply copy webcam function - stuck on first frame.

import processing.video.*;
Capture cam;
PImage img;

void setup(){
  size(640*2, 480*2);
  cam = new Capture(this);
  cam.start();
  img = createImage(640, 480, RGB);
}

void draw() { 
   if (cam.available()) {
    cam.read(); 
  }
  cam.loadPixels();
  image(cam, 0, 0);
  img.loadPixels();
  if (cam.pixels.length>0){

    for(int i = 0; i<640*480; i++){
      float r = red(cam.pixels[i]);
      float g = green(cam.pixels[i]);
      float b = blue(cam.pixels[i]);
      img.pixels[i] = color(r,g,b);  
    }
  image(img, 640, 480);
  }

  print("works..");

}

That's the code. yet all i get on the 'img' displayed is the first frame frozen from the cam. The print 'works' however repeatedly prints, and i do not understand why should the cam.read() also repeatedly work and thus provide img with the full stream of frames. (The natural cam display - image(cam, 0, 0) works well).

any advice?

Answers

Sign In or Register to comment.