Flex实现的上传摄像头拍照并将UI保存为图片
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.graphics.codec.JPEGEncoder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.utils.UIDUtil;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
initCamera(videoDis);
}
//初始化Camera控件,并添加在VideoDisplay
public function initCamera(videoDis:UIComponent):void
{
var myCamera:Camera = Camera.getCamera();//获取客户端摄像头
myCamera.setMode(500,500,30);
var myVideo:Video = new Video(500,500);
myVideo.attachCamera(myCamera);//获取摄像头的视频流
videoDis.addChild(myVideo);
}
//将可视的UIComponent组件转换为图片
public function UItoBitmap(source:UIComponent,target:UIComponent):void
{
var width :int = source.width;
var height :int = source.height;
var bitmapData:BitmapData =new BitmapData(width,height);
bitmapData.draw(source,new Matrix());
var bitmap:Bitmap=new Bitmap(bitmapData);
var uic:UIComponent = new UIComponent();
uic.addChild(bitmap);
target.addChild(uic);
}
//将可视的UIComponent组件保存为本地图片
public function UISaveAsImg(imgID:UIComponent):void
{
var width :int = imgID.width;
var height :int = imgID.height;
var bitmapData:BitmapData =new BitmapData(width,height);
bitmapData.draw(imgID);
var byteArr:ByteArray = bitmapData.getPixels(new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);
var fileRefer:FileReference = new FileReference();
fileRefer.save(byteArr123,UIDUtil.createUID()+".png");
fileRefer.addEventListener(Event.COMPLETE,function completeHandler():void{
Alert.show("保存本地成功");
});
}
//照片上传到服务器
protected function upLoadImg(imgID:UIComponent):void
{
var width :int = imgID.width;
var height :int = imgID.height;
var bitmapData:BitmapData =new BitmapData(width,height);
bitmapData.draw(imgID);
var byteArr:ByteArray = bitmapData.getPixels(new Rectangle(0,0,width,height));
var byteArr123:ByteArray =new JPEGEncoder().encodeByteArray(byteArr,width,height);
webService.UploadFile(byteArr123,"123.png");
}
protected function webService_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
protected function webService_successHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:WebService id="webService" wsdl="http://10.19.1.48/upImg/Service1.asmx?WSDL" fault="webService_faultHandler(event)">
<s:operation name="UploadFile" result="webService_successHandler(event)"></s:operation>
</s:WebService>
</fx:Declarations>
<s:VideoDisplay id="videoDis" width="500" height="500" click="UItoBitmap(videoDis,t_img_Picture),UItoBitmap(videoDis,content)" toolTip="点击拍照"></s:VideoDisplay>
<mx:DateChooser id="myDate" x="62" y="508" click="UItoBitmap(myDate,t_img_Picture),UItoBitmap(myDate,content)" toolTip="点击拍照"/>
<mx:Image id="t_img_Picture" x="522" y="0" width="500" height="500" click="UISaveAsImg(t_img_Picture)" toolTip="点击保存本地"/>
<mx:Canvas id="content" x="500" y="300" width="500" height="500" click="UISaveAsImg(content)" toolTip="点击保存本地"></mx:Canvas>
<s:Button x="305" y="537" label="上传" width="130" height="64" click="upLoadImg(t_img_Picture)"/>
</s:Application>
/// <summary>
/// 上传文件到远程服务器
/// </summary>
/// <param name="fileBytes">文件流</param>
/// <param name="fileName">文件名</param>
/// <returns>字符串</returns>
[WebMethod(Description = "上传文件到远程服务器.")]
public string UploadFile(byte[] fileBytes, string fileName)
{
try
{
MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。
FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); ///2.定义实际文件对象,保存上载的文件。
memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件
memoryStream.Close();
fileUpload.Close();
fileUpload = null;
memoryStream = null;
return "文件已成功上传至服务器";
}
catch (Exception ex)
{
return ex.Message;
}
}