Delay while grabbing frames from a webcam

Hi!

I am grabbing frames from a webcam with the following code.

push_buffer_stream.read(buffer);

image=buffer_to_image.createImage(buffer);

pixel_grabber = new PixelGrabber(image,0,0,width,height,pixels,0,width);

pixel_grabber.grabPixels;

The problem is that when I grab several frames in a row, a delay is accumulated (even when I reinitialise the buffer).

For example, suppose I film a rabbit running from point A to point B. The image I grab when the rabbit has reached point B shows in fact the rabbit only halfway through.

What would be the solution?

Thank you very much!

[638 byte] By [zaphod_beeblebroxa] at [2007-10-3 3:59:40]
# 1

Another behind the scenes thread will be a circular buffer which is grabbing frames, and it may well be being starved out by the processing in your code snippet. Putting thread.yield() between each line in that code of yours may help.

Better though, or as well, approach it another way. What you are doing is demanding on resources, and pixelgrabbing can be especially slow if trying that within live frames.

As you have the buffer then do (byte[] or int[])buffer.getData() to get at the pixel data, then you can work on that to derive the pixel values you need; eg depending upon format :-

for(...

byte red = inData[arraypos + 2];

byte green = inData[arraypos+ 1];

byte blue = inData[arraypos];

int pixel = (red & 0xff) << 16 | (green & 0xff) << 8 | (blue & 0xff) << 0;

etc

andyblea at 2007-7-14 21:58:35 > top of Java-index,Security,Cryptography...
# 2
Why not just record the entire video, then grab the frames from the video afterwards ?regards,Owen
omcgoverna at 2007-7-14 21:58:35 > top of Java-index,Security,Cryptography...