Flash/Flex学习笔记(2):捕获摄像头
Flash中使用摄像头,个人感觉比silverlight要更容易
时间轴第一帧上敲以下代码就可以了:
//import fl.controls.Label; //var camera:Camera = Camera.getCamera(); //trace(camera==null?"未检测到摄像头":"已安装了摄像头"); var W:int=stage.stageWidth;//场景的原始宽度 var H:int=stage.stageHeight;//场景的原始高度 stage.scaleMode=StageScaleMode.NO_SCALE;//舞台禁止缩放,但是好象没效果? var video:Video; var camera:Camera=Camera.getCamera(); if (camera==null) { trace("未检测到摄像头!"); lbl1.text="未检测到摄像头!"; btnCapture.visible=false; } else { camera.addEventListener(ActivityEvent.ACTIVITY,activityHander); video=new Video(camera.width,camera.height); trace("视频宽度:" + camera.width + ",视频高度:" + camera.height); video.attachCamera(camera); //定位到水平中心点 video.x = (W - video.width) /2; video.y=10; addChild(video);//加载到当前舞台中 btnCapture.x = (W - btnCapture.width) /2; btnCapture.y=video.y+video.height+5; btnCapture.addEventListener(MouseEvent.CLICK,captureImage); } function activityHander(e:ActivityEvent):void { trace("activityHander:" + e); } var dp:DataProvider = new DataProvider(); function captureImage(e:MouseEvent):void { //trace("你点击了按钮"); var bit:BitmapData=new BitmapData(video.width,video.height); bit.draw(video); var bmp:Bitmap=new Bitmap(bit); //bmp.y=btnCapture.y+btnCapture.height+10; //bmp.x = (W - bmp.width) /2; var d:Date = new Date(); dp.addItem({label:"截图" + d.getFullYear() + d.getMonth() + d.getDay() + d.getHours() + d.getMinutes() + d.getSeconds() + d.getMilliseconds(),source:bmp}); //addChild(bmp); tilelst1.dataProvider = dp; } stop();
运行效果图:
源代码下载:http://cid-2959920b8267aaca.skydrive.live.com/self.aspx/Flash/GetCamera.rar