Flex编程摄像头使用实例

本文提供了一个使用Flex进行摄像头操作的示例代码,包括摄像头的开启、关闭及状态侦测等功能。通过实例展示了如何利用Flash Media API实现摄像头视频流的获取与处理。

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

<?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.type,
					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>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值