How to take a photo by webcam with Flex Spark

The new Spark them is good, but it is less easy than before to watch the webcam,so here is a tips for do it easily. Here is the source:

<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" width="800" height="300"   >
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import spark.components.mediaClasses.DynamicStreamingVideoSource;
           
            private var _cam:DynamicStreamingVideoSource = new DynamicStreamingVideoSource();           
            protected function imgCamera_creationCompleteHandler(event:FlexEvent):void
            {
                var c:Camera=Camera.getCamera();
                imgCamera.videoObject.attachCamera(c);
            }
           
            protected function btnTakePhoto_clickHandler(event:MouseEvent):void
            {
                var imgBD: BitmapData = new BitmapData(imgCamera.width,imgCamera.height);
                imgBD.draw(imgCamera);
                imgResult.source=imgBD;
            }
           
        ]]>
    </fx:Script>
    <fx:Declarations>
    </fx:Declarations>
    <s:VideoDisplay id="imgCamera" x="19" y="6" width="320" height="240" creationComplete="imgCamera_creationCompleteHandler(event)" source="_cam"/>
    <s:Button id="btnTakePhoto" x="360" y="10" label="Take photo"       click="btnTakePhoto_clickHandler(event)"/>
    <s:Image id="imgResult" x="449" y="10" width="320" height="240"/>
</s:TitleWindow>

The main point is too use "source="_cam"" for be able to attach the webcam !

PS: for stop the video : imgCamera.source=null; (that's silly, but i searched 1 hour)

Aucun commentaire: