I’ve got a new pitch in the pipe work and for it I need to be able to capture the web cam. It’ll have to be video, but I’ve started writing a class that simplifies it for you.
Displaying and capturing the web cam in Actionscript 3 isn’t hard, here’s a nice tutorial from Riacodes.com: Capture images from the webcam. But I thought I’d make it into a class so it would be reusable for your everyday applications.
Interested? Cool, well get the latest code from my Github and use this as your default application’s code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package { import com.firestartermedia.lib.as3.display.component.interaction.ButtonSimple; import com.firestartermedia.lib.as3.display.component.video.WebCam; import com.firestartermedia.lib.as3.utils.DisplayObjectUtil; import flash.display.Bitmap; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.MouseEvent; import flash.text.Font; [SWF( width="580", height="500", frameRate="30", backgroundColor="#FFFFFF" )] public class App extends Sprite { [Embed( systemFont='Arial', fontName='Arial', mimeType='application/x-font', unicodeRange='U+0020,U+0041-U+005A,U+0020,U+0061-U+007A,U+0020-U+002F,U+003A-U+0040,U+005B-U+0060,U+007B-U+007E' )] private var arial:Class; private var webcam:WebCam; public function App() { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; Font.registerFont( arial ); init(); } private function init():void { var button:ButtonSimple = new ButtonSimple(); button.addEventListener( MouseEvent.CLICK, handleButtonClick ); button.border = false; button.buttonText = 'Capture an image!'; button.textFormat.size = 14; button.draw(); addChild( button ); webcam = new WebCam(); addChild( webcam ); button.x = ( webcam.width / 2 ) - ( button.width / 2 ); button.y = webcam.height + 20; } private function handleButtonClick(e:MouseEvent):void { var capture:Bitmap = webcam.captureImage(); DisplayObjectUtil.scale( capture, 150, 150 ); capture.x = webcam.width + 20; addChild( capture ); } } } |
And you’ll get this, don’t you look pretty:
So the next thing I’m going to do is capture video, that shouldn’t be too hard!



