Capture images from the webcam

July 6th, 2009 in Flash | 50 Comments

In this tutorial, we are going to learn how to take image captures from the webcam.

View DemoDownload Source

You might be interested in looking at a sequel of this tutorial : Captures images from the webcam and save them to the desktop

1. Create a new flash file (Actionscript 3.0) and save it as captureWebcam.fla.

2. Rename ‘layer 1′ to ‘button’. Create a button on the stage, convert it to a movie clip and give it an instance name of capture_mc.

3. Create a new layer named ‘actions’ and with its first frame selected open the actions panel.
All that we are going to do now happens in the actionscript code.

4. First we initialize a webcam by attaching a camera to a Video object that we add to the stage:

var cam:Camera = Camera.getCamera();
var video:Video = new Video(320,240);
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);

5. Then, we need to use the Bitmap and BitmapData classes to store the image that we want to capture and display on the stage.
We start by importing the Bitmap and BitmapData classes:

import flash.display.Bitmap;
import flash.display.BitmapData;

6. Then create a new BitmapData object of the same proportions of the video object to store the BitmapData captured from the playing webcam.
And create a bitmap object to display the data from the BitmapData object and add it to the stage.

var bitmapData:BitmapData = new BitmapData(video.width,video.height);

var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 360;
bitmap.y = 20;
addChild(bitmap);

7. Finally, after setting its buttonMode property to true, we add a Click event listener to our button. This event is handled by the captureImage function that draws the image that is currently displaying from the webcam into our bitmapData object.

capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

function captureImage(e:MouseEvent):void {
	bitmapData.draw(video);
}

8. Here is the final code :

import flash.display.Bitmap;
import flash.display.BitmapData;

var cam:Camera = Camera.getCamera();
var video:Video = new Video(320,240);
video.attachCamera(cam);
video.x = 20;
video.y = 20;
addChild(video);

var bitmapData:BitmapData = new BitmapData(video.width,video.height);

var bitmap:Bitmap = new Bitmap(bitmapData);
bitmap.x = 360;
bitmap.y = 20;
addChild(bitmap);

capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

function captureImage(e:MouseEvent):void {
	bitmapData.draw(video);
}

9. Test your movie to see it in action.

  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Twitter


What you think ...

(50 Comments)

