不好意思,让大家久等了!今天我要实现的一个功能是异步录像!先讲下为什么要提出这样一个问题----假设被监控区域出现了异常(一个陌生人进入),那么本监控系统就要对这一段时间发生的一切进行录制,但此同时我们又在另一端观测这一段时间的实时视频,如何才能做到这一点-----既能录像又不打断我们观测实时视频?

 

解决原理:我们可以制作发布两个视频流,一个视频流的作用仍然是我们上篇文章所讲到的“把实时视频流传送到FMS服务器”,另外一个视频流的作用则是播放此实时视频流,假如发生异常则利用此视频流进行录像,这样就解决了第一段落提出的问题。 下面的代码可以覆盖上篇文章提到的server.mxml

 


Xml代码

 
   
 
1. <?xml version="1.0" encoding="utf-8"?>
2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="571">
3.     <mx:Script>  
4. <![CDATA[ 
5.             import mx.core.UIComponent; 
6.             import flash.media.Microphone ; 
7.             import mx.controls.Alert ; 
8.             import flash.display.Graphics ; 
9.             //测试网络可行性 
10.             private var conne:NetConnection = new NetConnection() ; 
11.             private function xianshi():void { 
12.                 //必须  在AS3.0中默认的ObjectEncoding为AMF3,但是FMS不支持AMF3,所以 
13.                 //要显示的声明为AMF0 
14.                 conne.objectEncoding = ObjectEncoding.AMF0 ; 
15.                 conne.connect("rtmp://127.0.0.1/example") ; 
16.                 conne.addEventListener(NetStatusEvent.NET_STATUS,chuli) ; 
17.             } 
18.              
19.             private var nnnns:NetStream = null ;//专门用来向连接到服务器的客户端提供共享视频流 
20.             private var nnnnns:NetStream = null ;//分支视频流 专门用来录像 
21.             private var time:String = null ;//定义时间寄存器 
22.             private function chuli(e:NetStatusEvent):void { 
23.                 var result:String = e.info.code ; 
24.                 switch(result) { 
25.                     case "NetConnection.Connect.Success": 
26.                     vd.attachCamera(Camera.getCamera()) ; 
27.                     nnnns = new NetStream(conne) ; 
28.                     nnnns.attachAudio(Microphone.getMicrophone()) ; 
29.                     nnnns.attachCamera(Camera.getCamera()) ; 
30.                     //播放server端的视频流(实时视频流,供client端播放) 
31.                     nnnns.publish("wwww","live") ; 
32.                     nnnnns = new NetStream(conne) ; 
33.                     nnnnns.attachAudio(Microphone.getMicrophone()) ; 
34.                     nnnnns.attachCamera(Camera.getCamera()) ; 
35.                     time = new Date().getTime().toString() ; 
36.                     nnnnns.publish(time,"live") ;//记录当前时间,以此作为标志位,发布到FMS 
37.                     nnnnns.addEventListener(NetStatusEvent.NET_STATUS,netStreamHandler) ; 
38.                     break; 
39.                     case "NetStream.Play.StreamNotFound": 
40.                     Alert.show("失败") ; 
41.                     break; 
42.                     default : 
43.                     Alert.show("缺省") ; 
44.                     break ; 
45.                 } 
46.             } 
47.             //停止播放 
48.             private function tingzhi():void { 
49.                 nnnns.close() ; 
50.                 nnnnns.close() ; 
51.                 conne.close() ; 
52.             } 
53.              
54.             private function bofang():void { 
55.                 nnnns = new NetStream(conne) ; 
56.                 var v:Video = new Video() ; 
57.                 v.attachNetStream(nnnns) ; 
58.                 v.width = 276 ; 
59.                 v.height = 202 ; 
60.                 vv.addChild(v) ; 
61.                 nnnns.play("1199001232984") ; 
62.             } 
63.             private function luxiang():void { 
64.                 time = new Date().getTime().toString() ; 
65.                 nnnnns.publish(time,"record") ; 
66.             } 
67.             //停止录像 
68.             private function stopLuxiang():void { 
69.                 nnnnns.close() ; 
70.             } 
71.             //NetStream事件处理器 
72.             private function netStreamHandler(e:NetStatusEvent):void { 
73.                 var s:String = e.info.code ; 
74.                 switch(s) {//停止录像时,再记录当前时间,并以此为标志位,发不到FMS 
75.                     case "NetStream.Record.Stop" : 
76.                     nnnnns = new NetStream(conne) ; 
77.                     nnnnns.attachAudio(Microphone.getMicrophone()) ; 
78.                     nnnnns.attachCamera(Camera.getCamera()) ; 
79.                     time = new Date().getTime().toString() ; 
80.                     nnnnns.publish(time,"live") ; 
81.                     break ; 
82.                 } 
83.             } 
84.             //拍照功能实现 
85.             private function paizhao():void { 
86.                 var bmp:BitmapData = new BitmapData(vd.width,vd.height,true,0) ; 
87.                 bmp.draw(vd) ; 
88.                 var bitMap:Bitmap = new Bitmap(bmp) ; 
89.                 image.source = bitMap ; 
90.             } 
91.              
92.         ]]>
93.     </mx:Script>  
94.     <mx:VideoDisplay x="0" y="0" width="264" height="213" id="vd"/>
95.        
96.     <mx:Button label="连接服务器" click="xianshi()" x="10" y="247">
97.     </mx:Button>  
98.     <mx:Button click="tingzhi()" x="99" y="247" width="125" height="21" label="断开与服务器连接">
99.            
100.     </mx:Button>  
101.     <mx:VideoDisplay x="285" y="11" width="276" height="202" id="vv"/>
102.     <mx:Button x="458" y="247" label="播放" click="bofang()"/>
103.     <mx:Button x="302" y="247" label="开始录像" click="luxiang()"/>
104.     <mx:Button x="380" y="247" label="停止录像" click="stopLuxiang()"/>
105.     <mx:Button x="229" y="247" label="拍照" click="paizhao()"/>
106.     <mx:Image x="45" y="314" width="264" height="213" id="image"/>
107. </mx:Application>  
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="571">
	<mx:Script>
		<![CDATA[
			import mx.core.UIComponent;
			import flash.media.Microphone ;
			import mx.controls.Alert ;
			import flash.display.Graphics ;
			//测试网络可行性
			private var conne:NetConnection = new NetConnection() ;
			private function xianshi():void {
				//必须  在AS3.0中默认的ObjectEncoding为AMF3,但是FMS不支持AMF3,所以
				//要显示的声明为AMF0
				conne.objectEncoding = ObjectEncoding.AMF0 ;
				conne.connect("rtmp://127.0.0.1/example") ;
				conne.addEventListener(NetStatusEvent.NET_STATUS,chuli) ;
			}
			
			private var nnnns:NetStream = null ;//专门用来向连接到服务器的客户端提供共享视频流
			private var nnnnns:NetStream = null ;//分支视频流 专门用来录像
			private var time:String = null ;//定义时间寄存器
			private function chuli(e:NetStatusEvent):void {
				var result:String = e.info.code ;
				switch(result) {
					case "NetConnection.Connect.Success":
                    vd.attachCamera(Camera.getCamera()) ;
					nnnns = new NetStream(conne) ;
					nnnns.attachAudio(Microphone.getMicrophone()) ;
					nnnns.attachCamera(Camera.getCamera()) ;
					//播放server端的视频流(实时视频流,供client端播放)
					nnnns.publish("wwww","live") ;
					nnnnns = new NetStream(conne) ;
					nnnnns.attachAudio(Microphone.getMicrophone()) ;
					nnnnns.attachCamera(Camera.getCamera()) ;
					time = new Date().getTime().toString() ;
					nnnnns.publish(time,"live") ;//记录当前时间,以此作为标志位,发布到FMS
					nnnnns.addEventListener(NetStatusEvent.NET_STATUS,netStreamHandler) ;
                    break;
                    case "NetStream.Play.StreamNotFound":
                    Alert.show("失败") ;
                    break;
                    default :
                    Alert.show("缺省") ;
                    break ;
				}
			}
			//停止播放
			private function tingzhi():void {
				nnnns.close() ;
				nnnnns.close() ;
				conne.close() ;
			}
			
			private function bofang():void {
				nnnns = new NetStream(conne) ;
				var v:Video = new Video() ;
				v.attachNetStream(nnnns) ;
				v.width = 276 ;
				v.height = 202 ;
				vv.addChild(v) ;
				nnnns.play("1199001232984") ;
			}
			private function luxiang():void {
				time = new Date().getTime().toString() ;
				nnnnns.publish(time,"record") ;
			}
			//停止录像
			private function stopLuxiang():void {
			    nnnnns.close() ;
			}
			//NetStream事件处理器
			private function netStreamHandler(e:NetStatusEvent):void {
				var s:String = e.info.code ;
				switch(s) {//停止录像时,再记录当前时间,并以此为标志位,发不到FMS
					case "NetStream.Record.Stop" :
					nnnnns = new NetStream(conne) ;
					nnnnns.attachAudio(Microphone.getMicrophone()) ;
					nnnnns.attachCamera(Camera.getCamera()) ;
					time = new Date().getTime().toString() ;
					nnnnns.publish(time,"live") ;
					break ;
				}
			}
			//拍照功能实现
			private function paizhao():void {
				var bmp:BitmapData = new BitmapData(vd.width,vd.height,true,0) ;
				bmp.draw(vd) ;
				var bitMap:Bitmap = new Bitmap(bmp) ;
				image.source = bitMap ;
			}
			
		]]>
	</mx:Script>
	<mx:VideoDisplay x="0" y="0" width="264" height="213" id="vd"/>
	
	<mx:Button label="连接服务器" click="xianshi()" x="10" y="247">
	</mx:Button>
	<mx:Button click="tingzhi()" x="99" y="247" width="125" height="21" label="断开与服务器连接">
		
	</mx:Button>
	<mx:VideoDisplay x="285" y="11" width="276" height="202" id="vv"/>
	<mx:Button x="458" y="247" label="播放" click="bofang()"/>
	<mx:Button x="302" y="247" label="开始录像" click="luxiang()"/>
	<mx:Button x="380" y="247" label="停止录像" click="stopLuxiang()"/>
	<mx:Button x="229" y="247" label="拍照" click="paizhao()"/>
	<mx:Image x="45" y="314" width="264" height="213" id="image"/>
</mx:Application>


请大家注意看下,写注释的地方,那些是本篇文章的核心!(因为代码比较多,因此本人大致做了些删改),有什么不明白的可以留言,本人将会为大家解答!

 

下篇文章就是本视频监控软件最核心的地方----图像识别,至今为止,本人仍在探寻较为高效的算法。哪位兄弟对此方面的图像识别算法有研究,可以共同交流下!