I'm trying to catch two consecutive frames from a webcam, and I tried doing it the following way.
import processing.video.*;
Capture video;
PImage image1, image2; //Images à comparer
int videoVar = 1;
void setup() {
size(800, 600);
video = new Capture(this, Capture.list()[61]);
video.start();
}
void draw() {
image1 = video.get();
int m = videoVar;
while (m == videoVar) {
//println(m);
}
image2 = video.get();
//image(image1, 0, 0);
//image(image2, width/2, 0);
}
void captureEvent(Capture video) {
video.read();
videoVar = videoVar*(-1);
}
The problem is that the while loop, which is supposed to act as a custom delay basically, never exits it self. It only does so if there's something inside the while loop, as a "print('m');" for example. The thing is, printing takes time, and I want my program to be as fast as possible.
Would anyone have a suggestion on what is going on?
I would expect the while
loop to continuously loop itself, regardless of what's inside it.
You shouldn't use a loop as a delay at all. You could possibly use the delay()
function.
But better yet, you should use the millis()
function or the frameCount
variable to check whether a certain amount of time has gone by. Or you should just set a boolean
value that you set from the captureEvent()
function.
If you still can't figure it out, please be more specific about exactly what you're trying to do here.
Edit: Just for fun, can you try adding the keyword volatile
in front of the declaration of videoVar
? The line would look like this: volatile int videoVar = 1;
Then try the while
loop without anything in it.
But besides that, I'm recommending that you change your code to look like this:
boolean doThing = false;
void setup() {
size(800, 600);
}
void draw() {
if(doThing){
// do the thing
doThing = false;
}
}
void captureEvent(Capture video) {
doThing = true;
}
That's just an example, but hopefully it demonstrates the approach. Alternatively, you could use the noLoop()
and loop()
functions:
void setup() {
size(800, 600);
}
void draw() {
// do the thing
noLoop();
}
void captureEvent(Capture video) {
loop();
}