[AS3]URLLoader+URLRequest+JPGEncoder实现BitmapData图片数据保存

    这段时间工作忙,一直抽不出空来写点内容。今天晚上正好能静下心来,把工作中用到Flash位图数据保存整理一下,写了下面的应用实例。
Flash客户端代码(test12.swf)
  1. package project.test {  
  2.       
  3.     import flash.display.*;   
  4.     import flash.net.*;  
  5.     import flash.events.*;  
  6.     import flash.utils.ByteArray;  
  7.       
  8.     import fl.controls.Button;  
  9.       
  10.     import com.klstudio.images.JPGEncoder;  
  11.       
  12.     public class Test12 extends Sprite{  
  13.         private var _base:Shape;  
  14.         private var _rnd_button:Button;  
  15.         private var _submit_button:Button;  
  16.           
  17.         private static const UPLOAD_PAGE:String="http://localhost:8080/upload.jsp";   
  18.           
  19.         public function Test12(){             
  20.             _base = new Shape();  
  21.             _base.x = _base.y = 5;  
  22.             addChild(_base);              
  23.             drawLine();       
  24.               
  25.             _rnd_button = new Button();  
  26.             _rnd_button.label = "随机画图";  
  27.             _rnd_button.setSize(90,22);  
  28.             _rnd_button.move(5,205);  
  29.             _rnd_button.addEventListener(MouseEvent.CLICK,clickHandler);  
  30.             addChild(_rnd_button);  
  31.               
  32.             _submit_button = new Button();  
  33.             _submit_button.label = "保存图片";  
  34.             _submit_button.setSize(90,22);  
  35.             _submit_button.move(105,205);  
  36.             _submit_button.addEventListener(MouseEvent.CLICK,clickHandler);  
  37.             addChild(_submit_button);  
  38.         }  
  39.           
  40.         public function drawRndRect():void{  
  41.             _base.graphics.beginFill(getRndColor());  
  42.             var gWidth:uint = getRndInt();  
  43.             var gHeight:uint = getRndInt();  
  44.             var gX:uint = getRndInt(5,180-gWidth+5);  
  45.             var gY:uint = getRndInt(5,180-gHeight+5);  
  46.             _base.graphics.drawRect(gX,gY,gWidth,gHeight);            
  47.         }  
  48.           
  49.         public function getRndInt(min:uint=10,max:uint=180):uint{  
  50.             return uint(Math.random()*(max-min))+min;  
  51.         }  
  52.           
  53.         public function getRndColor():uint{  
  54.             var r:uint = getRndInt(10,220);  
  55.             var g:uint = getRndInt(10,220);  
  56.             var b:uint = getRndInt(10,220);  
  57.             var c:uint = (r << 16) + (g << 8) + b;  
  58.             return c;  
  59.         }  
  60.           
  61.         public function drawLine():void{              
  62.             _base.graphics.beginFill(0x666666);  
  63.             _base.graphics.drawRect(0,0,190,190);  
  64.             _base.graphics.beginFill(0xFFFFFF);  
  65.             _base.graphics.drawRect(1,1,188,188);  
  66.             _base.graphics.endFill();  
  67.         }  
  68.           
  69.         public function getBitmapData():BitmapData{  
  70.             var bmd:BitmapData = new BitmapData(_base.width,_base.height);  
  71.             bmd.draw(_base);  
  72.             return bmd;  
  73.         }  
  74.           
  75.         public function submit():void{  
  76.             _rnd_button.enabled = false;  
  77.             _submit_button.enabled = false;  
  78.             var encoder:JPGEncoder = new JPGEncoder(80);  
  79.             var bytes:ByteArray = encoder.encode(getBitmapData());  
  80.             var request:URLRequest = new URLRequest(UPLOAD_PAGE);  
  81.             //data值就为图片编码数据ByteArray;  
  82.             request.data = bytes;  
  83.             request.method = URLRequestMethod.POST;  
  84.             //这个是关键,内容类型必须是下面文件流形式;  
  85.             request.contentType = "application/octet-stream";  
  86.               
  87.             var loader:URLLoader = new URLLoader();           
  88.             loader.addEventListener(Event.COMPLETE, completeHandler);  
  89.             loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);  
  90.             loader.load(request);  
  91.         }  
  92.           
  93.         private function completeHandler(event:Event):void{  
  94.             trace("图片上传成功");  
  95.             _rnd_button.enabled = true;  
  96.             _submit_button.enabled = true;  
  97.         }  
  98.           
  99.         private function errorHandler(event:IOErrorEvent):void{  
  100.             trace("图片上传失败");  
  101.             _rnd_button.enabled = true;  
  102.             _submit_button.enabled = true;  
  103.         }  
  104.           
  105.         private function clickHandler(event:MouseEvent):void{  
  106.             switch(event.target){  
  107.                 case _rnd_button:  
  108.                     _base.graphics.clear();  
  109.                     drawLine();  
  110.                     drawRndRect();  
  111.                     drawRndRect();  
  112.                     drawRndRect();  
  113.                     drawRndRect();  
  114.                     break;  
  115.                 case _submit_button:  
  116.                     submit();  
  117.                     break;  
  118.             }  
  119.         }  
  120.     }     
  121. }  
服务端代码(upload.jsp):我使用是Java服务端,当然PHP,ASP.NET也可以了,我在这里就不一一举例了,有兴趣的朋友自己研究了!

  1. <%@ page contentType="text/html; charset=utf-8" language="java"%>  
  2. <%@ page import="java.util.*"%>  
  3. <%@ page import="java.io.*"%>  
  4. <%  
  5. int v;  
  6. String filePath = request.getRealPath(System.currentTimeMillis()+".jpg");  
  7. BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream());  
  8. FileOutputStream outputStream = new FileOutputStream(new File(filePath));  
  9. byte [] bytes = new byte[1024];  
  10. while((v=inputStream.read(bytes))>0){  
  11.     outputStream.write(bytes,0,v);  
  12. }  
  13. outputStream.close();  
  14. inputStream.close();  
  15. %>  
相关实例文件打包下载: http://www.klstudio.com/demo/test/test12.rar

你可能感兴趣的:(request)