[AS3]利用as3+flash通过摄像头或本地上传图片到服务器
[AS3]通过摄像头或本地上传图片到服务器
[AS3]通过摄像头或本地上传图片到服务器
- package
- {
- import flash.display.Sprite;
- import flash.media.Camera;
- import flash.media.Video;
- import flash.events.Event;
- import flash.events.StatusEvent;
- import flash.events.ProgressEvent;
- import flash.events.IOErrorEvent;
- import flash.events.SecurityErrorEvent;
- import flash.events.MouseEvent;
- import flash.display.BitmapData;
- import flash.display.Bitmap;
- import flash.geom.Matrix;
- import mx.graphics.codec.JPEGEncoder;
- import flash.utils.ByteArray;
- import flash.net.LocalConnection;
- import flash.net.URLRequestHeader;
- import flash.net.URLRequest;
- import flash.net.URLLoader;
- import flash.net.URLRequestMethod;
- import flash.net.URLVariables;
- import flash.net.FileFilter;
- import flash.net.FileReference;
- import flash.net.FileReferenceList;
- [SWF(width = "720",height = "450",backgroundColor = "#445566")]
- public class Main extends Sprite
- {
- private var cam:Camera;
- private var vid:Video;
- private var upMc;
- private var bitmap:Bitmap;
- private var bitmapData:BitmapData;
- private var mcArr = [];
- private var frl:FileReferenceList;
- private var uploadURL;
- public function Main():void
- {
- Init();
- frl=new FileReferenceList();
- frl.addEventListener(Event.SELECT,selectHandler);
- frl.addEventListener(Event.CANCEL,cancelHandler);
- }
- private function Init()
- {
- getCamer();
- }
- private function getCamer()
- {
- cam = Camera.getCamera();
- cam.addEventListener(StatusEvent.STATUS,statusHandler);
- cam.setMode(384,216,12);
- cam.setQuality(0,100);
- vid = new Video ;
- vid.width = 384;
- vid.height = 216;
- vid.attachCamera(cam);
- addChild(vid);
- vid.x = stage.stageWidth / 2 - vid.width / 2;
- vid.y = stage.stageHeight / 2 - vid.height / 2;
- bitmap = new Bitmap();
- bitmapData = new BitmapData(vid.width,vid.height,true,0);
- }
- private function statusHandler(e:StatusEvent)
- {
- switch (e.code)
- {
- case "Camera.Unmuted" :
- uploadFun(true);
- break;
- case "Camera.Muted" :
- uploadFun(false);
- break;
- default :
- break;
- }
- }
- private function uploadFun(right:Boolean)
- {
- upMc=new UpMc();
- addChild(upMc);
- visibleBtn();
- if (right)
- {
- upMc.x = vid.x + vid.width - upMc.width;
- upMc.y = vid.y + 10 + vid.height;
- pleaseScreen();
- }
- else
- {
- upMc.x = stage.stageHeight / 2 - upMc.width / 2;
- upMc.y = stage.stageHeight / 2 + upMc.height / 2;
- pleaseLoad();
- }
- }
- private function pleaseLoad()
- {
- upMc.browseBtn.visible = true;
- upMc.browseBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- mcArr.push(upMc.browseBtn);
- }
- private function upDataImg(type:String)
- {
- switch (type)
- {
- case "1" :
- var jpgSource:BitmapData = new BitmapData(bitmap.width,bitmap.height,true,0);
- jpgSource.draw(bitmap);
- var jpgEncoder:JPEGEncoder = new JPEGEncoder(300);
- var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
- var domain:String = "localhost";
- var LocalConn:LocalConnection=new LocalConnection();
- domain = LocalConn.domain;
- var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
- var jpgURLRequest:URLRequest=new URLRequest("http://"+domain+"/vini/up/jpg_upload.php?name="+ "gogo");
- jpgURLRequest.requestHeaders.push(header);
- jpgURLRequest.method = URLRequestMethod.POST;
- jpgURLRequest.data = jpgStream;
- var myVariables:URLVariables=new URLVariables();//这个用不到了。。
- var loader:URLLoader=new URLLoader();
- loader.load(jpgURLRequest);
- loader.addEventListener(Event.COMPLETE,uploadCompleteHandler);
- break;
- default :
- break;
- }
- }
- private function uploadCompleteHandler(e:Event)
- {
- trace("图片上传成功了,这里要给数据库添加记录吧!");
- }
- public function clickHandler(e:MouseEvent)
- {
- switch (e.target.name)
- {
- case "screenBtn" :
- removeEvent();
- bitmapData.draw(vid);
- bitmap.bitmapData = bitmapData;
- bitmap.width = vid.width;
- bitmap.height = vid.height;
- removeChild(vid);
- addChild(bitmap);
- bitmap.x = stage.stageWidth / 2 - bitmap.width / 2;
- bitmap.y = stage.stageHeight / 2 - bitmap.height / 2;
- upMc.mesTxt.text = "请上传图片或重新截图";
- upMc.uploadBtn.visible = true;
- upMc.uploadBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- mcArr.push(upMc.uploadBtn);
- upMc.retakeBtn.visible = true;
- upMc.retakeBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- mcArr.push(upMc.retakeBtn);
- upMcupMc.retakeBtn.x = upMc.uploadBtn.x + upMc.retakeBtn.width + 5;
- break;
- case "uploadBtn" :
- removeEvent();
- upMc.mesTxt.text = "图片开始上传中";
- upDataImg("1");
- break;
- case "retakeBtn" :
- removeEvent();
- removeChild(bitmap);
- addChild(vid);
- pleaseScreen();
- break;
- case "browseBtn" :
- //removeEvent();
- uploadhandler("图片档案(*.jpg;*.gif;*.png)|*.jpg;*.gif;*.png");
- break;
- }
- }
- public function uploadhandler(filter_str,type:String=''):void
- {
- var up_name = get_pic_name(5)+'.'+ getFileExt(filter_str);
- trace(up_name);
- uploadURL = new URLRequest('http://localhost/vini/up/upload.php?tg_name='+up_name+'&filepath=loadImg/');
- //uploadURL = new URLRequest('http://localhost/zh_cn/ebook/upload.php?tg_name='+up_name+'&filepath=http://www.cnblogs.com/vini/up/loadImg/');
- var tmp_arr:Array = filter_str.split('|');
- trace(tmp_arr);
- var filter:FileFilter = new FileFilter(tmp_arr[0],tmp_arr[1]);
- frl.browse([filter]);
- }
- private function selectHandler(e:Event)
- {
- trace("可以上传么");
- var fileReferenceList:FileReferenceList = e.target as FileReferenceList;
- var fileList:Array = fileReferenceList.fileList;
- for (var i:uint = 0; i < 1; i++)
- {
- // org is : i < fileList.length : now just upload one
- //control.test_txt.appendText("fileList["+i+"]:"+fileList[i].name + "\n");
- addPendingFile(fileList[i]);
- }
- }
- private function addPendingFile(file:FileReference):void
- {
- //control.load_file_bar.visible = true; //显示加载进度
- file.addEventListener(Event.OPEN, openHandler);
- file.addEventListener(Event.COMPLETE, completeHandler);
- file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
- file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
- file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
- file.upload(uploadURL);
- trace("开始上传");
- }
- private function openHandler(event:Event):void
- {
- var file:FileReference = FileReference(event.target);
- trace("开始了么");
- //control.load_file_bar.ready(file.name);
- }
- private function completeHandler(event:Event):void
- {
- trace("aaa"+event.target.data);
- return;
- var file:FileReference = FileReference(event.target);
- trace(file.name);
- }
- private function progressHandler(event:ProgressEvent):void
- {
- trace("过程过程");
- var file:FileReference = FileReference(event.target);
- if (event.bytesLoaded / event.bytesTotal < 1)
- {
- //control.load_file_bar.ready(file.name);
- }
- else
- {
- //control.load_file_bar.updateinfo(file.name, event.bytesLoaded, event.bytesTotal);
- }
- }
- private function ioErrorHandler(event:Event):void
- {
- trace("错误一");
- //control.test_txt.appendText("ioErrorHandler \n");
- }
- private function securityErrorHandler(event:Event):void
- {
- trace("错误二");
- //control.test_txt.appendText("securityErrorHandler \n");
- }
- private function cancelHandler(e:Event)
- {
- }
- private function pleaseScreen()
- {
- upMc.mesTxt.text = "请截图";
- upMc.screenBtn.visible = true;
- upMc.screenBtn.addEventListener(MouseEvent.CLICK,clickHandler);
- mcArr.push(upMc.screenBtn);
- }
- private function removeEvent()
- {
- var len:int = mcArr.length;
- for (var i=0; i<len; i++)
- {
- mcArr[i].visible = false;
- mcArr[i].removeEventListener(MouseEvent.CLICK,clickHandler);
- }
- mcArr = [];
- }
- private function visibleBtn()
- {
- upMc.retakeBtn.visible = false;
- upMc.screenBtn.visible = false;
- upMc.browseBtn.visible = false;
- upMc.uploadBtn.visible = false;
- }
- private function get_pic_name(no=null)
- {
- var nowtime:Date = new Date();
- var str:String = timeformate(nowtime.getFullYear());
- strstr = str.concat(timeformate(nowtime.getMonth()+1),timeformate(nowtime.getDate())
- ,timeformate(nowtime.getHours()),timeformate(nowtime.getMinutes()),timeformate(nowtime.getSeconds()));
- if (no==null)
- {
- return str;
- }
- for (var i:uint=0; i<no; i++)
- {
- var ch:Number = randRange(0,51);
- if (ch>=26)
- {
- str += String.fromCharCode(97+ch-26);
- }
- else
- {
- str += String.fromCharCode(65+ch);
- }
- }
- return str;
- }
- private function getFileExt(file_name)
- {
- var file_array = file_name.split(".");
- trace("vvv:"+file_array[file_array.length - 1])
- return file_array[file_array.length - 1];
- }
- private function timeformate(num):String
- {
- if (num < 10)
- {
- return ("0" + num);
- }
- return num;
- }
- private function randRange(min:Number, max:Number):Number
- {
- var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
- return randomNum;
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...