In many websites, for a most immersive surf you take a shot of your face. Often, it is simply a red square. You move your head in, and you take the picture; it is really easy to do. However if you want to take only the head with a circle for exemple it’s a bit more difficult.
This is a script to do that (on the website, the swf is opened with a lightbox and the picture is saved) :
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | package { import com.adobe.images.PNGEncoder; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.external.ExternalInterface; import flash.geom.Point; import flash.geom.Rectangle; import flash.media.Camera; import flash.media.Video; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestHeader; import flash.net.URLRequestMethod; import flash.utils.ByteArray; /** * @author Aymeric */ public class CaptureTete extends Sprite { public var button:Sprite; private var _cam:Camera; private var _video:Video; private var _bitmapData:BitmapData; private var _bitmap:Bitmap; private var _conteneur:Sprite; private var _drawPanel:Sprite; public function CaptureTete() { _cam = Camera.getCamera(); _video = new Video(320, 240); _bitmapData = new BitmapData(_video.width, _video.height); _bitmap = new Bitmap(_bitmapData); _video.attachCamera(_cam); _video.x = _bitmap.x = 20; _video.y = _bitmap.y = 40; addChild(_video); _conteneur = new Sprite(); addChild(_conteneur); _drawPanel = new Sprite(); _drawPanel.graphics.clear(); _drawPanel.graphics.lineStyle(2, 0xeea41e); _drawPanel.graphics.beginFill(0xDEFACE, 0); _drawPanel.graphics.drawCircle(80, 80, 80); _drawPanel.graphics.endFill(); _conteneur.addChild(_drawPanel); _drawPanel.x = 100; _drawPanel.y = 100; button.buttonMode = true; button.addEventListener(MouseEvent.CLICK, _captureImage); } private function _captureImage(e:MouseEvent):void { button.removeEventListener(MouseEvent.CLICK, _captureImage); _bitmapData.draw(_video); _conteneur.addChild(_bitmap); _conteneur.setChildIndex(_drawPanel, _conteneur.numChildren - 1); _cam = null; removeChild(_video); _video = null; _bitmap.mask = _drawPanel; _bitmapData = new BitmapData(320, 240,true,0x000000); _bitmapData.draw(_conteneur); var bitmap:Bitmap = new Bitmap(_bitmapData); _conteneur.addChild(bitmap); var bmd:BitmapData = new BitmapData(160, 160); var rect:Rectangle = new Rectangle(100, 100, 160, 160); bmd.copyPixels(_bitmapData, rect, new Point(0, 0)); var pngStream:ByteArray = PNGEncoder.encode(bmd); var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream"); var savePNG:URLRequest = new URLRequest("save.php"); savePNG.requestHeaders.push(header); savePNG.method = URLRequestMethod.POST; savePNG.data = pngStream; var urlLoader:URLLoader = new URLLoader(); urlLoader.load(savePNG); urlLoader.addEventListener(Event.COMPLETE, _loaded); } private function _loaded(evt:Event):void { evt.target.removeEventListener(Event.COMPLETE, _loaded); ExternalInterface.call("closeLightbox"); } } } |
You can found the external library made by Mike Chambers : as3corelib.
The Php script to save it :
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) { $png = $GLOBALS["HTTP_RAW_POST_DATA"]; $img = $_GET["img"]; $filename = "images/img_". mktime(). ".png"; file_put_contents($filename, $png); } else { echo "Encoded PNG information not received."; } ?> |
To go further, we can draw our own circle or an other shape. Using a Php Session for the filename is cool too !
Exemple (don’t worry, I do not save ! 😉 )