{"id":272,"date":"2008-04-03T21:12:31","date_gmt":"2008-04-04T05:12:31","guid":{"rendered":"http:\/\/dougmccune.com\/blog\/2008\/04\/03\/simple-flex-motion-detection-example\/"},"modified":"2008-11-02T14:01:11","modified_gmt":"2008-11-02T22:01:11","slug":"simple-flex-motion-detection-example","status":"publish","type":"post","link":"https:\/\/dougmccune.com\/blog\/2008\/04\/03\/simple-flex-motion-detection-example\/","title":{"rendered":"Simple Flex Motion Detection example"},"content":{"rendered":"<p>Here&#8217;s a simple motion detection utility that you can use to do motion detection on a webcam, or video, or any other component in your Flex application. It uses a bitmap technique to compare image snapshots and calculate the number of pixels that were changed between snapshots. This gives you a percentage of the total pixels that were changed, which is a crude way to figure out motion. Basically what you do is draw one frame onto the previous frame using the &#8220;difference&#8221; blend mode. Then you threshold the image and you can figure out the number of pixels that had any change.<\/p>\n<p>See the full example after the jump.<br \/>\n<!--more--><\/p>\n<p>The example below uses the motion detector and ties it to a webcam. You&#8217;ll have to allow the app to access your webcam to see it in action. If the motion in the webcam exceeds a certain amount (20%), then it takes a snapshot and sends it to the CIA (ok, ok, it doesn&#8217;t really do that, it doesn&#8217;t send the snapshot anywhere). <\/p>\n<p><div id=\"swf010d1\">This movie requires Flash Player 9.<\/div>\n<script type=\"text\/javascript\">\n\tswfobject.embedSWF(\"http:\/\/dougmccune.com\/flex\/motion\/MotionDetectionExample.swf\", \"swf010d1\", \"350\", \"450\", \"9.0.0\", \"\", {}, {wmode: \"window\", menu: \"false\", quality: \"high\", bgcolor: \"#FFFFFF\", allowScriptAccess: \"always\"}, {});\n<\/script>\n<\/p>\n<p><a href=\"http:\/\/dougmccune.com\/flex\/motion\/srcview\/index.html\">View source<\/a><\/p>\n<p>Someone emailed me recently asking about this code, since it was shown in a <a href=\"https:\/\/dougmccune.com\/blog\/2007\/07\/23\/tileui-desktop-demo-video\/\">previous video I created<\/a> when I was working on TileUI a while back. This is the first in a series of chunks of code that I&#8217;m thinking about taking from that project and releasing open source. No, I don&#8217;t think I&#8217;ll be releasing all of TileUI, but just some of the components within it (like the radial menu component maybe).<\/p>\n<div style=\"border:1px solid silver; padding:5px\"><strong>UPDATE:<\/strong> As various people have pointed out, there is built in motion detection in the Camera class, so if you want to detect motion in a webcam feed all you have to do is monitor the <strong><code>activityLevel<\/code><\/strong> property of the Camera. I guess I should&#8217;ve known that before posting \ud83d\ude1b oh well. This motion detector example is still useful if you&#8217;re trying to detect motion in anything other than a webcam feed though, such as another video playing in your app, or any other UI component that has visible changes.<\/div>\n<p>P.S. I&#8217;ve been very quiet on the blogging front. This is because I&#8217;ve been super crazy mega busy between my job and writing <a href=\"http:\/\/www.wiley.com\/WileyCDA\/WileyTitle\/productCd-0470277920.html\">Flex for Dummies<\/a>, which is available for <a href=\"http:\/\/www.amazon.com\/Adobe-Flex-Dummies-Computer-Tech\/dp\/0470277920\">pre-order on Amazon<\/a>. I expect to get back to blogging much more sometime after May 13 (after I sleep for a week).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a simple motion detection utility that you can use to do motion detection on a webcam, or video, or any other component in your Flex application. It uses a bitmap technique to compare image snapshots and calculate the number of pixels that were changed between snapshots. This gives you a percentage of the total [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-flex"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":1,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions\/346"}],"wp:attachment":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}