webcam access from OpenLaszlo

We’ve written a few Flash applications using OpenLaszlo that need to access a webcam (”ambush“, “puppet“), and render a processed version of its input. This seems a little odd to do, since any webcam processing in practice requires use of a lot of Flash specific classes, so the Flash/DHTML portability of OpenLaszlo is lost. However, OpenLaszlo is such a pleasant way to develop Flash apps that we wanted to use it anyway.

There may be smarter ways, but the somewhat ugly way we found was as follows. First, we create global variables for our input and output bitmaps:

<script>     
  var src_bmp = new flash.display.BitmapData(160,120,true);
  var dest_bmp = new flash.display.BitmapData(160,120,true);
</script>

Then, we do some set-up in a global oninit method to enable our output bitmap:

<method event=“oninit”>
  _root.createEmptyMovieClip(“bmp”, 1);
  _root.bmp.attachBitmap(dest_bmp, 2);
</method>

Now we create a view for our camera input:

<view name=“camwrap” x=“160″>
  <videoview name=“mycamera” width=“320″ height=“240″>
    <camera name=“cam” width=“160″ height=“120″ show=“true”/>
  </videoview>
</view>

Now, we can access both our input and our output as bitmap objects:

var vid = (this.camwrap.mycamera[‘_getflashvideo’] != undefined)
  ? this.camwrap.mycamera._getflashvideo()
  : null;
src_bmp.draw(vid);
// use flash filters to process src_bmp and generate dest_bmp
 

See the adobe flash site for tips on filters you can use. Just make sure to add the full qualification to class names, such as flash.filters.ColorMatrixFilter.

Leave a Reply