前段時間做了一個類似的應用,現將源代碼公佈。(請自行配置php和gd2庫)
Flash as3:(放到幀上,可自行修改移至相關class)
- function clickHandler(e:MouseEvent):void
- {
- var bitmapdata:BitmapData = new BitmapData(video.width, video.height);
- bitmapdata.draw(video);
- var jpgEncoder:JPGEncoder = new JPGEncoder(80);//圖片質量
- var jpgStream:ByteArray = jpgEncoder.encode(bitmapdata);
- var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
- var jpgURLRequest:URLRequest = new URLRequest ("http://localhost/jpg_encoder_download.php?name=camera.jpg"); //此web url請自行修改
- jpgURLRequest.requestHeaders.push(header);
- jpgURLRequest.method = URLRequestMethod.POST;
- jpgURLRequest.data = jpgStream;
- navigateToURL(jpgURLRequest, "_blank");
- }
- var video:Video = new Video;
- addChild(video);
- video.attachCamera(Camera.getCamera());
- var btn:Sprite = new Sprite;
- btn.graphics.beginFill(0,1);
- btn.graphics.drawRoundRect(0,0,100,25,10);
- btn.x = 110;
- btn.y = 250;
- btn.buttonMode = true;
- btn.addEventListener(MouseEvent.CLICK, clickHandler);
- addChild(btn);
兩個類文件BitString.as, JPGEncoder.as,下載後注意大小寫,此wp不知道咋整的,把偶上傳時的大寫都改成小寫了。
php:
- <?php
- if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
- // get bytearray
- $im = $GLOBALS["HTTP_RAW_POST_DATA"];
- // add headers for download dialog-box
- header('Content-Type: image/jpeg');
- header("Content-Disposition: attachment; filename=".$_GET['name']);
- echo $im;
- } else echo 'An error occured.';
- ?>