Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture photo using Webcam using Actionscript

I have a simple actionscript file with a webcam and an image. What I am looking to do is - when a button is clicked, I want to capture that instant of webcam and image and render it in a DIV on the browser. How do I capture it? I am guessing the bitmapdata needs to be used. I want to do this via code

like image 225
Nikhil Baliga Avatar asked May 17 '12 09:05

Nikhil Baliga


1 Answers

Here's what I wrote myself to answer this question. Tested in Chrome, FF and IE9.

You need Base64 encoder (one is here) and png/jpg encoder (for example from Flex library).

AS code:

package
{
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.external.ExternalInterface;
    import flash.media.Camera;
    import flash.media.Video;

    [SWF(width="640", height="480", backgroundColor="#000000")]
    public class CameraToJS extends Sprite
    {
        private var camera:Camera;
        private var video:Video;

        public function CameraToJS()
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            stage.addEventListener(MouseEvent.CLICK, saveSnapshot);
        }

        protected function addedToStageHandler(event:Event):void
        {
            camera = Camera.getCamera(); 
            video =  new Video();
            video.attachCamera(camera);
            addChild(video);
        }

        protected function saveSnapshot(event:MouseEvent):void
        {
            var bmData:BitmapData = new BitmapData(video.width, video.height);
            bmData.draw(video);

            var encoder:PNGEncoder = new PNGEncoder();

            ExternalInterface.call("image", Base64.encodeByteArray(encoder.encode(bmData)));
        }
    }
}

Javascript code:

function image(data)
{
    document.getElementById("img").src = "data:image/png;base64,"+ data;
}
like image 97
Art Avatar answered Sep 21 '22 23:09

Art