Flex4 合成图片另存为到硬盘

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"  
  3.       xmlns:s="library://ns.adobe.com/flex/spark"  
  4.       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" 
  5.       creationComplete="application1_creationCompleteHandler(event)" width="660" height="800"> 
  6. <fx:Declarations> 
  7.   <!-- 将非可视元素(例如服务、值对象)放在此处 --> 
  8.    
  9. </fx:Declarations> 
  10. <fx:Script> 
  11.   <![CDATA[
  12.    import flash.net.FileReference;
  13.   
  14.    import mx.events.FlexEvent;
  15.    import mx.graphics.ImageSnapshot;
  16.    import mx.graphics.codec.JPEGEncoder;
  17.   
  18.    import spark.events.IndexChangeEvent;
  19.   
  20.    private var file:FileReference;
  21.    private var ImageArray:Array;
  22.    private var bitMapArray:Array;
  23.    private var currentMoveImage:Image;
  24.    private var dx:Number;
  25.    private var dy:Number;
  26.    private var qyX:Number = 10;
  27.    private var qyY:Number = 210;
  28.    private var qyWidth:Number = 640;
  29.    private var qyHeight:Number = 400;
  30.    private var bitmapdata:BitmapData;
  31.   
  32.    [Embed(source='1.gif')]
  33.    private var gif1:Class;
  34.   
  35.    [Embed(source='2.gif')]
  36.    private var gif2:Class;
  37.   
  38.    [Embed(source='3.gif')]
  39.    private var gif3:Class;
  40.   
  41.    [Embed(source='4.gif')]
  42.    private var gif4:Class;
  43.   
  44.    [Embed(source='5.gif')]
  45.    private var gif5:Class;
  46.   
  47.    [Embed(source='6.gif')]
  48.    private var gif6:Class;
  49.   
  50.    [Embed(source='7.gif')]
  51.    private var gif7:Class;
  52.   
  53.    [Embed(source='8.gif')]
  54.    private var gif8:Class;
  55.   
  56.    [Embed(source='9.gif')]
  57.    private var gif9:Class;
  58.   
  59.    [Embed(source='10.gif')]
  60.    private var gif10:Class;
  61.   
  62.    [Bindable]private var imgArray:Array = [
  63.     {imgSource:gif1},
  64.     {imgSource:gif2},
  65.     {imgSource:gif3},
  66.     {imgSource:gif4},
  67.     {imgSource:gif5},
  68.     {imgSource:gif6},
  69.     {imgSource:gif7},
  70.     {imgSource:gif8},
  71.     {imgSource:gif9},
  72.     {imgSource:gif10}
  73.                ]
  74.   
  75.   
  76.    /**
  77.     *点击另存为方法
  78.     */
  79.    protected function saveas_clickHandler(event:MouseEvent):void
  80.    {
  81.     bitmapdata = new BitmapData(qyWidth,qyHeight);
  82.     var l:int = bitMapArray.length;
  83.     for(var i:int = 0;i<l;i++)
  84.     {
  85.      var bit:Bitmap = bitMapArray[i];
  86.      currentMoveImage = ImageArray[i];
  87.      var bitdata:BitmapData = bit.bitmapData;
  88.      var rect:Rectangle = new Rectangle(0,0,currentMoveImage.width,currentMoveImage.height);
  89.      var point:Point = new Point(currentMoveImage.x-qyX,currentMoveImage.y-qyY);
  90.      bitmapdata.copyPixels(bitdata,rect,point);
  91.     }
  92.     saveImg.source = new Bitmap(bitmapdata);
  93.     var timer:Timer = new Timer(10,1);
  94.     timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComplete);
  95.     timer.start();
  96.    }
  97.   
  98.    /**
  99.     *延时10毫秒等待数据加载完
  100.     */
  101.    private function onTimerComplete(event:TimerEvent):void
  102.    {
  103.     var imgData:ImageSnapshot = ImageSnapshot.captureImage(saveImg,0);
  104.     file.save(imgData.data,"image.png");
  105.    }
  106.   
  107.    /**
  108.     *点击清除
  109.     */
  110.    private function clearImg(event:MouseEvent):void
  111.    {
  112.     for(var i:int = 0;i<ImageArray.length;i++)
  113.     {
  114.      this.removeElement(ImageArray[i]);
  115.     }
  116.     ImageArray = [];
  117.     bitMapArray = [];
  118.    }
  119.   
  120.    private function onChange(event:IndexChangeEvent):void
  121.    {
  122.     var obj:Object = list.selectedItem;
  123.     var bit:Bitmap = new obj.imgSource();
  124.     bitMapArray.push(bit);
  125.     var img:Image = new Image();
  126.     img.source = obj.imgSource;
  127.     img.y = 220;
  128.     img.x = 20;
  129.     img.buttonMode = true;
  130.     img.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
  131.     ImageArray.push(img);
  132.     this.addElement(img);
  133.     list.selectedIndex = -1;
  134.    }
  135.    /**
  136.     *鼠标按下
  137.     */
  138.    private function onDown(event:MouseEvent):void
  139.    {
  140.     currentMoveImage = event.target as Image;
  141.     dx = mouseX - currentMoveImage.x;
  142.     dy = mouseY - currentMoveImage.y;
  143.     this.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
  144.     this.stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
  145.    }
  146.    /**
  147.     *鼠标移动实现图片拖动
  148.     */
  149.    private function onMove(event:MouseEvent):void
  150.    {
  151.     currentMoveImage.x = mouseX-dx;
  152.     currentMoveImage.y = mouseY-dy;
  153.     if(currentMoveImage.x<qyX){
  154.      currentMoveImage.x = qyX+1;
  155.     }
  156.     if(currentMoveImage.x+currentMoveImage.width>qyWidth+qyX){
  157.      currentMoveImage.x = qyWidth+qyX-1-currentMoveImage.width;
  158.     }
  159.     if(currentMoveImage.y<qyY){
  160.      currentMoveImage.y = qyY+1;
  161.     }
  162.     if(currentMoveImage.y+currentMoveImage.height>(qyHeight+qyY)){
  163.      currentMoveImage.y = qyHeight+qyY-1-currentMoveImage.height;
  164.     }
  165.    }
  166.    /**
  167.     *鼠标抬起移除事件
  168.     */
  169.    private function onUp(event:MouseEvent):void
  170.    {
  171.     this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
  172.     this.stage.removeEventListener(MouseEvent.MOUSE_UP,onUp);
  173.     currentMoveImage = null;
  174.    }
  175.    protected function application1_creationCompleteHandler(event:FlexEvent):void
  176.    {
  177.     //创建上传下载文件对象
  178.     file = new FileReference();
  179.     //实例化数组 盛放创建完得图片
  180.     ImageArray = new Array();
  181.     bitMapArray = new Array();
  182.    
  183.     saveImg.width = qyWidth;
  184.     saveImg.height = qyHeight;
  185.     k.graphics.lineStyle(1,0x222222,1);
  186.     k.graphics.drawRect(qyX,qyY,qyWidth,qyHeight);
  187.     k.graphics.endFill();
  188.    }
  189.   ]]> 
  190. </fx:Script> 
  191. <s:Label x="23" y="10" text="请选择图片进行拼图" height="23" color="#F70606" fontFamily="宋体" fontSize="15"/> 
  192. <s:List x="23" y="30" width="611" itemRenderer="imgRender" height="158" id="list" change="onChange(event)"> 
  193.   <s:layout> 
  194.    <s:HorizontalLayout/> 
  195.   </s:layout> 
  196.   <s:dataProvider> 
  197.    <s:ArrayCollection source="{imgArray}"/> 
  198.   </s:dataProvider> 
  199. </s:List> 
  200. <s:Button x="11" y="653" label="另存为" id="saveas" click="saveas_clickHandler(event)" visible="true"/> 
  201. <mx:Image id="saveImg" visible="false"/> 
  202. <s:SpriteVisualElement id="k"/> 
  203. <s:Button x="104" y="653" label="清除" click="clearImg(event)"/> 
  204. </s:Application> 

 

 

zhuan zi :http://632408004.iteye.com/blog/940881

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值