flex实现手写在线签名

 转载自:http://www.iteye.com/topic/377994

Actionscript代码 复制代码

  1. package com.humanmonth.home.component.page.signature   
  2. {   
  3.     import flash.display.CapsStyle;   
  4.     import flash.display.JointStyle;   
  5.     import flash.display.LineScaleMode;   
  6.     import flash.events.MouseEvent;   
  7.        
  8.     import mx.containers.Canvas;   
  9.     import mx.core.UIComponent;   
  10.        
  11.     /**   
  12.      * 实现手写签名的白板   
  13.      * @author presses   
  14.      *    
  15.      */   
  16.     public class WriteArea extends Canvas   
  17.     {   
  18.         /**   
  19.          *笔    
  20.          */   
  21.         public var signature:UIComponent=new UIComponent();   
  22.         /**   
  23.          *颜色    
  24.          */   
  25.         public var myColor:uint=0x000000;   
  26.         /**   
  27.          *线条粗细    
  28.          */   
  29.         public var lineSize:int=1;   
  30.         /**   
  31.          *模式    
  32.          */   
  33.         public var pattern:String="圆珠笔";   
  34.         /**   
  35.          *当前的x座标    
  36.          */   
  37.         private var cX:Number;   
  38.         /**   
  39.          *当前的y座标    
  40.          */   
  41.         private var cY:Number;   
  42.            
  43.         public function WriteArea()   
  44.         {   
  45.             this.addChild(signature);   
  46.             this.addEventListener(MouseEvent.MOUSE_DOWN,beginDraw);   
  47.             this.addEventListener(MouseEvent.MOUSE_UP,endDraw);   
  48.         }   
  49.            
  50.         /**   
  51.          *鼠标压下时,开始画图,并添加移动鼠标画线的监听器    
  52.          */   
  53.         private function beginDraw(event:MouseEvent):void{   
  54.             this.signature.graphics.lineStyle(lineSize,myColor,1,true,LineScaleMode.NONE,CapsStyle.ROUND,JointStyle.ROUND,99);   
  55.             this.signature.graphics.beginFill(myColor);   
  56.             this.cX=event.localX;   
  57.             this.cY=event.localY;   
  58.             this.signature.graphics.moveTo(this.cX,this.cY);   
  59.             this.addEventListener(MouseEvent.MOUSE_MOVE,drawIng);   
  60.         }   
  61.            
  62.         /**   
  63.          * 鼠标移动时,画线    
  64.          */   
  65.         private function drawIng(event:MouseEvent):void{   
  66.             if(this.pattern=="圆珠笔"){   
  67.                 this.signature.graphics.moveTo(this.cX,this.cY);   
  68.             }   
  69.             this.signature.graphics.lineTo(event.localX,event.localY);   
  70.             this.cX=event.localX;   
  71.             this.cY=event.localY;   
  72.         }   
  73.            
  74.         /**   
  75.          * 结束画图    
  76.          */   
  77.         private function endDraw(event:MouseEvent):void{   
  78.             this.removeEventListener(MouseEvent.MOUSE_MOVE,drawIng);   
  79.         }   
  80.            
  81.     }   
  82. }  

效果图:http://rea.humanmonth.com/

