加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex编程摄像头使用实例

发布时间:2020-12-15 04:37:44 所属栏目:百科 来源:网络整理
导读:实例包含摄像头打开、关闭、侦测等功能,源代码如下: ?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="9

实例包含摄像头打开、关闭、侦测等功能,源代码如下:

<?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 flash.media.Video;
			
			import mx.events.FlexEvent;
			import mx.utils.StringUtil;
			
			private var _video:Video = null;
			private var camera:Camera = null;	
			private var timerid:uint;
			private var isBusyCamera:Boolean = false;
			private var times:uint=0;
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				if(videoDisplay.numChildren == 2){
					trace(videoDisplay.numChildren);					
					
					if(videoDisplay.numChildren == 2){
						videoDisplay.removeChildAt(1);
					}
					
					trace(videoDisplay.numChildren);
					
					_video.clear();
					_video.attachCamera(null);
					_video = null;
					camera = Camera.getCamera(null); 
					camera = null;
				}
			}
			
			protected function button2_clickHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				trace("Camera.isSupported: ",Camera.isSupported);
				trace("Camera.names.length",Camera.names.length);
				if(camera == null){					
					if(Camera.isSupported && Camera.names.length >0){
						camera = Camera.getCamera();
					}					
				}
				trace(camera);
				if (camera) {				
					camera.addEventListener(ActivityEvent.ACTIVITY,camera_activity);
					camera.addEventListener(StatusEvent.STATUS,camera_status);
					if(_video == null){
						_video = new Video();
					}
					_video.attachCamera(camera);
					
					timerid=setInterval(callback,2000);
					
					_video.width = videoDisplay.width;
					_video.height = videoDisplay.height;
					trace(videoDisplay.numChildren);
					videoDisplay.addChild(_video);
					trace(videoDisplay.numChildren);
				} else {
					trace("You don't seem to have a camera.");
				}
			}
			
			private function callback():void 
			{ 
				trace("currentFPS=" + camera.currentFPS.toString());
				
				if (camera.currentFPS > 0) 
				{ 
					//视频设备可用 
					clearInterval(this.timerid); 
					this.isBusyCamera=false; 
					trace("摄像头正常"); 
				} 
				else 
				{ 
					times++; 
					trace("times=" + times.toString());
					
					if (times > 2) 
					{ 
						//视频设备忙 
						clearInterval(timerid); 
						this.isBusyCamera=true; 
						trace("摄像头被占用"); 
					} 
				} 
			}
			
			private function camera_activity(evt:ActivityEvent):void {
				var str:String = "[{0}] activating:{1}n";
				textArea.text += StringUtil.substitute(str,evt.type,evt.activating);
			}
			
			private function camera_status(evt:StatusEvent):void {
				var str:String = "[{0}] code:'{1}',level:'{2}'n";
				textArea.text += StringUtil.substitute(str,evt.code,evt.level);
				switch (evt.code) {
					case "Camera.Muted":
						trace("User denied access to camera.");
						break;
					case "Camera.Unmuted":
						trace("User allowed access to camera.");
						break;
				}
			}
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	
	
	<s:VGroup>
		<s:Panel x="53" y="26" width="394" height="290" title="My Camera">
			<s:VideoDisplay id="videoDisplay" x="10" y="10" width="371" height="238"/>			
		</s:Panel>
		<s:Button label="停止" click="button1_clickHandler(event)"/>
		<s:Button label="开始" click="button2_clickHandler(event)"/>
		<s:TextArea id="textArea" width="100%" height="{videoDisplay.height}" editable="false"
					verticalScrollPolicy="on"/>
	</s:VGroup>
	
</s:Application>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读