·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]利用as3+flash通过摄像头或本地上传图片到服务器

时间:2014-12-01 16:21酷播
[AS3]通过摄像头或本地上传图片到服务器

[AS3]通过摄像头或本地上传图片到服务器

  1. package  
  2.     import flash.display.Sprite; 
  3.     import flash.media.Camera; 
  4.     import flash.media.Video; 
  5.     import flash.events.Event; 
  6.     import flash.events.StatusEvent; 
  7.     import flash.events.ProgressEvent; 
  8.     import flash.events.IOErrorEvent; 
  9.     import flash.events.SecurityErrorEvent; 
  10.     import flash.events.MouseEvent; 
  11.     import flash.display.BitmapData; 
  12.     import flash.display.Bitmap; 
  13.     import flash.geom.Matrix; 
  14.     import mx.graphics.codec.JPEGEncoder; 
  15.     import flash.utils.ByteArray; 
  16.     import flash.net.LocalConnection; 
  17.     import flash.net.URLRequestHeader; 
  18.     import flash.net.URLRequest; 
  19.     import flash.net.URLLoader; 
  20.     import flash.net.URLRequestMethod; 
  21.     import flash.net.URLVariables; 
  22.     import flash.net.FileFilter; 
  23.     import flash.net.FileReference; 
  24.     import flash.net.FileReferenceList; 
  25.  
  26.  
  27.     [SWF(width = "720",height = "450",backgroundColor = "#445566")] 
  28.     public class Main extends Sprite 
  29.     { 
  30.         private var cam:Camera; 
  31.         private var vid:Video; 
  32.         private var upMc; 
  33.         private var bitmap:Bitmap; 
  34.         private var bitmapData:BitmapData; 
  35.         private var mcArr = []; 
  36.  
  37.         private var frl:FileReferenceList; 
  38.         private var uploadURL; 
  39.  
  40.         public function Main():void 
  41.         { 
  42.             Init(); 
  43.             frl=new FileReferenceList(); 
  44.             frl.addEventListener(Event.SELECT,selectHandler); 
  45.             frl.addEventListener(Event.CANCEL,cancelHandler); 
  46.         } 
  47.         private function Init() 
  48.         { 
  49.             getCamer(); 
  50.         } 
  51.         private function getCamer() 
  52.         { 
  53.             cam = Camera.getCamera(); 
  54.             cam.addEventListener(StatusEvent.STATUS,statusHandler); 
  55.             cam.setMode(384,216,12); 
  56.             cam.setQuality(0,100); 
  57.             vid = new Video  ; 
  58.             vid.width = 384
  59.             vid.height = 216
  60.             vid.attachCamera(cam); 
  61.             addChild(vid); 
  62.             vid.x = stage.stageWidth / 2 - vid.width / 2; 
  63.             vid.y = stage.stageHeight / 2 - vid.height / 2; 
  64.  
  65.             bitmap = new Bitmap(); 
  66.             bitmapData = new BitmapData(vid.width,vid.height,true,0); 
  67.         } 
  68.         private function statusHandler(e:StatusEvent) 
  69.         { 
  70.             switch (e.code) 
  71.             { 
  72.                 case "Camera.Unmuted" : 
  73.                     uploadFun(true); 
  74.                     break; 
  75.                 case "Camera.Muted" : 
  76.                     uploadFun(false); 
  77.                     break; 
  78.                 default : 
  79.                     break; 
  80.             } 
  81.         } 
  82.  
  83.         private function uploadFun(right:Boolean) 
  84.         { 
  85.             upMc=new UpMc(); 
  86.             addChild(upMc); 
  87.             visibleBtn(); 
  88.             if (right) 
  89.             { 
  90.                 upMc.x = vid.x + vid.width - upMc.width; 
  91.                 upMc.y = vid.y + 10 + vid.height; 
  92.                 pleaseScreen(); 
  93.             } 
  94.             else 
  95.             { 
  96.                 upMc.x = stage.stageHeight / 2 - upMc.width / 2; 
  97.                 upMc.y = stage.stageHeight / 2 + upMc.height / 2; 
  98.                 pleaseLoad(); 
  99.             } 
  100.         } 
  101.  
  102.         private function pleaseLoad() 
  103.         { 
  104.             upMc.browseBtn.visible = true
  105.             upMc.browseBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  106.             mcArr.push(upMc.browseBtn); 
  107.         } 
  108.         private function upDataImg(type:String) 
  109.         { 
  110.             switch (type) 
  111.             { 
  112.                 case "1" : 
  113.                     var jpgSource:BitmapData = new BitmapData(bitmap.width,bitmap.height,true,0); 
  114.                     jpgSource.draw(bitmap); 
  115.                     var jpgEncoder:JPEGEncoder = new JPEGEncoder(300); 
  116.                     var jpgStream:ByteArray = jpgEncoder.encode(jpgSource); 
  117.  
  118.                     var domain:String = "localhost"
  119.                     var LocalConn:LocalConnection=new LocalConnection(); 
  120.                     domain = LocalConn.domain; 
  121.  
  122.                     var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream"); 
  123.  
  124.                     var jpgURLRequest:URLRequest=new URLRequest("http://"+domain+"/vini/up/jpg_upload.php?name="+ "gogo"); 
  125.                     jpgURLRequest.requestHeaders.push(header); 
  126.                     jpgURLRequest.method = URLRequestMethod.POST; 
  127.                     jpgURLRequest.data = jpgStream
  128.  
  129.                     var myVariables:URLVariables=new URLVariables();//这个用不到了。。 
  130.  
  131.                     var loader:URLLoader=new URLLoader(); 
  132.                     loader.load(jpgURLRequest); 
  133.                     loader.addEventListener(Event.COMPLETE,uploadCompleteHandler); 
  134.                     break; 
  135.                 default : 
  136.                     break; 
  137.             } 
  138.         } 
  139.  
  140.         private function uploadCompleteHandler(e:Event) 
  141.         { 
  142.             trace("图片上传成功了,这里要给数据库添加记录吧!"); 
  143.         } 
  144.  
  145.         public function clickHandler(e:MouseEvent) 
  146.         { 
  147.  
  148.             switch (e.target.name) 
  149.             { 
  150.                 case "screenBtn" : 
  151.                     removeEvent(); 
  152.                     bitmapData.draw(vid); 
  153.                     bitmap.bitmapData = bitmapData; 
  154.                     bitmap.width = vid.width; 
  155.                     bitmap.height = vid.height; 
  156.                     removeChild(vid); 
  157.                     addChild(bitmap); 
  158.                     bitmap.x = stage.stageWidth / 2 - bitmap.width / 2; 
  159.                     bitmap.y = stage.stageHeight / 2 - bitmap.height / 2; 
  160.                     upMc.mesTxt.text = "请上传图片或重新截图"
  161.  
  162.                     upMc.uploadBtn.visible = true
  163.                     upMc.uploadBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  164.                     mcArr.push(upMc.uploadBtn); 
  165.  
  166.                     upMc.retakeBtn.visible = true
  167.                     upMc.retakeBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  168.                     mcArr.push(upMc.retakeBtn); 
  169.  
  170.                     upMcupMc.retakeBtn.x = upMc.uploadBtn.x + upMc.retakeBtn.width + 5; 
  171.                     break; 
  172.                 case "uploadBtn" : 
  173.                     removeEvent(); 
  174.                     upMc.mesTxt.text = "图片开始上传中"
  175.                     upDataImg("1"); 
  176.                     break; 
  177.                 case "retakeBtn" : 
  178.                     removeEvent(); 
  179.                     removeChild(bitmap); 
  180.                     addChild(vid); 
  181.                     pleaseScreen(); 
  182.                     break; 
  183.                 case "browseBtn" : 
  184.                     //removeEvent(); 
  185.                     uploadhandler("图片档案(*.jpg;*.gif;*.png)|*.jpg;*.gif;*.png"); 
  186.                     break; 
  187.             } 
  188.         } 
  189.  
  190.  
  191.         public function uploadhandler(filter_str,type:String=''):void 
  192.         { 
  193.  
  194.  
  195.             var up_name = get_pic_name(5)+'.'+ getFileExt(filter_str); 
  196.             trace(up_name); 
  197.             uploadURL = new URLRequest('http://localhost/vini/up/upload.php?tg_name='+up_name+'&filepath=loadImg/'); 
  198.             //uploadURL = new URLRequest('http://localhost/zh_cn/ebook/upload.php?tg_name='+up_name+'&filepath=http://www.cnblogs.com/vini/up/loadImg/'); 
  199.             var tmp_arr:Array = filter_str.split('|'); 
  200.             trace(tmp_arr); 
  201.             var filter:FileFilter = new FileFilter(tmp_arr[0],tmp_arr[1]); 
  202.             frl.browse([filter]); 
  203.         } 
  204.  
  205.  
  206.  
  207.         private function selectHandler(e:Event) 
  208.         { 
  209.             trace("可以上传么"); 
  210.             var fileReferenceList:FileReferenceList = e.target as FileReferenceList; 
  211.             var fileList:Array = fileReferenceList.fileList; 
  212.             for (var i:uint = 0; i < 1; i++) 
  213.             { 
  214.                 // org is : i < fileList.length : now just upload one  
  215.                 //control.test_txt.appendText("fileList["+i+"]:"+fileList[i].name + "\n"); 
  216.                 addPendingFile(fileList[i]); 
  217.             } 
  218.         } 
  219.  
  220.         private function addPendingFile(file:FileReference):void 
  221.         { 
  222.             //control.load_file_bar.visible = true;  //显示加载进度 
  223.  
  224.             file.addEventListener(Event.OPEN, openHandler); 
  225.             file.addEventListener(Event.COMPLETE, completeHandler); 
  226.             file.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
  227.             file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
  228.             file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
  229.             file.upload(uploadURL); 
  230.             trace("开始上传"); 
  231.         } 
  232.  
  233.  
  234.         private function openHandler(event:Event):void 
  235.         { 
  236.             var file:FileReference = FileReference(event.target); 
  237.             trace("开始了么"); 
  238.             //control.load_file_bar.ready(file.name); 
  239.         } 
  240.         private function completeHandler(event:Event):void 
  241.         { 
  242.             trace("aaa"+event.target.data); 
  243.             return; 
  244.             var file:FileReference = FileReference(event.target); 
  245.             trace(file.name); 
  246.         } 
  247.  
  248.         private function progressHandler(event:ProgressEvent):void 
  249.         { 
  250.             trace("过程过程"); 
  251.             var file:FileReference = FileReference(event.target); 
  252.             if (event.bytesLoaded / event.bytesTotal < 1
  253.             { 
  254.                 //control.load_file_bar.ready(file.name); 
  255.             } 
  256.             else 
  257.             { 
  258.                 //control.load_file_bar.updateinfo(file.name, event.bytesLoaded, event.bytesTotal); 
  259.             } 
  260.         } 
  261.         private function ioErrorHandler(event:Event):void 
  262.         { 
  263.             trace("错误一"); 
  264.             //control.test_txt.appendText("ioErrorHandler \n"); 
  265.         } 
  266.         private function securityErrorHandler(event:Event):void 
  267.         { 
  268.             trace("错误二"); 
  269.             //control.test_txt.appendText("securityErrorHandler \n"); 
  270.         } 
  271.  
  272.         private function cancelHandler(e:Event) 
  273.         { 
  274.  
  275.         } 
  276.  
  277.         private function pleaseScreen() 
  278.         { 
  279.             upMc.mesTxt.text = "请截图"
  280.             upMc.screenBtn.visible = true
  281.             upMc.screenBtn.addEventListener(MouseEvent.CLICK,clickHandler); 
  282.             mcArr.push(upMc.screenBtn); 
  283.         } 
  284.  
  285.         private function removeEvent() 
  286.         { 
  287.             var len:int = mcArr.length; 
  288.             for (var i=0; i<len; i++) 
  289.             { 
  290.                 mcArr[i].visible = false
  291.                 mcArr[i].removeEventListener(MouseEvent.CLICK,clickHandler); 
  292.             } 
  293.             mcArr = []; 
  294.         } 
  295.  
  296.         private function visibleBtn() 
  297.         { 
  298.             upMc.retakeBtn.visible = false
  299.             upMc.screenBtn.visible = false
  300.             upMc.browseBtn.visible = false
  301.             upMc.uploadBtn.visible = false
  302.         } 
  303.  
  304.         private function get_pic_name(no=null
  305.         { 
  306.             var nowtime:Date = new Date(); 
  307.             var str:String = timeformate(nowtime.getFullYear()); 
  308.             strstr = str.concat(timeformate(nowtime.getMonth()+1),timeformate(nowtime.getDate()) 
  309.             ,timeformate(nowtime.getHours()),timeformate(nowtime.getMinutes()),timeformate(nowtime.getSeconds())); 
  310.             if (no==null) 
  311.             { 
  312.                 return str; 
  313.             } 
  314.             for (var i:uint=0; i<no; i++) 
  315.             { 
  316.                 var ch:Number = randRange(0,51); 
  317.  
  318.                 if (ch>=26) 
  319.                 { 
  320.                     str += String.fromCharCode(97+ch-26); 
  321.                 } 
  322.                 else 
  323.                 { 
  324.                     str += String.fromCharCode(65+ch); 
  325.                 } 
  326.             } 
  327.             return str; 
  328.         } 
  329.  
  330.         private function getFileExt(file_name) 
  331.         { 
  332.             var file_array = file_name.split("."); 
  333.             trace("vvv:"+file_array[file_array.length - 1]) 
  334.             return file_array[file_array.length - 1]; 
  335.         } 
  336.  
  337.         private function timeformate(num):String 
  338.         { 
  339.             if (num < 10
  340.             { 
  341.                 return ("0" + num); 
  342.             } 
  343.             return num; 
  344.  
  345.         } 
  346.  
  347.         private function randRange(min:Number, max:Number):Number 
  348.         { 
  349.             var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min; 
  350.             return randomNum; 
  351.         } 
  352.     } 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器