package com.humanmonth.home.component.page.signature
{
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	/**
	 * 实现手写签名的白板
	 * @author presses
	 * 
	 */
	public class WriteArea extends Canvas
	{
		/**
		 *笔 
		 */
		public var signature:UIComponent=new UIComponent();
		/**
		 *颜色 
		 */
		public var myColor:uint=0x000000;
		/**
		 *线条粗细 
		 */
		public var lineSize:int=1;
		/**
		 *模式 
		 */
		public var pattern:String="圆珠笔";
		/**
		 *当前的x座标 
		 */
		private var cX:Number;
		/**
		 *当前的y座标 
		 */
		private var cY:Number;
		
		public function WriteArea()
		{
			this.addChild(signature);
			this.addEventListener(MouseEvent.MOUSE_DOWN,beginDraw);
			this.addEventListener(MouseEvent.MOUSE_UP,endDraw);
		}
		
		/**
		 *鼠标压下时,开始画图,并添加移动鼠标画线的监听器 
		 */
		private function beginDraw(event:MouseEvent):void{
			this.signature.graphics.lineStyle(lineSize,myColor,1,true,LineScaleMode.NONE,CapsStyle.ROUND,JointStyle.ROUND,99);
			this.signature.graphics.beginFill(myColor);
			this.cX=event.localX;
			this.cY=event.localY;
			this.signature.graphics.moveTo(this.cX,this.cY);
			this.addEventListener(MouseEvent.MOUSE_MOVE,drawIng);
		}
		
		/**
		 * 鼠标移动时,画线 
		 */
		private function drawIng(event:MouseEvent):void{
			if(this.pattern=="圆珠笔"){
				this.signature.graphics.moveTo(this.cX,this.cY);
			}
			this.signature.graphics.lineTo(event.localX,event.localY);
			this.cX=event.localX;
			this.cY=event.localY;
		}
		
		/**
		 * 结束画图 
		 */
		private function endDraw(event:MouseEvent):void{
			this.removeEventListener(MouseEvent.MOUSE_MOVE,drawIng);
		}
		
	}
}


            上传签名图片(上传到服务器或保存到本地):fp10(flash player)可以不经服务器,直接把图片保存到本地。但为了兼容fp9,这里的实现是先把图片上传到服务器,再调用下载功能。实现的思路是先把画图的组件转化为BitmapData,然后再编码成jpeg格式,并上传到服务器。最后调用客户端下载。这里要注意的一点是,fp10对下载的api作了限制,下载动作只能由用户触发。代码如下:

Actionscript代码 复制代码
  1. package com.humanmonth.home.component.page.signature.remote   
  2. {   
  3.     import com.humanmonth.global.Config;   
  4.        
  5.     import flash.display.BitmapData;   
  6.     import flash.events.Event;   
  7.     import flash.net.FileReference;   
  8.     import flash.net.URLLoader;   
  9.     import flash.net.URLRequest;   
  10.     import flash.net.URLRequestMethod;   
  11.        
  12.     import mx.controls.Alert;   
  13.     import mx.graphics.codec.JPEGEncoder;   
  14.     import mx.managers.CursorManager;   
  15.        
  16.     /**   
  17.      * 图片的上传及下载   
  18.      * @author presses   
  19.      *    
  20.      */   
  21.     public class Connector   
  22.     {   
  23.         private var file:FileReference;   
  24.         private var myId:String;   
  25.         public function Connector()   
  26.         {   
  27.         }   
  28.            
  29.         /**   
  30.          * 保存图片   
  31.          */   
  32.         public function savePic(myData:BitmapData,fun:Function):void{   
  33.             CursorManager.setBusyCursor();   
  34.             var url:String=Config.picLink+"rea/pic.do?action=savePic&timestamp="+new Date().getTime();   
  35.             var request:URLRequest = new URLRequest(url);              
  36.             request.method=URLRequestMethod.POST;   
  37.             request.contentType = "application/octet-stream";    
  38.             request.data=new JPEGEncoder(80).encode(myData);   
  39.             var loader:URLLoader = new URLLoader();                     
  40.             loader.load(request) ;           
  41.             loader.addEventListener(Event.COMPLETE, fun) ;     
  42.             loader.addEventListener(Event.COMPLETE,initMyId);   
  43.             Alert.show("正在上传图片,等待数秒后,即可下载图片");           
  44.         }   
  45.            
  46.         private function initMyId(event:Event):void{   
  47.             CursorManager.removeBusyCursor();   
  48.             var loader:URLLoader=URLLoader(event.target);   
  49.             this.myId=loader.data;   
  50.             Alert.show("上传图片成功,现在可以点击‘下载图片’按钮,保存图片到本地。");   
  51.                
  52.         }   
  53.            
  54.         /**   
  55.          * 下载图片    
  56.          */   
  57.         public function downloadFile(event:Event):void{   
  58.             var url2:String=Config.picLink+"rea/pic.do?action=queryPicById&pid="+myId+"&timestamp="+new Date().getTime();   
  59.             var req:URLRequest=new URLRequest(url2);   
  60.             file=new FileReference();   
  61.             file.download(req,"humanmonth.jpg");   
  62.         }   
  63.     }   
  64. }  
