flash on 2009-10-28

by m0ose
♥0 | Line 131 | Modified 2009-10-28 02:56:00 | MIT License
play

ActionScript3 source code

/**
 * Copyright m0ose ( http://wonderfl.net/user/m0ose )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oC9i
 */

package {
        import flash.display.*;
        import flash.events.*;
        import flash.display.BitmapData;
	    import flash.display.Shape;
	    import flash.geom.*;
	    import flash.media.Camera
	    import flash.media.Video
	    import flash.utils.Timer
	    
		 [SWF(frameRate=60)]
            public class Laser_fallow extends Sprite
            {
                public var cam:Camera
		    public var vid:Video
		    public var curr:BitmapData
		    public var prev:BitmapData
		    public var showing:BitmapData
		    public var looptimer:Timer
		
                    public function Laser_fallow()
                    {
                           //curr = new BitmapData( 640, 480)

                            change_cam()
                                      
                    }
			public function change_cam(e:Event = null):void
			{
					//var zt:String = list.selectedIndex.toString()
					cam = Camera.getCamera()
					if(cam){
						cam.setMode( 640,480, 10)
						
						vid = new Video(cam.width, cam.height)
						vid.attachCamera( cam)
						
						curr = new BitmapData( cam.width, cam.height)
						prev = new BitmapData( cam.width, cam.height)
						showing= curr.clone()
						stage.addChild( new Bitmap( showing) ).name = "silverscreen"
					}
					if( !looptimer)
					{
						looptimer = new Timer(100)
						looptimer.addEventListener(TimerEvent.TIMER, loop)
						looptimer.start()
						
					}
			}
			public function loop(e:TimerEvent):void
			{	
				if( curr)
				{
					prev.draw( curr)
					curr.draw( vid)
				}
				
				/*if( curr)
					img1.source = new Bitmap(curr)
			*/
				if( prev){
					showing.draw( new Bitmap( get_laser( prev)  )  )
	                            }
	                            
			}


			public var dots:Array = []
			public function get_laser( b1:BitmapData):BitmapData
			{  
				
				var activeRectangle:Rectangle = activeRect( prev, curr)
				
				var cur_p:Point = findBrightest( b1.clone(), activeRectangle )
				var cur_rect:Rectangle = new Rectangle( cur_p.x, cur_p.y, 10, 10)
				//b1.fillRect( cur_rect  , 0xffff0000)
				
				var sh:Shape = new Shape()
				sh.graphics.lineStyle( 5, 0x77ff0000)
				if( dots.length > 0)
					sh.graphics.moveTo( dots[0].x, dots[0].y)
				for( var i:int = 0 ; i < dots.length; i++)
				{	
					if( dots.length > 30){
						dots.shift()
					}
					else
					{
						sh.graphics.lineTo( dots[i].x, dots[i].y)
						//b1. fillRect( new Rectangle( dots[i].x, dots[i].y, 5,5) , 0x55ff0000)	
					}
				}
				
				//b1.fillRect( activeRectangle, 0x4400ff00)
				
				b1.draw( sh)
				if( cur_p.x > 0 && cur_p.y > 0){
					dots.push( cur_p)}
				

				return b1	
			}
		
			public function findBrightest( bm:BitmapData, rect:Rectangle = null):Point
			{
				var scale:Number=0.5
				var bp:Point = new Point(0,0)
				var bv:uint = 0
				//var tmpIS:ImageStuff = new ImageStuff()
				var matrix:Matrix = new Matrix
				matrix.scale( scale, scale)
				
				
				var bm_small:BitmapData = new BitmapData( bm.width * scale, bm.height * scale)
				bm_small.draw( bm, matrix)
				
				var rect2:Rectangle
				if(rect == null){
					rect2 = bm_small.rect }
				else
				{
					rect2 = new Rectangle( rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale)
				}
				
				
				//img1.source = new Bitmap( bm_small)
				
				for( var x:int = rect2.topLeft.x ; x <= rect2.bottomRight.x ; x++)
				{
					for( var y:int = rect2.topLeft.y ; y< rect2.bottomRight.y ; y++)
					{
						var tmpv:uint = bm_small.getPixel(x, y)
						if(AbiggerthanB(tmpv, bv)){
							bv = tmpv
							bp.x = x; bp.y = y	
						}
					}
				}
				bp.x = bp.x  /scale
				bp.y = bp.y / scale
			
				return bp	
			}
			public function activeRect( old:BitmapData, cur:BitmapData):Rectangle
			{
				var old2:BitmapData = old.clone()
				old2.draw( cur, null, null, "difference")
				old2.threshold( old2, old2.rect, new Point(0,0), ">", 0xff444444, 0xffffffff)
				//old2.threshold( old2, old2.rect, new Point(0,0), "<", 0xff444444, 0x00000000)

				var recta:Rectangle = old2.getColorBoundsRect( 0xffffffff, 0xffffffff, true)
				return recta
			}
				public function AbiggerthanB( a:uint , b:uint):Boolean
		        {
			//this takes two pixels as input
				
			var ar:uint = a >> 16 & 0xff //red
			var ag:uint = a >> 8 & 0xff//green
			var ab:uint = a & 0xff//blue
			
			var br:uint = b >> 16 & 0xff
			var bg:uint = b >> 8 & 0xff
			var bb:uint = b & 0xff
			
			if(ar >= br && ag >= bg && ab >= bb)
				return true;
			else
				return false;	
	        }
		   
		   
		 }
	 }