验证中...
Languages: ActionScript
Categories: Web开发技术
Latest update 2019-04-08 23:56
flex 拍照
Raw Copy
<?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">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Style>
.textInput{
fontSize:30;
textAlign:"center";
color:"#808080"
}
.textInputBlack{
fontSize:30;
textAlign:"center";
color:"#000000"
}
</fx:Style>
<fx:Script>
<![CDATA[
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Camera;
import flash.media.Video;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.utils.ByteArray;
import spark.components.Group;
import spark.components.Image;
import spark.components.Panel;
import spark.components.VGroup;
import spark.components.TextInput;
import mx.controls.Alert;
import mx.graphics.codec.JPEGEncoder;
//import flash.external.ExternalInterface;
//显示摄像头 video
private function setVideo(w:int, h:int, t:int, l:int): Video {
var video:Video = new Video(w, h);
var camera:Camera = Camera.getCamera();
camera.setMode(3978, 2976, 24);
video.attachCamera(camera);
return video;
}
//加载摄像头
private var subUrl:String = "";
private function loadCamer():void{
var cameraVideo:Video = setVideo(400, 300, 0, 0);
cameraVideo.name = "cameraVideo";
cameraVD.addChild(cameraVideo);
//var getStr:String = ExternalInterface.call("loadFinsh");
//this.subUrl = getStr;
}
//点击拍照按钮,进行拍照
[Embed(source='image/icon-error.png')] //关闭图标
private var imgClass:Class;
private function takePic():void{
var video:Video = cameraVD.getChildByName("cameraVideo") as Video;
var bmpData:BitmapData = new BitmapData(video.width, video.height);
bmpData.draw(video);
//var bmp =Bitmap(bmpData, PixelSnapping.NEVER, true);
//TileListDp.addItem(bmpData);
var group:Group = new Group(); //图片信息容器
group.scaleX = 0.5;
group.scaleY = 0.5;
var vgroup:VGroup = new VGroup();
vgroup.paddingTop = 13;
vgroup.name = "vgroup";
var img:Image = new Image(); //拍摄的图片
img.source = bmpData;
img.name = "img";
img.addEventListener("click", function(e:Event):void{
var bmpData:BitmapData = (e.currentTarget as Image).bitmapData;
var showImage:Image = new Image();
showImage.source = bmpData;
var diag:Group = new Group();
diag.graphics.beginFill(0xe5e5e5, 0.5);
diag.graphics.drawRect(0,0,stage.stageWidth*0.99,stage.stageHeight*0.99);
diag.graphics.endFill();
diag.width = body.width;
diag.height = body.height;
var showImageGroup:Group = new Group();
var showImageLeft:int = (diag.width - bmpData.width) / 2;
var showImageTop:int=(diag.height-bmpData.height)/2;
showImage.left = (diag.width - bmpData.width) / 2;
showImage.top = (diag.height-bmpData.height)/2;
showImageGroup.addElement(showImage);
var closeImg:Image = new Image();
closeImg.scaleX = 0.5;
closeImg.scaleY = 0.5;
closeImg.source = imgClass;
closeImg.buttonMode = true;
closeImg.left = showImageLeft+bmpData.width - 6;
closeImg.top = showImageTop - 10;
closeImg.addEventListener("click",function(e:Event):void{
var gp:Group = ((e.currentTarget as Image).parent as Group);
//(gp.parent as Group).removeElement(gp);
body.removeElement(gp.parent as Group);
});
showImageGroup.addElement(closeImg);
diag.addElement(showImageGroup);
body.addElement(diag);
});
vgroup.addElement(img);
var textInput:spark.components.TextInput = new spark.components.TextInput();
textInput.name = "desc";
textInput.text = "图片描述";
textInput.width = 400;
textInput.height = 40;
textInput.styleName = "textInput";
textInput.addEventListener("focusIn",function(e:Event):void{
var txtIpt:TextInput=(e.currentTarget as TextInput);
if(txtIpt.text=="图片描述"){
txtIpt.text = "";
txtIpt.setStyle("color", "#000000");
}
});
textInput.addEventListener("focusOut",function(e:Event):void{
var txtIpt:TextInput=(e.currentTarget as TextInput);
if(txtIpt.text==""){
txtIpt.text = "图片描述";
txtIpt.setStyle("color", "#808080");
}
});
vgroup.addElement(textInput);
group.addElement(vgroup);
var closeImg:Image = new Image();
closeImg.source = imgClass;
closeImg.buttonMode = true;
closeImg.left = 390;
closeImg.top = 0;
closeImg.addEventListener("click",function(e:Event):void{
var gp:Group = ((e.currentTarget as Image).parent as Group);
//(gp.parent as Group).removeElement(gp);
picGroup.removeElement(gp);
});
group.addElement(closeImg);
picGroup.addElement(group);
}
private var cnt:int = 0;
//提交上传事件
private function submit():void{
cnt = 0;
var requestHeader:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
//var url: String = "http://192.168.0.100:8080/webflash/pictureServlet";
var url:String = this.subUrl;
for(var i:int=0;i< picGroup.numElements;i++){
var gp:Group=picGroup.getElementAt(i) as Group;
var vgroup:VGroup= gp.getChildByName("vgroup") as VGroup;
var img:Image = vgroup.getChildByName("img") as Image;
var data:ByteArray = new JPEGEncoder(100).encode(img.bitmapData);
var desc:String=(vgroup.getChildByName("desc") as TextInput).text;
var req: URLRequest = new URLRequest(encodeURI(url + "?desc=" + desc));
req.contentType = "application/octet-stream";
req.method = URLRequestMethod.POST;
req.data = data;
var loader: URLLoader = new URLLoader;
configureListeners(loader); //监听请求事件
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.load(req);
}
}
//配置URLRequest 事件
private function configureListeners(dispatcher:IEventDispatcher):void {
//加载完成事件;
dispatcher.addEventListener(Event.COMPLETE, loaderHandler);
//开始访问事件;
dispatcher.addEventListener(Event.OPEN, loaderHandler);
//加载进度事件;
dispatcher.addEventListener(ProgressEvent.PROGRESS, loaderHandler);
//跨域访问安全策略事件;
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler);
//Http状态事件;
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHandler);
//访问出错事件;
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, loaderHandler);
}
//事件处理函数
private function loaderHandler(event:*):void{
switch(event.type) {
case Event.COMPLETE:
cnt++;
trace("成功: "+(cnt/picGroup.numElements));
//resultStr=urlLoader.data;//请求成功,并取得返回值
break;
case Event.OPEN:
//trace("open: " + event);
break;
case ProgressEvent.PROGRESS:
trace("progress: " + event);
break;
case SecurityErrorEvent.SECURITY_ERROR:
//trace("securityError: " + event);
break;
case HTTPStatusEvent.HTTP_STATUS:
trace("httpStatus: " + event);
//Alert.show(event);
break;
case IOErrorEvent.IO_ERROR:
trace("ioError: " + event);
Alert.show(event);
break;
}
}
]]>
</fx:Script>
<s:Group width="100%" height="100%" horizontalCenter="0" verticalCenter="0" creationComplete="loadCamer()" id="body">
<!--定义一个普通组:整个页面-->
<s:HGroup>
<!--定义一个横向排列组-->
<s:Group width="402" height="370">
<s:VGroup horizontalCenter="0" horizontalAlign="center">
<!--定义一个纵向排列组-->
<s:VideoDisplay id="cameraVD" width="400" height="300"/>
<!--用于装载摄像头-->
<s:HGroup>
<s:Button label="拍照" left="50" top="50" click="takePic()" />
<!--拍照按钮-->
<s:Button label="提交上传" left="80" top="50" click="submit()" />
<!--提交按钮-->
</s:HGroup>
</s:VGroup>
</s:Group>
<s:Scroller width="657" left="500" top="0" right="0" bottom="0" focusEnabled="false">
<!--定义滚动条对象,width=内容宽度+滚动条宽度-->
<s:Group id="picGroup" width="650" height="600" verticalScrollPosition="50">
<!--定义一个普通组:整个图片组-->
<s:layout>
<!--定义布局方式-->
<s:TileLayout requestedColumnCount="3" horizontalGap="2" />
<!--定义布局方式为每行显示3列 超出部分自动换行-->
</s:layout>
</s:Group>
</s:Scroller>
</s:HGroup>
</s:Group>
</s:Application>

Comment list( 0 )

You need to Sign in for post a comment

Help Search