<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-32584115</id><updated>2011-12-27T01:00:40.615+01:00</updated><title type='text'>Sander Kruger's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sanderkruger.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/32584115/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sanderkruger.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sander Kruger</name><uri>http://www.blogger.com/profile/13607700055993678292</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-32584115.post-4640856559858607465</id><published>2007-02-20T08:52:00.000+01:00</published><updated>2007-02-20T09:37:09.272+01:00</updated><title type='text'>Working with Cameras in AS3</title><content type='html'>Trying to get my Flash application to work with multiple cameras hasn't been a breeze, so to speak. Detecting cameras and their capabilities is a bit difficult with Flash, at least if you need it to work on most platforms.&lt;br /&gt;&lt;br /&gt;The Flash Camera API is quite limited, but, when used wisely, will allow you to get more information about the connected camera than you'd think.&lt;br /&gt;&lt;br /&gt;I've been able to get good results by taking a number of steps in acquiring a camera. First, let's think about the different cases that may keep your Flash application from getting a good signal:&lt;br /&gt;1) there is no camera driver on the computer&lt;br /&gt;2) there's a shutter in front of the camera&lt;br /&gt;3) another program is using the camera&lt;br /&gt;4) there are several drivers for the camera, but the default driver won't work with Flash&lt;br /&gt;5) there are drivers, but the camera is not connected&lt;br /&gt;&lt;br /&gt;It would be great to be able to distinguish between these cases, so that you may prompt the user with information on what to do to fix the problem. It turns out you can, at least almost.&lt;br /&gt;&lt;br /&gt;The first thing to do is getting the default camera:&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;var cam :Camera = Camera.getCamera();&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;If there is no camera driver in the system, this method will return &lt;span style="font-weight: bold;font-family:courier new;" &gt;null&lt;/span&gt;, so case 1) is solved. If there is, you will get a reference to the default camera, although this doesn't mean you will actually get access to it. The next step is to try and get access by attaching the camera stream to a video display.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;cam.addEventListener( StatusEvent.STATUS , onCamStatus );&lt;br /&gt;video.attachCamera( cam );&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;If this is the first time requesting a camera, this will make Flash Player prompt the user to allow (or deny) access to the camera. Let's suppose that the user allows access. This will result in &lt;span style="font-weight: bold;font-family:courier new;" &gt;onCamStatus&lt;/span&gt; being called.&lt;br /&gt;&lt;br /&gt;The next step is to verify that the camera actually has something to show. Unfortunately, Flash Player isn't much help there. Even if the camera doesn't have a signal, it won't peep. The best way to figure out if the camera is working is to check the motion level and the frame rate. BUT! don't check it right away, or be fooled. Now, here's where it gets tricky. To be able to monitor the activity level, you have to attach a listener. Otherwise, the activityLevel is always 0.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;cam.addEventListener( ActivityEvent.ACTIVITY, onCamActivity );&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Now, here's the trick: When you activate the camera and the motion detector, the activity spikes immediately (presumably because the first captured frame is a lot different from no frame at all). But this is not the kind of activity we're interested in. So the activity events are no use. But the activity detector IS. Instead of using the ActivityEvent, we need to poll the framerate and activity level with a timer.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;timer.addEventListener( TimerEvent.TIMER, onCamCheck );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;timer.start();&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;On the timer event, check the &lt;span style="font-family: courier new; font-weight: bold;"&gt;cam.currentFPS&lt;/span&gt; and &lt;span style="font-family: courier new; font-weight: bold;"&gt;cam.activityLevel&lt;/span&gt;. Now, there are different possible outcomes of this. Let's say you check these values a few dozen times. If the framerate is 0 all the time, it means that Flash doesn't get any signal from the camera (case 3, 4, 5). If you DO get a framerate &gt; 0, but the activityLevel doesn't go beyond, say, 5, it probably means that there's a shutter in front of the camera or your user is a stiff (case 2).&lt;br /&gt;&lt;br /&gt;In case 3, 4 or 5, it is a good idea to check whether there are other cameras and to prompt the user to select one. Here you need another little trick to make it work.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;if (Camera.names.length &gt; 1)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  dialog = new SelectDialog();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  dialog.addEventListener( Event.SELECT, onSelectCam );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  PopUpManager.addPopUp( dialog, Sprite( Application.application ) );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Here, &lt;span style="font-family: courier new; font-weight: bold;"&gt;SelectDialog&lt;/span&gt; is your own brewed dialog window that dispatches the &lt;span style="font-family: courier new; font-weight: bold;"&gt;SELECT&lt;/span&gt; event if the user selects another camera. In my case, the SelectDialog class has a property &lt;span style="font-family: courier new; font-weight: bold;"&gt;selectedCam&lt;/span&gt;, which returns the selected index in the &lt;span style="font-family: courier new; font-weight: bold;"&gt;Camera.names&lt;/span&gt; array. The reason for this is that &lt;span style="font-family: courier new; font-weight: bold;"&gt;Camera.getCamera()&lt;/span&gt; doesn't accept the actual name of a camera, but it's index (contrary to documentation). So, here's the code for selecting the right camera:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;private function onSelectCam( event :Event ) :void&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  var index :String = dialog.selectedCam;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  var name :String = Camera.names[index];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;  cam = Camera.getCamera( index );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;And there it is, all the building blocks you need to successfully get camera access. While it is not 100% bullet-proof, this will give you your best shot at helping the user fix his camera problems.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/32584115-4640856559858607465?l=sanderkruger.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sanderkruger.blogspot.com/feeds/4640856559858607465/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=32584115&amp;postID=4640856559858607465' title='92 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/32584115/posts/default/4640856559858607465'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/32584115/posts/default/4640856559858607465'/><link rel='alternate' type='text/html' href='http://sanderkruger.blogspot.com/2007/02/working-with-cameras-in-as3.html' title='Working with Cameras in AS3'/><author><name>Sander Kruger</name><uri>http://www.blogger.com/profile/13607700055993678292</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>92</thr:total></entry><entry><id>tag:blogger.com,1999:blog-32584115.post-115533264357651738</id><published>2006-08-11T21:38:00.000+01:00</published><updated>2007-02-13T22:43:30.594+01:00</updated><title type='text'>Having fun with Bitmaps in AS3</title><content type='html'>So, I wanted to grab a snapshot from my camera in Flash Player and send the image to the server to share it in a web conference.&lt;br /&gt;&lt;br /&gt;Thanks to the excellent work of Tinic Uro of Adobe I started off with adding his AS3 port of a JPEG encoder to my app to make sure that it doesn't send megabytes data to my server. Have a look &lt;a href="http://www.kaourantin.net/2005/10/more-fun-with-image-formats-in-as3.html"&gt;here&lt;/a&gt; for his article on the AS3 JPEGEncoder.&lt;br /&gt;&lt;br /&gt;It turned out that my pc is a bit slow and will take about 8 seconds to encode a 640x480 bitmap. This wouldn't be so bad if Flash Player was multi-threaded, but as it is, the player (and the browser that runs it) will block during the encoding. On my newer laptop, the encoding only takes about 1 second. But hey, I'm probably not the only one that has an older PC, so I decided to do something about the blocking.&lt;br /&gt;&lt;br /&gt;I modified the JPEGEncoder class so that it chops up the work in chunks and automatically adapts the chunksize to a maximum blocking delay that is configurable. It also emits ProgressEvent's and a complete event when done. And I threw in a cancel() function as well. Here's the source for &lt;a href="http://www.doubleclickconferencing.com/sources/JPEGEncoder.as"&gt;JPEGEncoder with ProgressEvent&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;With this modification, the encoder pauses for 10 ms about every 200ms to let the player do other things like updating the display list while only adding about 5% to the processing time. And I added a progress bar to show the user how the encoder is doing.&lt;br /&gt;&lt;br /&gt;Have fun with it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/32584115-115533264357651738?l=sanderkruger.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sanderkruger.blogspot.com/feeds/115533264357651738/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=32584115&amp;postID=115533264357651738' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/32584115/posts/default/115533264357651738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/32584115/posts/default/115533264357651738'/><link rel='alternate' type='text/html' href='http://sanderkruger.blogspot.com/2006/08/kick-off.html' title='Having fun with Bitmaps in AS3'/><author><name>Sander Kruger</name><uri>http://www.blogger.com/profile/13607700055993678292</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry></feed>
