当前位置: 移动技术网 > IT编程>网页制作>Flex > Flex实现的上传摄像头拍照并将UI保存为图片

Flex实现的上传摄像头拍照并将UI保存为图片

2017年12月01日  | 移动技术网IT编程  | 我要评论
flex客户端代码:
复制代码 代码如下:

<?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>

webservice代码:
复制代码 代码如下:

/// <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;
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网