隐藏

C# 实现IP视频监控(摄像头)画面推送(简单的不能再简单的DEMO)

发布:2019/11/12 13:51:07作者:管理员 来源:本站 浏览次数:1260

最近继续在家休息,在完成上一个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.摄像头驱动、图片抓取(Andrew Kirillov 写的)

5.图片流解析,显示(Andrew Kirillov 写的,也可以直接在Firefox浏览器打开直接显示)

 

运行截图:

1.视频监控端 (Andrew Kirillov 写的 视频源支持N种,当前配置推送频率50毫秒 w=240&h=120)

 

2.视频服务端(我写的 简陋的DEMO 不过实现了功能 嘎嘎)

 

 

下面开始贴核心源码(最近右胳膊有石膏,左手写代码  凑合看吧!):

 

1.建立HTTP服务:


using (HttpListener listerner = new HttpListener())
                {
                    listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
                    listerner.Prefixes.Add("http://+:6666/");
                    
                    //listerner.Prefixes.Add("http://+/");
                    //listerner.Prefixes.Add("http://+:8080/");
                    //listerner.Prefixes.Add("http://+:6666/");
                    //listerner.Prefixes.Add("http://+/video.cgi/");
                    //listerner.Prefixes.Add("http://+:8080/video.cgi/");

                    listerner.Start();
                    Console.WriteLine("WebServer Start Successed.......");
                    while (true)
                    {
                        try
                        {
                            //等待请求连接
                            //没有请求则GetContext处于阻塞状态
                            HttpListenerContext ctx = listerner.GetContext();

                            SendImgService oService = new SendImgService();
                            oService.Ctx = ctx;
                            localsev.NewFrame += new CameraEventHandler(oService.camera_NewFrame);
                            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), oService);

                            //Thread osThread = new Thread(new ThreadStart(oService.ServiceRun));
                            //osThread.Start();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }
                    listerner.Stop();
                    listerner.Close();
                }


2.启动本地视频头,并抓取图片

public void ServiceRun()
        {

            FilterCollection filters = new FilterCollection(FilterCategory.VideoInputDevice);

            if (filters.Count == 0)
                throw new ApplicationException();

            // add all devices to combo
            foreach (Filter filter in filters)
            {
                Console.WriteLine(filter.Name + ":" + filter.MonikerString);
            }
            CaptureDevice localSource = new CaptureDevice();
            localSource.VideoSource = filters[0].MonikerString;

            // create camera
            camera = new Camera(localSource);
            // start camera
            camera.Start();


            // set event handlers
            camera.NewFrame += new CameraEventHandler(camera_NewFrame);

        }

        // On new frame ready
        private void camera_NewFrame(object sender, CameraEventArgs e)
        {
            if (seq == 999)
            {
                seq = 0;
            }
         //   Console.WriteLine("LocalCamService get camera_NewFrame ==> {0}", ++seq);

            // lock
            Monitor.Enter(this);

            if (camera != null)
            {
                camera.Lock();

                // dispose old frame
                if (lastFrame != null)
                {
                    lastFrame.Dispose();
                }
                // draw frame
                if (camera.LastFrame != null)
                {
                    lastFrame = (Bitmap)camera.LastFrame.Clone();
                    // notify client
                    if (NewFrame != null)
                        NewFrame(this, new CameraEventArgs(lastFrame));
                }


                camera.Unlock();
            }

            // unlock
            Monitor.Exit(this);
        }
    }


3.图片推送


public void ServiceRun()
        {
            remoteInfo = ctx.Request.RemoteEndPoint.ToString();
            string intervalstr = ctx.Request.QueryString["i"];
            string widthstr = ctx.Request.QueryString["w"];
            string heightstr = ctx.Request.QueryString["h"];

            if (!string.IsNullOrWhiteSpace(intervalstr))
            {
                interval = int.Parse(intervalstr);
            }
            if (!string.IsNullOrWhiteSpace(widthstr))
            {
                width = int.Parse(widthstr);
            }
            if (!string.IsNullOrWhiteSpace(heightstr))
            {
                height = int.Parse(heightstr);
            }
            Console.WriteLine("Accept one new request:{0},interval:[{1}]", remoteInfo, interval);


            ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
            ctx.Response.ContentType = "multipart/x-mixed-replace; boundary=--BoundaryString";

            string rspheard = "--BoundaryString\r\nContent-type: image/jpg\r\nContent-Length: {0}\r\n\r\n";
            string strrn = "\r\n";

            using (Stream stream = ctx.Response.OutputStream)
            {
                while (true)
                {
                    Thread.Sleep(interval);

                    try
                    {
                        // lock
                        Monitor.Enter(this);

                        if (newFrame == null)
                        {
                            continue;
                        }
                        //得到一个ms对象
                        byte[] imageBuffer;
                        using (MemoryStream ms = new MemoryStream())
                        {

                            //newFrame = (Bitmap)GetThumbnail(newFrame, width, height);
                            //将图片保存至内存流
                            newFrame.Save(ms, ImageFormat.Jpeg);

                            rspheard = string.Format(rspheard, ms.Length);

                            byte[] heardbuff = Encoding.ASCII.GetBytes(rspheard);
                            stream.Write(heardbuff, 0, heardbuff.Length);

                            imageBuffer = new byte[512];
                            int c;
                            ms.Position = 0;
                            //通过内存流读取到imageBytes
                            while ((c = ms.Read(imageBuffer, 0, 512)) > 0)
                            {
                                stream.Write(imageBuffer, 0, c);
                            }
                            byte[] rnbuff = Encoding.ASCII.GetBytes(strrn);
                            stream.Write(rnbuff, 0, rnbuff.Length);

                            Console.WriteLine("[{0}] : SendImgService send NewFrame", remoteInfo);

                        }

                        // stream.Flush();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);

                        break;
                    }
                    finally
                    {
                        // unlock
                        Monitor.Exit(this);
                    }
                }
            }
            Console.WriteLine("[{0}] : 线程结束...", remoteInfo);
        }



附件:(刚会传文件,还不知道怎么插入链接,谁教我下?)

可运行程序:

http://files.cnblogs.com/files/ryhan/%E7%9B%91%E6%8E%A7%E5%8F%8A%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%8F%AF%E8%BF%90%E8%A1%8C%E7%A8%8B%E5%BA%8F.zip

 

监控端源码:

http://files.cnblogs.com/files/ryhan/%E7%9B%91%E6%8E%A7%E7%AB%AF%E6%BA%90%E7%A0%81.zip

 

服务端源码:

http://files.cnblogs.com/files/ryhan/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%BA%90%E7%A0%81%28%E5%8D%9A%E5%AE%A2%E4%B8%AD%E5%AE%9E%E7%8E%B0%29.zip

 

PS:

1.建议用VS2010打开

2.监控端cv_src目录下cv3.sln为监控客户端程序,用来看画面,cameras.config配置视频源

3.HttpImageStream是本次实现的图片推送Demo 效率上估计有点问题。

4.运行HttpImageStream时,建议电脑上有摄像头,不然估计会无法启动。