<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:media="flash.media.*" xmlns:components="com.flexstuff.components.*"
    xmlns:shapes="com.flexstuff.shapes.*"
    creationPolicy="all"
    creationComplete="init()" viewSourceURL="srcview/index.html">
    
    <!--
    The code below could not have been writen without the help of 2 awsome articles.  You guys rule!!
    
    http://www.adobe.com/devnet/flash/articles/webcam_motion.html - Guy Watson
    http://www.tobytremayne.com/index.cfm/2007/3/3/Flex-motion-tracking - Toby Tremayne 
    -->
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import flash.utils.setInterval;
            import flash.utils.setTimeout;
            
            private var _before:BitmapData = null;
            private var _now:BitmapData = null;
            private var _keycolor:uint = 0xFF00FF00;
            private var _pixel_hitCount:int;
            private var _hitCount:int = 0;
            private var _inImage:Boolean = false;
            private var _pauseCount:int = 0;
            private const _HIT_THRESHOLD:int = 800;
            private const _PAUSE_THRESHOLD:int = 4;
            
            private function init() : void
            {
                setInterval(takeSnapshot, 200 );
            }
            
            private function takeSnapshot() : void
            {
                if(_inImage == false)
                {
                    _inImage = true;
                    _now = camera.getSnapshot();
                    if(_before !=null )
                    {
                        _before.draw(_now, null, null, BlendMode.DIFFERENCE);
                        
                        _before.threshold(_before, _before.rect, _before.rect.topLeft, ">", 0xFF111111, _keycolor, 0x00FFFFFF, false);
                        _before.threshold(_before, _before.rect, _before.rect.topLeft, "!=", _keycolor, 0x00FFFFFF, 0x00FFFFFF, true);
                        
                        var pixels:ByteArray = _before.getPixels(new Rectangle(0,0,50,50));
                        _pixel_hitCount = 0;
                    
                        for(var i:int; i<pixels.length; i++)
                        { 
                            if(pixels[i] == 255)
                            {
                                if(++_pixel_hitCount > _HIT_THRESHOLD && _pauseCount > _PAUSE_THRESHOLD )
                                {
                                    _pauseCount = 0;
                                    _hitCount++;
                                    number.text = _hitCount.toString();
                                    break;
                                }
                            }    
                        }
                        
                        if( i== pixels.length )
                        {
                            _pauseCount++;
                        }
                        
                    }

                    _before = _now.clone();
                    _now = null;
                    _inImage = false;
                }
            }
    
        ]]>
    </mx:Script>
    
    
    <mx:Canvas top="0" bottom="0" left="0" right="0">
            <components:VideoCamera id="camera" height="100%" width="100%" visible="true"/>
            <mx:Canvas id="block" height="50" width="50" backgroundColor="white">
                <mx:Label id="number" fontSize="28" fontWeight="bold"/>
            </mx:Canvas>        
    </mx:Canvas>
</mx:Application>