最近繼續在家休息,在完成上一個Python抓取某音樂網站爬蟲后,琢磨著實現一個基于HTTP推送的 IP視頻監控,比如外出的時候,在家里
開啟一個監控端(攝像頭+服務端),可以看到實時畫面,如果再加上自動告警,就更好了。公網訪問需要在 路由器上設置 花生殼+端口轉發。
計劃在退休的安卓手機上實現這IP視頻監控軟件,雖然應用市場一大堆別人寫好的軟件,不過我覺得吧,既然是程序員,自己敲代碼實現的軟件會
更有成就感??紤]到需要先驗證下方案的可行性,我用比較熟悉的C# 控制臺實現了一個DEMO。
設想的方案:
1.實現一個簡單HTTP服務器,用來接受請求并啟動一個線程處理圖片流的推送功能
2.開發一個實時抓取圖片的線程,并將圖片交給HTTP推送線程
3.HTTP的請求URL參數中 附帶推送頻率、圖片高度和寬度
4.使用一個IP攝像頭監控端(或者Firefox瀏覽器),實時查看視頻畫面
5.循環錄制視頻(未實現)
6.對畫面進行監控告警(未實現)
核心技術點:
1.HttpListener (HTTP.SYS)
2.HTTP :multipart/x-mixed-replace;
3.線程同步、委托、事件
4.攝像頭驅動、圖片抓?。ˋndrewKirillov 寫的)
5.圖片流解析,顯示(AndrewKirillov 寫的,也可以直接在Firefox瀏覽器打開直接顯示)
運行截圖:
1.視頻監控端 (AndrewKirillov 寫的 視頻源支持N種,當前配置推送頻率50毫秒 w=240&h=120)
2.視頻服務端(我寫的 簡陋的DEMO 不過實現了功能 嘎嘎)
下面開始貼核心源碼(最近右胳膊有石膏,左手寫代碼 湊合看吧!):
1.建立HTTP服務:
1 using (HttpListener listerner = new HttpListener()) 2 { 3 listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份驗證 Anonymous匿名訪問 4 listerner.View Code2.啟動本地視頻頭,并抓取圖片
1 public void ServiceRun() 2 { 3 4 FilterCollection filters = new FilterCollection(FilterCategory.VideoInputDevice); 5 6 if (filters.Count == 0) 7 throw new applicationException(); 8 9 // add all devices to combo10 foreach (Filter filter in filters)11 {12 Console.WriteLine(filter.Name + ":" + filter.MonikerString);13 }14 CaptureDevice localSource = new CaptureDevice();15 localSource.VideoSource = filters[0].MonikerString;16 17 // create camera18 camera = new Camera(localSource);19 // start camera20 camera.Start();21 22 23 // set event handlers24 camera.NewFrame += new CameraEventHandler(camera_NewFrame);25 26 }27 28 // On new frame ready29 private void camera_NewFrame(object sender, CameraEventArgs e)30 {31 if (seq == 999)32 {33 seq = 0;34 }35 // Console.WriteLine("LocalCamService get camera_NewFrame ==> {0}", ++seq);36 37 // lock38 Monitor.Enter(this);39 40 if (camera != null)41 {42 camera.Lock();43 44 // dispose old frame45 if (lastFrame != null)46 {47 lastFrame.Dispose();48 }49 // draw frame50 if (camera.LastFrame != null)51 {52 lastFrame = (Bitmap)camera.LastFrame.Clone();53 // notify client54 if (NewFrame != null)55 NewFrame(this, new CameraEventArgs(lastFrame));56 }57 58 59 camera.Unlock();60 }61 62 // unlock63 Monitor.Exit(this);64 }65 }View Code3.圖片推送
1 public void ServiceRun() 2 { 3 remoteInfo = ctx.Request.RemoteEndPoint.ToString(); 4 string intervalstr = ctx.Request.QueryString["i"]; 5 string widthstr = ctx.Request.QueryString["w"]; 6 string heightstr = ctx.Request.QueryString["h"]; 7 8 if (!string.IsNullOrWhiteSpace(intervalstr)) 9 {10 interval = int.Parse(intervalstr);11 }12 if (!string.IsNullOrWhiteSpace(widthstr))13 {14 width = int.Parse(widthstr);15 }16 if (!string.IsNullOrWhiteSpace(heightstr))17 {18 height = int.Parse(heightstr);19 }20 Console.WriteLine("Accept one new request:{0},interval:[{1}]", remoteInfo, interval);21 22 23 ctx.Response.StatusCode = 200;//設置返回給客服端http狀態代碼24 ctx.Response.ContentType = "multipart/x-mixed-replace; boundary=--BoundaryString";25 26 string rspheard = "--BoundaryString/r/nContent-type: image/jpg/r/nContent-Length: {0}/r/n/r/n";27 string strrn = "/r/n";28 29 using (Stream stream = ctx.Response.OutputStream)30 {31 while (true)32 {33 Thread.Sleep(interval);34 35 try36 {37 // lock38 Monitor.Enter(this);39 40 if (newFrame == null)41 {42 continue;43 }44 //得到一個ms對象45 byte[] imageBuffer;46 using (MemoryStream ms = new MemoryStream())47 {48 49 //newFrame = (Bitmap)GetThumbnail(newFrame, width, height);50 //將圖片保存至內存流51 newFrame.Save(ms, ImageFormat.Jpeg);52 53
新聞熱點
疑難解答