在線手寫簽名分兩部份。第一部分是畫圖功能的實(shí)現(xiàn),第二部份是上傳圖片的功能(上傳到服務(wù)器或保存到本地)。
畫圖:畫圖比較簡(jiǎn)單,只要用到了graphics對(duì)像的幾個(gè)方法。當(dāng)鼠標(biāo)按下時(shí),調(diào)用graphics的beginFill和moveTo方法。同時(shí),還要把調(diào)用了lineTo的方法加入到鼠標(biāo)的MOUSE_MOVE事件中。代碼如下:
Actionscript代碼
復(fù)制代碼 代碼如下:
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;
/**
* 實(shí)現(xiàn)手寫簽名的白板
* @author presses
*
*/
public class WriteArea extends Canvas
{
/**
*筆
*/
public var signature:UIComponent=new UIComponent();
/**
*顏色
*/
public var myColor:uint=0x000000 ;
/**
*線條粗細(xì)
*/
public var lineSize:int=1 ;
/**
*模式
*/
public var pattern:String="圓珠筆";
/**
*當(dāng)前的x座標(biāo)
*/
private var cX:Number;
/**
*當(dāng)前的y座標(biāo)
*/
private var cY:Number;
public function WriteArea()
{
this.addChild(signature);
this.addEventListener(MouseEvent.MOUSE_DOWN,beginDraw);
this.addEventListener(MouseEvent.MOUSE_UP,endDraw);
}
/**
*鼠標(biāo)壓下時(shí),開(kāi)始畫圖,并添加移動(dòng)鼠標(biāo)畫線的監(jiān)聽(tīng)器
*/
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);
}
/**
* 鼠標(biāo)移動(dòng)時(shí),畫線
*/
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;
}
/**
* 結(jié)束畫圖
*/
private function endDraw(event:MouseEvent):void{
this.removeEventListener(MouseEvent.MOUSE_MOVE,drawIng);
}
}
}
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;
/**
* 實(shí)現(xiàn)手寫簽名的白板
* @author presses
*
*/
public class WriteArea extends Canvas
{
/**
*筆
*/
public var signature:UIComponent=new UIComponent();
/**
*顏色
*/
public var myColor:uint=0x000000;
/**
*線條粗細(xì)
*/
public var lineSize:int=1;
/**
*模式
*/
public var pattern:String="圓珠筆";
/**
*當(dāng)前的x座標(biāo)
*/
private var cX:Number;
/**
*當(dāng)前的y座標(biāo)
*/
private var cY:Number;
public function WriteArea()
{
this.addChild(signature);
this.addEventListener(MouseEvent.MOUSE_DOWN,beginDraw);
this.addEventListener(MouseEvent.MOUSE_UP,endDraw);
}
/**
*鼠標(biāo)壓下時(shí),開(kāi)始畫圖,并添加移動(dòng)鼠標(biāo)畫線的監(jiān)聽(tīng)器
*/
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);
}
/**
* 鼠標(biāo)移動(dòng)時(shí),畫線
*/
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;
}
/**
* 結(jié)束畫圖
*/
private function endDraw(event:MouseEvent):void{
this.removeEventListener(MouseEvent.MOUSE_MOVE,drawIng);
}
}
}
上傳簽名圖片(上傳到服務(wù)器或保存到本地):fp10(flash player)可以不經(jīng)服務(wù)器,直接把圖片保存到本地。但為了兼容fp9,這里的實(shí)現(xiàn)是先把圖片上傳到服務(wù)器,再調(diào)用下載功能。實(shí)現(xiàn)的思路是先把畫圖的組件轉(zhuǎn)化為BitmapData,然后再編碼成jpeg格式,并上傳到服務(wù)器。最后調(diào)用客戶端下載。這里要注意的一點(diǎn)是,fp10對(duì)下載的api作了限制,下載動(dòng)作只能由用戶觸發(fā)。代碼如下:
Actionscript代碼
復(fù)制代碼 代碼如下:
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("正在上傳圖片,等待數(shù)秒后,即可下載圖片");
}
private function initMyId(event:Event):void{
CursorManager.removeBusyCursor();
var loader:URLLoader=URLLoader(event.target);
this.myId=loader.data;
Alert.show("上傳圖片成功,現(xiàn)在可以點(diǎn)擊‘下載圖片'按鈕,保存圖片到本地。");
}
/**
* 下載圖片
*/
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");
}
}
}
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("正在上傳圖片,等待數(shù)秒后,即可下載圖片");
}
private function initMyId(event:Event):void{
CursorManager.removeBusyCursor();
var loader:URLLoader=URLLoader(event.target);
this.myId=loader.data;
Alert.show("上傳圖片成功,現(xiàn)在可以點(diǎn)擊‘下載圖片'按鈕,保存圖片到本地。");
}
/**
* 下載圖片
*/
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代碼
復(fù)制代碼 代碼如下:
package com.humanmonth.home.component.page.signature
{
import com.humanmonth.home.component.page.signature.remote.Connector;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.core.Application;
import mx.events.ColorPickerEvent;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.events.NumericStepperEvent;
/**
* 控制面版
* @author presses
*
*/
public class MyControlBarAs extends MyControlBar
{
public var writearea:WriteArea;
private var connector:Connector=new Connector();
public function MyControlBarAs()
{
super();
this.addEventListener(FlexEvent.CREATION_COMPLETE,myInit);
}
private function myInit(event:Event):void{
this.writearea=Application.application.signature.writearea;
this.reset.addEventListener(MouseEvent.CLICK,cleanArea);
this.size.addEventListener(NumericStepperEvent.CHANGE,setLineSize);
this.color.addEventListener(ColorPickerEvent.CHANGE,setColor);
this.pattern.addEventListener(ListEvent.CHANGE,setPattern);
this.savePic.addEventListener(MouseEvent.CLICK,savePicture);
this.downloadPic.addEventListener(MouseEvent.CLICK,connector.downloadFile)
}
/**
* 保存圖片
*/
private function savePicture(event:Event):void{
var myData:BitmapData=new BitmapData(this.writearea.width,this.writearea.height);
myData.draw(this.writearea);
connector.savePic(myData,enableDownload);
}
private function enableDownload(event:Event):void{
this.downloadPic.enabled=true;
}
/**
* 設(shè)置模式
*/
private function setPattern(event:Event):void{
this.writearea.pattern=String(this.pattern.value);
}
/**
* 清空寫字區(qū)
*/
private function cleanArea(event:Event):void{
this.writearea.signature.graphics.clear();
}
/**
* 設(shè)置線條粗細(xì)
*/
public function setLineSize(event:Event):void{
this.writearea.lineSize=this.size.value;
}
/**
* 設(shè)置顏色
*/
public function setColor(event:Event):void{
this.writearea.myColor=uint(this.color.value);
}
}
}
package com.humanmonth.home.component.page.signature
{
import com.humanmonth.home.component.page.signature.remote.Connector;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import mx.core.Application;
import mx.events.ColorPickerEvent;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.events.NumericStepperEvent;
/**
* 控制面版
* @author presses
*
*/
public class MyControlBarAs extends MyControlBar
{
public var writearea:WriteArea;
private var connector:Connector=new Connector();
public function MyControlBarAs()
{
super();
this.addEventListener(FlexEvent.CREATION_COMPLETE,myInit);
}
private function myInit(event:Event):void{
this.writearea=Application.application.signature.writearea;
this.reset.addEventListener(MouseEvent.CLICK,cleanArea);
this.size.addEventListener(NumericStepperEvent.CHANGE,setLineSize);
this.color.addEventListener(ColorPickerEvent.CHANGE,setColor);
this.pattern.addEventListener(ListEvent.CHANGE,setPattern);
this.savePic.addEventListener(MouseEvent.CLICK,savePicture);
this.downloadPic.addEventListener(MouseEvent.CLICK,connector.downloadFile)
}
/**
* 保存圖片
*/
private function savePicture(event:Event):void{
var myData:BitmapData=new BitmapData(this.writearea.width,this.writearea.height);
myData.draw(this.writearea);
connector.savePic(myData,enableDownload);
}
private function enableDownload(event:Event):void{
this.downloadPic.enabled=true;
}
/**
* 設(shè)置模式
*/
private function setPattern(event:Event):void{
this.writearea.pattern=String(this.pattern.value);
}
/**
* 清空寫字區(qū)
*/
private function cleanArea(event:Event):void{
this.writearea.signature.graphics.clear();
}
/**
* 設(shè)置線條粗細(xì)
*/
public function setLineSize(event:Event):void{
this.writearea.lineSize=this.size.value;
}
/**
* 設(shè)置顏色
*/
public function setColor(event:Event):void{
this.writearea.myColor=uint(this.color.value);
}
}
}
到這里為止,功能已經(jīng)實(shí)現(xiàn)了。但效果不太好。主要是簽名時(shí),筆畫不圓滑,在flex 的api中,好像找不到在flash中設(shè)置圓滑的功能。
效果圖:http://rea.humanmonth.com/
12下一頁(yè)閱讀全文