Topic: Best way to have a webcam load images with a listener?

I'm working on some code to load a webcam using a listener, which would load the images at the fastest possible speed.

For those who don't know, a listener fires an event after the image is fully loaded. So right after frame 1 is loaded, it could start loading frame 2, etc. The current method used by the webcam sample can easily get overbuffered and display a blank screen.

I'm having a couple of problems with the listener:

- how to load the image into an invisible movie clip, and have it become visible only after loading?

- how to repeat the function without recursion? In other words, currently when the image is loaded it allows us to fire some event, and if we fire the doLoad function again that would be doLoad executing doLoad, which is a recursion problem.

Here's some starter code I have, does anyone have any ideas?




// URL of webcam
url = "http://216.203.123.137/jpg/image.jpg";

// movie clip to hold the webcam image
this.createEmptyMovieClip("cam", 1);

function doLoad() {
   
    start_time = (new Date()).getTime();   
   
    // Defeat caching by adding a dummmy query string
    var url = url + "?" + start_time;
   
    // Our movie loader
    var loader:MovieClipLoader = new MovieClipLoader();

    loader.loadClip(url, cam);
   
    var preload:Object = new Object();
    loader.addListener(preload);
   
    preload.onLoadStart = function(target){
        trace("Started Loading");
    }
   
    preload.onLoadProgress = function (target, loadedBytes, totalBytes){
        trace(Math.floor((loadedBytes/totalBytes)*100)+"% loaded");
    }
   
    preload.onLoadComplete = function(target){
        finish_time = (new Date()).getTime();   
        total_time = (finish_time - start_time) / 1000;
        trace ("Loaded " + url + " in " + total_time + " seconds");
       
        // Scaling it for the tiny Chumby screen (320 x 240)
        cam._yscale = 50;
        cam._xscale = 50;
       
        // Now load the next one!
        // ?????
       
    }

}

doLoad();

2 (edited by gingerbeardman 2008-03-11 13:19:55)

Re: Best way to have a webcam load images with a listener?

onLoadComplete, set a flag/semaphore/boolean variable that you can check from a controller function/movieclip/etc that will fire off the next doLoad

cotrolling movieclips are great ways to sidestep the innaccuracy of the interval/timeout method of polling

here's how I do my clocks: http://animation.about.com/od/flashanim … lock_4.htm

hope that helps,
matt

3 (edited by wrybread 2008-03-11 16:25:56)

Re: Best way to have a webcam load images with a listener?

Good idea...

Hmm, having trouble posting... For some reason whenever I post it truncates the code, so I posted it here:

http://gizmoware.net/chumby/webcam_template.txt

This works pretty well, and even scales the image for the Chumby screen.

Note that I'm on the road and my Chumby isn't managing to join the hotel's wifi network so I haven't tested it on my Chumby yet... Anyone feel like giving it a try?

One thing that should be fixed is its one reference to _level0, which would prevent widgets made using this method from working in virtual chumbies... Anyone know of a way around that? I tried doing it with _root instead but that didn't work in this case.

Re: Best way to have a webcam load images with a listener?

If anyone needs it, I posted a webcam template to my Chumby page here:

http://gizmoware.net/chumby

Re: Best way to have a webcam load images with a listener?

Thanks wrybread.  I've used your webcam template with lots of success.  It works great.

Re: Best way to have a webcam load images with a listener?

Great.

If you ever need to combine movie clips from the timeline and the webcam image, let me know and I'll post an example of a how I'm doing it. I have no clue if this is the best way to do it, but it works.

In a nutshell you need to put everything on the timeline in a movie clip (could be individual, could be all one), give it an instance name, then you need to assign a level to your movie clip using SwapDepths. Otherwise it all gets covered by the webcam images.

For example if the instance name of the movie on the timeline was "label", I'd add this line to make it on level 9999:

label.swapDepths(9999);

Note that if your webcam image goes above level 9999 it'll then cover your "label" movie, so I add this to the doLoad command to keep the webcam level from getting this high. Remember that "counter" is what determines the level of the cam.

if (counter > 600) {
  trace("RESTARTING COUNTER AND MOVIE LEVEL");
  counter = 0;
  // Remove the last few movie cips so they don't cover our new ones
  _level0.cam601.removeMovieClip();
  _level0.cam600.removeMovieClip();
  _level0.cam599.removeMovieClip();
  _level0.cam598.removeMovieClip();
}