• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3423
  • Last Modified:

AS3: Track motion within an interactive webcam.

I have a webcam viewed in Flash and I can detect when there is motion.  What I need to do now is determine where on the stage (where the webcam is viewed and motion detected) the motion is at on the stage.  Track the motion.  I want to be able to place mcs on the stage and when determining that there is webcam motion over that area on the stage fire a function.  Does anyone know the syntax for this?
var camera:Camera = Camera.getCamera();
var video:Video = new Video();
 
camera.setMode(720,540,15,true);
camera.setQuality(0, 100);
video.attachCamera(camera);
video.width = camera.width;
video.height = camera.height;
camera.setMotionLevel(10, 1000);
 
addChild(video);
 
addChildAt(button, 2);
 
camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
 
 
function activityHandler(event:ActivityEvent):void {
	trace("activityHandler: " + event);
	trace("event.activating: " + event.activating);
	if (event.activating == true) {
		trace("movement has occured");
	} else if (event.activating == false) {
		trace("nope");
	}
}

Open in new window

0
cubical38
Asked:
cubical38
  • 3
1 Solution
 
john_hollingsCommented:
Not entirely sure where to begin however I do know about the following tutorial which seems to touch on this type of activity

http://blog.soulwire.co.uk/flash/actionscript-3/webcam-motion-detection-tracking/

Might be worth checking out.
0
 
ccareyCommented:
It's a bit of an exercise. What you need to do is compare each rendered frame with the last and apply a difference filter to determine areas of motion.
Here's an article from Guy Watson outlining the way it works
http://www.adobe.com/devnet/flash/articles/webcam_motion.html

0
 
cubical38Author Commented:
Thus far I have (code below), what I need now is the syntax for comparing the the now bitmap with the previous bitmap...  Anyone now how to do this?



import flash.display.BitmapData;
import flash.geom.Matrix;
 
 
var camera:Camera = Camera.getCamera();
var video:Video = new Video();
var snapshot:BitmapData = new BitmapData(video.width,video.height);
var now:BitmapData;
var old:BitmapData;
var matrix:Matrix;
var trackSpot:MovieClip = new MovieClip();
 
camera.setMode(720,540,15,true);
camera.setQuality(0, 100);
camera.setMotionLevel(10, 1000);
video.attachCamera(camera);
video.width = camera.width;
video.height = camera.height;
 
 
 
addChild(video);
addChildAt(button, 2);
 
camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
 
 
function activityHandler(event:ActivityEvent):void {
	trace("activityHandler: " + event);
	trace("event.activating: " + event.activating);
	if (event.activating == true) {
		trace("movement has occured");
		snapshot = new BitmapData(video.width, video.height);
		snapshot.draw(video);
		trackSpot.graphics.beginBitmapFill(snapshot);
		trackSpot.graphics.drawRect(0,0,100,100);
		trackSpot.graphics.endFill();
		
		addChild(trackSpot);
	} else if (event.activating == false) {
		trace("nope");
	}
}

Open in new window

0
 
cubical38Author Commented:
With the following code, I am still not seeing the intended text within the asp page...  Any suggestions?

Thanks for all the info and the replies, by the way...
ASP
 
<% Set rs = conn.execute("Select * from insertfiles where id=52")
				reminder = rs("insertfilehtml")
				rs.Close
				'response.write inserthtml
				
			 %>
 
<script language="javascript">AC_FL_RunContent = 0;</script>
<script src="AC_RunActiveContent.js" language="javascript"></script>
 
<h1><a href="index.asp">Apartment association GREATER DALLAS</a></h1>
<%if reminder<>"" AND reminder<>"&nbsp;" AND reminder<>" " then%>
<div class="important-info">
  <script type="text/javascript" src="js/swfobject.js"></script>
  <div id="flashcontent"> This text is replaced by the Flash movie. </div>
  <script type="text/javascript">
   	var so = new SWFObject("flash/important.swf", "important", "575", "30", "9", "#ffffff");
	//so.addVariable("reminder", "<%= linkName %>");
	so.addParam("FlashVars", "reminder=<%= reminder%>&");
	so.addParam("wmode", "transparent");
   	so.write("flashcontent");
</script>
</div>
<% end if %>
 
AS3
 
//root.loaderInfo.addEventListener(Event.COMPLETE, onLoaderInfoComplete);
 
function onLoaderInfoComplete(e:Event):void
{
        var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters;
		important_mc.reminder.text = allFlashVars.reminder;
        trace( allFlashVars.reminder );
}

Open in new window

0
 
cubical38Author Commented:
I was masking the text...  this code works
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now