+ Add a Comment
  1. Paul
    Jul 8th, 2009

    Great stuff …
    Thanks

  2. Márcio Guerra
    Jul 11th, 2009

    Hi! This is nice, yet I knew it from AS2. Thanks for the upgrade. Seems a lot similar… Just one thing. Do you know if it is possible to do it also in Director? I know that Director accepts SWF. files from Flash, but, in Director itself, can we do it similarly without any SWF, just «old plain» DIR. files?
    Thanks

    Márcio Guerra

  3. Sudeep Gurung
    Jul 13th, 2009

    Great tutorial, thanks a lot.

    Is there any way to give a name to the captured image and save it on the server side or the same computer?

  4. surendar singh rathod
    Jul 14th, 2009

    hey thanks for this i need for catching my friend pics from webcam

  5. Flash tutorials | Some Special Flash Effect | Lemlinh.com
    Jul 21st, 2009

    [...] Read tutorial  [...]

  6. Albert
    Jul 21st, 2009

    Hi really nice tutorial, i am a bit of a beginner when it comes to AS, but I am wondering how to do the exact same thing but for AS 1 or 2 so I can incorporate this function in my flash project?

  7. admin
    Jul 24th, 2009

    @ALBERT : Hello,
    Sorry but our site offers only AS3 tutorials.
    By the way, we encourage you to upgrade to AS3.
    Good luck

  8. Marcel
    Jul 24th, 2009

    Thanks you!

  9. Script
    Jul 27th, 2009

    nice tutorial

  10. Ryan Barnes
    Jul 28th, 2009

    This method dosen’t seem to be picking up my Macbook Pros webcam. Any idea why that’s happening?

  11. admin
    Jul 29th, 2009

    @ RYAN BARNES :
    Hi Ryan, sorry but we haven’t tested this code on a Mac. We’ll reach you when we find out the solution.

  12. Ryan Barnes
    Jul 30th, 2009

    Issue resolved. The problem came from the settings. If you right click on the .swf, go to settings, then click on the camera, then drop down the menu and choose ‘USB Class Video’ rather then the default ‘DV Video’, it will work on your new Macbook Pro.

    Thanks again!

  13. Piotr
    Aug 1st, 2009

    I have tested it on MacBookPro and I can confirm it :)

    GR8 stuff

    I would like to make a translation of this post on my blog, of course with linking to this site and U as author – is it ok for you?

  14. admin
    Aug 1st, 2009

    @ PIOTR :
    Hi Piotr, yes you can translate this post on your blog with reference to our site like you said.
    Stay tuned for more tuts …

  15. chandra_gpe
    Aug 17th, 2009

    nice tutorial…im a beginer, just wondering how to save the image capture in to a database of image..
    thx

  16. Ben
    Aug 18th, 2009

    Hello there, thank you for sharing this great little Flash asset. I’m wondering how could I save the taken picture on my server ?

  17. Ahmed Nuaman — Freelance Designer and Developer — Blog — Actionscript 3 Web Cam Capture Made Easy
    Sep 18th, 2009

    [...] 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 [...]

  18. Jyothidhar
    Sep 21st, 2009

    Good1 u saved me alot of time.
    Thnx keep up the good work

  19. scotty
    Sep 22nd, 2009

    Ryan-Thanks for figuring out the USB Class Video for the Mac…… I was so confused on why it would enable my cam on a iMac…. for like the past month.
    Thanks again!

  20. alikh
    Sep 24th, 2009

    this is awesome.

  21. Yeoj
    Oct 11th, 2009

    this is cool! im trying to look for this code till i get here but the one im looking for is how just the image, i want to save it just the image, i really like to add features for the next of my games like this http://joeyddizon.deviantart.com/art/Gorgeous-Angel-Dress-up-138992946 could you help me about it? thanks! in advance :D

  22. joshua
    Nov 3rd, 2009

    hi is it possible to export the captured image into a file..like a jpeg or png file?

    thanks!

  23. Márcio Ferreira Rosa
    Jan 31st, 2010

    Excelent tutorial!
    Could you tell me if I could to send to server this image?

  24. Gerardo Ulises
    Feb 28th, 2010

    Great work!!

    That´s just what i wanted

  25. Abdullah
    Mar 29th, 2010

    Hi guys
    I have Macromedia Flash MX

    how can I use

    import flash.display.Bitmap;
    import flash.display.BitmapData;

    in Macromedia Flash MX

  26. JJ
    Jul 21st, 2010

    Nice and simple. Just what I was looking for. Thank you.

  27. Como capturar a imagem da WebCam, e salvar no disco | flex.etc.br – Livros, tutoriais, exemplos sobre Adobe Flex
    Aug 23rd, 2010

    [...] exemplo rápido foi retirado dos sites: RiaCodes e Luiz Henrique [...]

  28. Torvics
    Aug 24th, 2010

    Nice tutorials…

    Do you have the as2 version of this?

    Thanks.

  29. Yenky
    Sep 4th, 2010

    wow.. its really nice script…

    Thanks a lot :)

  30. Captures images from the webcam and save them to the desktop « Catatan Kuliahku di TI UIN Jakarta
    Sep 17th, 2010

    [...] tutorial is a sequel of our tutorial called “Capture image from the webcam”. Here we’ll add a new functionnality that let the user save the captured photo to his desktop. [...]

  31. kit
    Sep 23rd, 2010

    hi can anyone help ?
    i want fileRef.save() not Opens a dialog box save a file to the local filesystem.
    i just want save automatically in local drive .
    any other class in FileReference().

    thanks a lot

  32. Yo
    Nov 12th, 2010

    Hey, how would I add any effects to the images?
    Thanks

  33. alul
    Jan 19th, 2011

    greatt!! i have try it, and the code work! thx……

  34. Ryan
    Mar 6th, 2011

    Very nice example! I was able to adapt it to Flex. Thank you!

  35. Jasmine
    Mar 22nd, 2011

    Hi, im a beginner, so maybe this would be a stupid questions.

    ‘If you right click on the .swf, go to settings, then click on the camera, then drop down the menu and choose ‘USB Class Video’ rather then the default ‘DV Video’,’

    where can i find the .swf to click on???

  36. Jasmine
    Mar 22nd, 2011

    sorry i figure out the way to change it to the USB Class Video
    but it doesn’t work for my macbook pro

    There are two errors
    which is
    scene1, Layer ‘actions’, Frame 1, Line 18&19 1120: Access of undefined property capture_mc

    after this, i highlight the button -> Modify -> Convert to symbol -> Name the button as Capture_mc ->
    Type: Movie clip

    it still doesnt work
    any ideas why?

  37. Bhavesh Savla
    Mar 24th, 2011

    Short, Simple & Sweet.
    Thanks for sharing.

  38. Fathah
    Apr 13th, 2011

    @SUDEEP GURUNG: to save the picture, you can combine the code with another tutorial for printing
    http://www.riacodes.com/flash/how-to-print-with-as3/

  39. Pavan
    Jun 8th, 2011

    That’s GREAT!!!!!!!Working Fine!!

  40. venkatesh.A
    Aug 5th, 2011

    Hey guys,
    i imported all my classes,but im getting runtime error…..pls clear my doubt…

    import flash.media.Camera;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.media.Video;

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at videoCamera_fla::MainTimeline/videoCamera_fla::frame1()

  41. Shashi
    Sep 20th, 2011

    Excellent Stuff, but where the captured images are saving. I did not find the captured images anywhere on my machine, i even did not find the ‘save captured image’ option. please do the needful.

    thanks in Advance
    Shashi

  42. Sunny
    Oct 14th, 2011

    Hey friend
    I seriously need u’r help
    can u post a Webcam recording and saving it in the desktop code
    please it would be a really big help friend
    or mail me at sunnythangaraj@aol.com

    I really want your help in this
    please help me out in this
    hope u would help me out ……….. Thank You

  43. samuel
    Oct 20th, 2011

    Great stuff …
    Thanks

  44. huseyin
    Nov 12th, 2011

    thank you very much this is exactly what i need, actually smplier what i expect

  45. boby
    Nov 15th, 2011

    after captured images, how to saving in database mysql,.thx

  46. Resources for Developers | Like for Israel
    Nov 29th, 2011

    [...] 2. Capturing web camera (web flash): jpegcam – http://code.google.com/p/jpegcam/ tutorial – http://www.riacodes.com/flash/capture-images-from-the-webcam/ [...]

  47. mj
    Nov 30th, 2011

    Hi does anyone know how to capture video and reinsert it into another video… bit like http://blistjarna.se/ cheers

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website