UPDATE: HTML5 Webcam Photobooth Web App
Two days ago I made a little Flash app that will allow anyone to take a picture with a webcam connected to a computer. Everything worked fine in AS3. It was only when I got to the PHP part of the project when I felt like I should ask for help. And I did, I asked a friend or mine (Mihai Bojin) who in a few minutes explained to me exactly what I needed to know in order to get this project working as it should (a big part of the PHP code is made by him).
The project is now open source and you can find the code and the source files underneath ๐
This open source project is release under the MIT license.
You can download the source files from here:
Photo Booth (52.8 KiB, 38,349 hits)
Here you can see the working application:
vamapaull.com/photobooth
Here you can see the ActionScript 3.0 code:
import flash.display.Bitmap; import flash.display.BitmapData; import com.adobe.images.JPGEncoder; var snd:Sound = new camerasound(); //new sound instance for the "capture" button click var bandwidth:int = 0; // Maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second. var quality:int = 100; // This value is 0-100 with 1 being the lowest quality. var cam:Camera = Camera.getCamera(); cam.setQuality(bandwidth, quality); cam.setMode(320,240,30,false); // setMode(videoWidth, videoHeight, video fps, favor area) var video:Video = new Video(); 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 { snd.play(); bitmapData.draw(video); save_mc.buttonMode = true; save_mc.addEventListener(MouseEvent.CLICK, onSaveJPG); save_mc.alpha = 1; } save_mc.alpha = .5; function onSaveJPG(e:Event):void{ var myEncoder:JPGEncoder = new JPGEncoder(100); var byteArray:ByteArray = myEncoder.encode(bitmapData); var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream"); var saveJPG:URLRequest = new URLRequest("save.php"); saveJPG.requestHeaders.push(header); saveJPG.method = URLRequestMethod.POST; saveJPG.data = byteArray; var urlLoader:URLLoader = new URLLoader(); urlLoader.addEventListener(Event.COMPLETE, sendComplete); urlLoader.load(saveJPG); function sendComplete(event:Event):void{ warn.visible = true; addChild(warn); warn.addEventListener(MouseEvent.MOUSE_DOWN, warnDown); warn.buttonMode = true; } } function warnDown(e:MouseEvent):void{ navigateToURL(new URLRequest("images/"), "_blank"); warn.visible = false; } warn.visible = false; |
Here you have the php code:
<?php if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){ $jpg = $GLOBALS["HTTP_RAW_POST_DATA"]; $img = $_GET["img"]; $filename = "images/filename_". mktime(). ".jpg"; file_put_contents($filename, $jpg); } else{ echo "Encoded JPEG information not received."; } ?> |
EDIT 16/03/2010:
From this little experiment a new project called UserBooth came to life ๐
EDIT 04/12/2011:
I just got the approval notice from ActiveDen for a much better version of this PhotoBooth app. This new version includes lot of options to customize it through XML and it includes both ASP and PHP files for the server-side part of the code. If you have any suggestions to improve it, please contact me though my ActiveDen profile
Thank you very much for a brilliant piece of very clean coding. Works a treat to boot. Very well done.
Thank you very much for your hard work, i’m looking for script that can access webcam from web based application (PHP), i already downloaded your photo boot, but i can’t open the take_picture.fla, i’m using Flash MX
Need reply
Thank you
Com on Sinantrya, you didn’t expect ActionScript 3 to work in Flash MX?!
a suggestion: you shouldn’t write the handler script name (save.php) in the flash source file , because some project will use mvc framework ^-^(like me) ,i can’t create such file in my www root , it should be a url: user/saveAvatar or may call it route path
Thank you very much for this, it’s terrific.
I’m trying to figure out how to add comments to the photo. I’d like to add text input from the swf to the iptc/exif info in the photo before it gets saved, then pull out the comments with a gallery script. Does that sound reasonable? I’d really appreciate any suggestions or advice, as this is a “learn as I go” project.
Thanks again!
Weird thing is, the demo @ userboot works well with Firefox, while the source files and precompiled swf you can download from here crash the flash plugin (?)
Works ok in IE tho.
mh nevermind, it’s something I changed in the fla, since the vanilla SWF works in firefox as well.
…nevermind again, I was trying to load the flash BOTH in IE and FF, and that crashed the browser where the flash had been opened later ๐
Anyway, nice script, I tweaked it a bit increasing the screenshot size by keeping the preview at 320×240, and adding a sort of ajax loader.
@Sinantrya – try to use Flash CS4/CS5 in opening the .FLA file.
@Vamapaull – hi! ๐ thanks for releasing such a wonderful script/program. btw, I use this on my local webserver(windows 7) and it works perfectly fine, but there is a problem when viewing it on the mac, the photobooth on mac it just showing a white window. There is a capture and save button but no camera window. Please help in fixing the problem. Thanks! ๐
@Brian – I really don’t use a Mac or own a Mac. I don’t know if I can help you with this problem.
What browser are you using? And where exactly is the project that you are testing?
Doesn’t work !
It doesn’t upload the file on the web site even with 777 folder mode.
I have tried many things but nothing work. Any idea ?
@vamapaull – my friend used safari on his mac. On windows, I successfully test it on IE, Firefox, Safari, Opera and Chrome. I have a test site on http://photobooth.biseroo.com/
Thanks for the help ^^
The code seems very nice ! I’m an experienced PHP & javascript programmer, but I have no experience at all with flash. So I downloaded the flash trial from adobe, and manager to open the *.fla file, but I can not figure out how to edit the action script ! Where are they
can someone please advise me how I can find these scripts ? All I want is to change the naming scheme of the stored images… I’ve tried right-clicking on the buttons, but then flash simply says “In actionscript 3.0, code cannot be placed directly on objects. Please select a frame or use the Code Snippets panel to apply code to the current selection in the stage”.
so I went to the timeline, clicked on the first frame (making a vertical red line) and then right-clicked in each of the four “layers” (or whatever they are called), but I can not find any script code ??
where is the code hidden ?
Just click on the ActionScript layer (the first layer if I remember correctly) and then hit F9
Nice module…Thanks for share.. ๐
I am trying to add it my green screen software , a new version
but I am looking to get the hihg resloution as well, I am developing with visual basic , do you think I can do that?
I don’t understand why you didn’t put as a parameter the post url instead to fixed with a save.php script. If we were able to setup the URI to call when save the photo then, we’ll be able to write an own script in differents plataforms.
Regards.
This is an excellent piece of work. Thanks.
vamapaull,
I LOVE THIS! Thank you for sharing your Photo Booth Flash piece. It’s extremely simple and I can put it into use almost instantly. I’m very impressed with the thoughtfulness put into the simplicity and execution.
Thank you, thank you, thank you,
Chris
hi can you show you how to access flash data in asp.net
Hi, help me
File does not exist , why ?
Thanks
giovanni: Problem fixed.
Hello. How can I send some variables from flash to php with the picture? thanks
@Felipe You can use & after “theId” variable.
Example: “save.php?id=”+theId+”&newVariable=”+variableValue
Thanks for the reply:
The line would be like this?
var saveJPG:URLRequest = new URLRequest(“save.php?id=”+theId+”&email=”+”email@gmail.com”); ?
I have to send an email address with the image. How can I do that?
Didnยดt make it to work
@Felipe Maybe you can save the image on your server and send an email width the link to that image. You can make it work but you need to have a bit of experience with php and flash.
I loved it very much and great work,
Here at present captuered images are saving in to “#http://vamapaull.com/photobooth/images/#” path. How to change that gallery path to my own path. please do the needful.
Thanks in Advance
Shashi
@Shashi You need Flash IDE for that. Just open the FLA file, edit and compile a new SWF.
Hi,
is it possible to set cam an pic size to 160 x 120 pixel ?
Thanks for the quick reply.
I am new to Flash. Where can i get that Flash IDE and how to use that in this application. As you said I have opened the FLA, but what changes i have to make to change the existing gallery path to my own path.
Thanks again
@Shashi If you opened the FLA file, then I believe you already have the Flash IDE (Flash CS3, CS4, CS5 etc.).
You have to first select the text and then change the URL from: Properties > Options > Link
@Igor I believe it is possible. You need to do some modifications in the ActionScript code.
ok, I changed
Video.height = 160;
Video.width = 120;
could change the image size of the camera, but not the bitmap, yet could not change the bitmap
see…
http://www.datatechonline.com.br/photo_booth/controle_acesso.php
@Igor I work as a freelancer, if you need custom work you can hire me.
Thanks for the update.
Sorry for the disturbing you again and again..
I have tried it..My requirement is to save the captured image in to my local machine desktop. But i couldn’t make it.
When i click on the “save image” button, it is showing some path to see the images. But i did not find any captured/saved images there.
If you don’t mind Could you please guide me…
More Thanks
Shashi
@Shashi I hope you understand that you need a server with PHP support in order to save the image.
I have been having trouble getting the photo to save on my MSFT hosting service so I tried on a linux/php box and it worked fine. I am using Coldfusion and must be on IIS. I am thinking that there is an issue with the image folder being writeable. possibly you have had this come up and know what is needed to do on IIS to be able to save photo to image folder.
@Ainvar Make sure that php is working fine on that server.
This helps me a lot <3
thanks
I’m passing a values into flashvars by adding these lines to photo_booth/index.html (id has been appended to the url)
var id = ;
mainswf.addParam(“flashvars”, “photo_id=”+id);
Anyone know if/how I can get this value in photo_booth/save.php
Thanks!
@Flash_Illiterate:
Sorry –
var id = ;
mainswf.addParam("flashvars", photor_id="+id);
Very nice piece of software. What if we use System.showSettings() to get full setting access dialog wherein we can select different cameras / check the remember me option to avoid access grant on every refresh / reload. I tried to check the access of webcam using camobj.muted, and then using onStatus property of camera object to show settings in absence of access, but somehow failed to do so. It would be nice if that can be incorporated so that users are least bothered about granting access everytime this is loaded.
@Ehsan You should try: Security.showSettings(SecurityPanel.CAMERA);
@vamapaull: Can’t this code be incorporated in photo_booth so that it becomes more usable.
Wow this is a great script, thanks!
Is it possible to change this so you can record movies and store them ?
Some ideas about it would be welcome.
Cheers,
Matt
@Matt To do that you’ll need a media server and add/remove big parts of this code. The simple answer is no, you can’t record videos.
@vamapaull I have a red5 server installed which works very OK, so I should be capable of doing so.
The question I have is how to temporary store the movie before you save it… that is kinda tricky it seems.
BTW, can it be that the PHP code does not work ? I need to test it on a normal webserver but on my red5 server which supports PHP it does not save or show the index.php in the image-folder.
Any help would be appriciated ๐
@Matt I don’t think it’s just that easy, having a media server dose not solve everything. You need to have a better understanding of ActionScript 3 in order to transform this code into a video player/recorder.
I have no experience with red5 and I don’t think I can help you out in your journey, making a webcam video recorder.
Recording a movie is not that difficult, it’s kinda simple using:
Such an Tut: http://imyousuf-tech.blogs.smartitengineering.com/2007/07/video-recording-using-flash.html#comment-form
And this kind of code is what you actually get:
var nc:NetConnection = new NetConnection();
nc.connect(fms_uri);
var ns:NetStream = new NetStream(nc);
var mic:Microphone = Microphone.get();
var cam:Camera = Camera.get();
ns.attachAudio(mic);
ns.attachVideo(cam);
ns.publish(filename, "record");
I think it’s doable to replace the code so you actually record.