package com.humanmonth.home.component.page.signature.remote
{
	import com.humanmonth.global.Config;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import mx.controls.Alert;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.CursorManager;
	
	/**
	 * 图片的上传及下载
	 * @author presses
	 * 
	 */
	public class Connector
	{
		private var file:FileReference;
		private var myId:String;
		public function Connector()
		{
		}
		
		/**
		 * 保存图片
		 */
		public function savePic(myData:BitmapData,fun:Function):void{
		    CursorManager.setBusyCursor();
			var url:String=Config.picLink+"rea/pic.do?action=savePic&timestamp="+new Date().getTime();
 			var request:URLRequest = new URLRequest(url); 			
 			request.method=URLRequestMethod.POST;
		 	request.contentType = "application/octet-stream"; 
 			request.data=new JPEGEncoder(80).encode(myData);
            var loader:URLLoader = new URLLoader();                  
            loader.load(request) ;        
            loader.addEventListener(Event.COMPLETE, fun) ; 	
            loader.addEventListener(Event.COMPLETE,initMyId);
            Alert.show("正在上传图片,等待数秒后,即可下载图片");		
		}
		
		private function initMyId(event:Event):void{
		    CursorManager.removeBusyCursor();
			var loader:URLLoader=URLLoader(event.target);
			this.myId=loader.data;
			Alert.show("上传图片成功,现在可以点击‘下载图片’按钮,保存图片到本地。");
			
		}
		
		/**
		 * 下载图片 
		 */
		public function downloadFile(event:Event):void{
			var url2:String=Config.picLink+"rea/pic.do?action=queryPicById&pid="+myId+"&timestamp="+new Date().getTime();
			var req:URLRequest=new URLRequest(url2);
			file=new FileReference();
			file.download(req,"humanmonth.jpg");
		}
	}
}

 

Actionscript代码 复制代码
  1. package com.humanmonth.home.component.page.signature   
  2. {   
  3.     import com.humanmonth.home.component.page.signature.remote.Connector;   
  4.        
  5.     import flash.display.BitmapData;   
  6.     import flash.events.Event;   
  7.     import flash.events.MouseEvent;   
  8.        
  9.     import mx.core.Application;   
  10.     import mx.events.ColorPickerEvent;   
  11.     import mx.events.FlexEvent;   
  12.     import mx.events.ListEvent;   
  13.     import mx.events.NumericStepperEvent;   
  14.        
  15.     /**   
  16.      * 控制面版   
  17.      * @author presses   
  18.      *    
  19.      */   
  20.     public class MyControlBarAs extends MyControlBar   
  21.     {   
  22.         public var writearea:WriteArea;   
  23.         private var connector:Connector=new Connector();   
  24.         public function MyControlBarAs()   
  25.         {   
  26.             super();   
  27.             this.addEventListener(FlexEvent.CREATION_COMPLETE,myInit);   
  28.         }   
  29.            
  30.         private function myInit(event:Event):void{   
  31.             this.writearea=Application.application.signature.writearea;   
  32.             this.reset.addEventListener(MouseEvent.CLICK,cleanArea);   
  33.             this.size.addEventListener(NumericStepperEvent.CHANGE,setLineSize);   
  34.             this.color.addEventListener(ColorPickerEvent.CHANGE,setColor);   
  35.             this.pattern.addEventListener(ListEvent.CHANGE,setPattern);   
  36.             this.savePic.addEventListener(MouseEvent.CLICK,savePicture);   
  37.             this.downloadPic.addEventListener(MouseEvent.CLICK,connector.downloadFile)   
  38.         }   
  39.         /**   
  40.          * 保存图片    
  41.          */   
  42.         private function savePicture(event:Event):void{   
  43.             var myData:BitmapData=new BitmapData(this.writearea.width,this.writearea.height);   
  44.             myData.draw(this.writearea);   
  45.             connector.savePic(myData,enableDownload);    
  46.         }   
  47.            
  48.         private function enableDownload(event:Event):void{   
  49.             this.downloadPic.enabled=true;   
  50.         }   
  51.         /**   
  52.          * 设置模式    
  53.          */   
  54.         private function setPattern(event:Event):void{   
  55.             this.writearea.pattern=String(this.pattern.value);   
  56.         }   
  57.         /**   
  58.          * 清空写字区    
  59.          */   
  60.         private function cleanArea(event:Event):void{   
  61.             this.writearea.signature.graphics.clear();   
  62.         }   
  63.            
  64.         /**   
  65.          * 设置线条粗细    
  66.          */   
  67.         public function setLineSize(event:Event):void{   
  68.             this.writearea.lineSize=this.size.value;   
  69.         }   
  70.            
  71.         /**   
  72.          * 设置颜色   
  73.          */   
  74.         public function setColor(event:Event):void{   
  75.             this.writearea.myColor=uint(this.color.value);   
  76.         }   
  77.            
  78.            
  79.     }   
  80. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值