65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 C#.NET 将实时 IP 摄像头流作为 Flash 视频广播到您的网站

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (25投票s)

2014 年 10 月 1 日

CPOL

11分钟阅读

viewsIcon

96527

downloadIcon

14039

我将展示如何使用 C# 将实时 IP 摄像头流集成到您的网站中,以便能够监控视频监控区域。

目录

IP 监控系统简介

纵观当今的监控技术,可以得出结论,用 IP(Internet Protocol)系统取代模拟 CCTV(闭路电视)已成为一种强劲的行业趋势。考虑到保护家庭、工作场所、财产和人身安全在某种程度上是不可避免的,我对构建定制化的 IP 摄像头解决方案越来越感兴趣。这就是为什么我将通过这篇文章概述 IP 摄像头编程中最重要的领域之一。

虽然任何 IP 摄像头都允许您在浏览器中查看其图像,但它无法将其集成到您自己的网站中。在本文中,我将展示如何将实时 IP 摄像头流集成到您的网站中,以便能够监控视频监控区域。

关于 IP 摄像头

要通过安装 IP 监控系统来保护您的财产,必不可少的是高质量的安全 IP 摄像头。IP 摄像头(也称为“网络摄像头”)是一种联网的数字视频摄像头,通过 Internet Protocol 传输数据。IP 摄像头最常用于 IP 监控,这是闭路电视 (CCTV) 的数字化和网络化版本。IP 摄像头使用大多数局域网所用的 Internet Protocol 以数字形式通过网络传输视频。它可以经由公共 Internet 传输,让您可以通过 PC 或智能手机上的任何宽带连接查看您的摄像头。

与模拟技术相比,IP 摄像头具有多项优势。最大的优势之一是 IP 摄像头允许从任何位置进行远程管理。IP 摄像头能够轻松地将图像和视频发送到任何有 Internet 连接的地方。逐行扫描技术可以从视频中提取更高质量的图像,尤其适合移动目标。其他一些优势和功能包括:数字变焦、可调节的帧速率和分辨率以满足特定需求、双向通信、更低的布线要求、在检测到可疑活动时发送警报的能力、对智能视频的支持等。

在购买 IP 摄像头之前,请花点时间了解 IP 摄像头市场,并根据您的需求选择最适合的摄像头。无论其用途如何,可靠性和图像质量是区分好摄像头和坏摄像头的关键特征。当然,它们自带的功能决定了它们的可用性。(可用性决定了未来可能实现什么类型的开发,因为如果您的摄像头不支持所需功能,那么您将无法自行实现它。)您可以在 Internet 上找到许多比较和评论,以帮助您选择最佳设备。例如,下图根据 Top10thebest 的评论展示了一些功能最强大的 IP 网络摄像头。

一些功能最强大的 IP 网络摄像头 - 来源:1。

为了解决 IP 视频监控标准化问题,开放网络视频接口论坛 (ONVIF) 行业组织于 2008 年成立。

什么是 ONVIF 技术

ONVIF 是一种基于 IP 的安全标准。引用 ONVIF 官方网站的话:“ONVIF (Open Network Video Interface Forum) 是一个开放的行业论坛,致力于为基于 IP 的物理安全产品的接口开发全球标准。” ONVIF 的成立是为了代表成员制定一个全球开放标准,用于基于 IP 的物理安全产品的接口。(部分成员包括:AXIS Communications、Bosch、Canon、Cisco、LG、Panasonic、Samsung、Sony、Synology 等。)

在此基础上,ONVIF 标准化了基于 IP 的物理安全之间的通信;在基于 IP 的物理安全产品之间实现了互操作性,无论制造商如何,并且对所有公司和组织开放。

ONVIF 规范定义了一个通用的协议,用于网络视频设备之间的信息交换,包括自动设备发现、视频流和智能元数据。

ONVIG 徽标 - 来源:4。

入门

现在,在研究了 IP 监控系统的技术背景后,是时候开始您的 IP 摄像头项目了。要将实时 IP 摄像头流广播到您的网站,您将需要以下内容:(1) Web 服务器,(2) ONVIF SDK,(3) C# 编程 IDE。好的,现在让我们看看您需要对之前的三个软件做什么。

1. Web 服务器安装

考虑到此应用程序将能够在网站上显示您的 IP 摄像头图像,Web 服务器是必不可少的。为了在浏览器中访问我的网站,我使用了 Apache,可以从 Apache 官方网站下载。安装 Web 服务器后,您需要建立 IP 摄像头与 Web 服务器之间的连接。为此,最简单的方法是下载我的 FlashClient.zip 源代码,然后将 bin-debug 文件夹中的所有内容复制并粘贴到 Apache 的 WWW 文件夹中。

2. ONVIF SDK 安装

为了实现任何类型的 IP 摄像头软件,都需要一个支持 ONVIF 的 IP 摄像头 SDK。为此,我使用了 Ozeki Camera SDK,它基于 ONVIF 标准,因此与其他基于标准的产品兼容。您可以从其 官方网站下载 SDK。要使用此 SDK,目标计算机体系结构和操作系统上需要运行 .NET Framework 组件。因此,还需要下载并 安装 Microsoft .NET Framework 4 Web 安装程序包。

3. 在 Visual Studio 中创建控制台应用程序

正如我一直在 C# 编程中使用 Microsoft Visual Studio 一样,我也在这个项目中使用此 IDE。您可以从 Microsoft 官方网站获取。由于在此应用程序的情况下,不需要在 Windows Forms 应用程序中显示 IP 摄像头图像,因此创建一个控制台应用程序就足够了。为此,您需要通过单击“新建项目”图标来创建一个新的 Visual Studio 项目。现在,您需要将 DLL 文件(由 ONVIF SDK 提供)作为引用添加到您的项目中。现在只剩下一个设置需要完成,以避免任何问题:单击“项目”菜单,然后选择 [您的应用程序名称] 属性元素。在右键单击第一个选项卡后,有一个名为“目标框架”的选项。请确保此设置为“ .NET Framework 4.0”。

好的,现在您的系统已准备好构建您自己的 IP 摄像头项目。

后端开发

现在我将展示所有必需的类,以便能够在您的浏览器中显示 IP 摄像头的图像。因此,将介绍三个类:MyMediaGateway.csMyClient.csProgram.cs

您可以在下面看到 MyMediaGateway 类,它允许您的应用程序在控制台应用程序和 Web 服务器之间建立连接。通过这种方式,您将能够将数据从您的网站发送到您的 C# 应用程序。正如您下面看到的,当应用程序运行时,可以将摄像头的捕获图像转发到您网站的 HTML 界面。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ozeki.MediaGateway;
using Ozeki.Media.IPCamera;
using Ozeki.MediaGateway.Config;
using Ozeki.MediaGateway.Service;

namespace Display_IP_Camera_Stream_On_Website
{
    public class MyMediaGateway : MediaGateway
    {
        private Dictionary<IClient, MyClient> clients;
        private IStreamService _streamService;

        public event EventHandler<EventArgs> ClientCountChange;

        public MyMediaGateway(MediaGatewayConfig config)
            : base(config)
        {
            clients = new Dictionary<IClient, MyClient>();
        }

        #region MediaGateway methods

        public override void OnStart()
        {
            base.OnStart();

            _streamService = GetService<IStreamService>();

            Console.WriteLine("MediaGateway started.");
        }

        public override void OnClientConnect(IClient client, object[] parameters)
        {
            Console.WriteLine(client.RemoteAddress + " client connected to the server with " + client.ClientType);
            if (clients.ContainsKey(client)) return;
            clients.Add(client, new MyClient(client, _streamService));
            ClientChange();
        }

        public override void OnClientDisconnect(IClient client)
        {
            var disconnectedClient = GetClient(client);
            if (disconnectedClient == null)
                return;

            disconnectedClient.DisconnectCamera();
            clients.Remove(client);
            ClientChange();
        }

        private void ClientChange()
        {
            var handler = ClientCountChange;
            if (handler != null)
                ClientCountChange(this, new EventArgs());
        }

        #endregion

        #region Client invokes

        public void CameraConnect(IClient client, string uri)
        {
            Console.WriteLine(client.RemoteAddress + " client IP camera connect " + uri);

            var myClient = GetClient(client);
            if (myClient == null)
                return;

            myClient.ConnectCamera(uri);
        }

        public void CameraDisconnect(IClient client)
        {
            Console.WriteLine(client.RemoteAddress + " client IP camera disconnect.");

            var myClient = GetClient(client);
            if (myClient == null)
                return;

            myClient.DisconnectCamera();
        }

        #endregion

        MyClient GetClient(IClient client)
        {
            return !clients.ContainsKey(client) ? null : clients[client];
        }
    }
}

正如上一个和下一个代码片段所示,摄像头图像传输基于 MyClient 类,该类由 MyMediaGateway 类使用。您可以看到下面的 MyClient 类,它包含构建 IP 摄像头连接并将其图像流式传输到您的 C# 应用程序所需的所有必要方法和成员。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.IPCamera;
using Ozeki.Media.MediaHandlers.IPCamera;
using Ozeki.MediaGateway;
using Ozeki.MediaGateway.Service;

namespace Display_IP_Camera_Stream_On_Website
{
    class MyClient
    {
        private IClient _client;
        private IStreamService _streamService;
        private IMediaStream _mediaStream;
        private MediaGatewayVideoReceiver _videoReceiver;
        private MediaConnector _connector;
        private IIPCamera _camera;

        public MyClient(IClient client, IStreamService streamService)
        {
            this._client = client;
            this._streamService = streamService;
            _connector = new MediaConnector();
        }

        internal void ConnectCamera(string uri)
        {
            // Connect to the camera
            if (String.IsNullOrEmpty(uri)) return;

            _camera = IPCameraFactory.GetCamera(uri, "admin", "admin");
            _camera.Start();

            if (_camera == null)
            {
                NotifyCameraStateChanged(IPCameraState.Error);
                return;
            }
            // Notify the client about the camera state
            NotifyCameraStateChanged(IPCameraState.Connected);

            // Start the stream
            var playStreamName = Guid.NewGuid().ToString();
            _mediaStream = _streamService.CreateStream(playStreamName);
            _videoReceiver = new MediaGatewayVideoReceiver(_mediaStream);

            _connector.Connect(_camera.VideoChannel, _videoReceiver);

            // Notify the client about the stream name
            OnPlayRemoteStream(playStreamName);
        }

        internal void DisconnectCamera()
        {
            if (_camera == null) return;
            _camera.Disconnect();

            _connector.Disconnect(_camera.VideoChannel, _videoReceiver);

            _videoReceiver.Dispose();
            _mediaStream.Close();

            NotifyCameraStateChanged(IPCameraState.Disconnected);
        }

        private void NotifyCameraStateChanged(IPCameraState state)
        {
            try
            {
                _client.InvokeMethod("OnCameraStateChanged", state);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        void OnPlayRemoteStream(string streamName)
        {
            try
            {
                _client.InvokeMethod("OnPlayRemoteStream", streamName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

最后,让我们看一下控制整个过程的 Program.cs 类。您可以在下面看到,在 Main 部分,您需要创建一个 MediaGateway 类型的对象并添加 FlashConfig 元素。(上面提到的 Flash 内容包含了所有其余的 C# 源代码。)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ozeki.Media.MediaHandlers.IPCamera;
using Ozeki.Media.MediaHandlers.IPCamera.Types;
using Ozeki.Media.Video;
using Ozeki.Media.Video.Controls;
using Ozeki.MediaGateway.Config;

namespace Display_IP_Camera_Stream_On_Website
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = new MediaGatewayConfig();
            config.AddConfigElement(new FlashConfig { ServiceName = "IPCameraServer" });

            MyMediaGateway _mediaGateway = new MyMediaGateway(config);
            _mediaGateway.Start();
            Console.Read();
        }
    }
}

为避免任何问题,我建议您将 System.Configuration.dll 添加为 Visual Studio 项目中的新引用。它提供了对客户端应用程序配置文件(.config 文件)的访问。

前端开发 (GUI)

好的,现在让我们看看使用我的代码示例可以实现的用户图形界面。在成功构建并运行项目后,您可以在浏览器中看到类似下图的内容。它演示了如何在 Chrome 浏览器中通过 Internet 监控您的工作站是多么简单。如您所见,即使我不在家,我也能够查看我的桌子和我的 PC。

ONVIF IP 摄像头流式传输到网站- 来源:自制

如何使用 C#.NET 在网络广播期间捕获 IP 摄像头的静态图像

作为一项小补充,在本节中,我将展示如何从您广播的 IP 摄像头捕获静态图像到网站。由于此功能,您将能够通过网络广播进行摄像头快照,并在您的网站上显示此图像。在您的网站上捕获的快照将自动保存并存储到您的 Web 服务器安装的文件夹中。

为了使此功能的实现更容易,我附加了一个新源代码,该源代码是专门为此功能创建的。因此,如果您对这个解决方案感兴趣,请下载 SnapshotDuringWebBroadcasting.zip 文件,该文件(除了源代码之外)包含一个新的基于 Flash 的内容。下载后,您需要用此内容覆盖以前的文件。

好的,是时候看看如何使用 C#.NET 在网络广播期间捕获静态图像了。首先,请在 Visual Studio 中以通常方式向您的“引用”中添加一个新项:右键单击“引用”,然后单击“添加引用”。在此,单击“.NET”选项卡,然后选择并添加 System.Drawing.dll 文件到您的引用中。

现在让我们看看为实现此功能需要进行的更改。在 MyMediaGateway.cs 类中,您需要添加一个路径,如下所示。它使您能够轻松找出捕获后快照的位置。在此修改之后,MyMediaGateway 类应以以下部分开头:

private Dictionary<IClient, MyClient> clients;
        private IStreamService _streamService;
        private string path;

        public event EventHandler<EventArgs> ClientCountChange;

        public MyMediaGateway(MediaGatewayConfig config, string path)
            : base(config)
        {
            clients = new Dictionary<IClient, MyClient>();
            this.path = path;
        }

完成上述修改后,让我们通过重写“客户端调用”区域来继续开发。它应该包含 CameraImageCapture 方法,如您下面所见。

#region Client invokes

        public void CameraConnect(IClient client, string uri)
        {
            Console.WriteLine(client.RemoteAddress + " client IP camera connect " + uri);

            var myClient = GetClient(client);
            if (myClient == null)
                return;

            myClient.ConnectCamera(uri);
        }

        public void CameraImageCapture(IClient client)
        {
            var snapShotImage = MyClient.Snapshot.TakeSnapshot().ToImage();
            snapShotImage.Save(path + "\\snapshot.jpg");
        }

        public void CameraDisconnect(IClient client)
        {
            Console.WriteLine(client.RemoteAddress + " client IP camera disconnect.");

            var myClient = GetClient(client);
            if (myClient == null)
                return;

            myClient.DisconnectCamera();
        }

        #endregion

考虑到 MyClient.cs 负责创建 IP 摄像头连接并将其图像流式传输到您的 C# 应用程序,请将此类保持不变。让我们继续到 Program.cs 类,您需要在其中进行一些修改。插入必要的附加行后,它应该看起来像这样:

class Program
    {
        static void Main(string[] args)
        {

            var config = new MediaGatewayConfig();
            config.AddConfigElement(new FlashConfig { ServiceName = "IPCameraServer" });
            Console.Write("Please give the path to the server's Main.html file:");
            var path = Console.ReadLine();

            MyMediaGateway _mediaGateway = new MyMediaGateway(config, path);
            _mediaGateway.Start();
            Console.Read();
        }
    }

在成功完成所有必要的修改后,让我们运行应用程序。当您开始流式传输以在浏览器中查看摄像头图像时,您可以看到与以前相同的 Web GUI,但也可以看到一个新按钮。这是“捕获图像”按钮,可用于捕获当前的摄像头图像。按下此按钮后,快照将自动显示在浏览器中,并且也会保存在您的 PC 上。

结论

ONVIF 技术提供了一种很好的方法,通过实现各种 IP 摄像头解决方案来改进您的安全系统。本文演示了如何将实时 IP 摄像头流集成到您的网站中,以便能够通过您的网站监控视频监控区域。它也使得远程监控成为可能。除了监控/安全系统之外,如果您是视频博主或 ERP 系统操作员,我也推荐此项目。要使用我的源代码,您的 PC 上需要安装以下先决条件:Web 服务器(如 Apache)、ONVIF SDK(如 Ozeki Camera SDK)和 C# 编程 IDE(如 Microsoft Visual Studio)。

关注点

在撰写本文时,我了解了许多关于 ONVIF 技术的有趣内容。我发现了它广泛的应用领域。我担心这个主题还有更多内容,例如运动检测、视频分析等。所以我深入研究并创建了一个新的摄像头应用程序:一个摄像头视频录制器。

如果您有兴趣录制摄像头的流,请参阅本文,其中我展示了如何使用 C# 将音频和视频流录制为 MPEG-4 文件格式,以便能够保存和存储视频监控区域中发生的情况。

如何使用 C#.NET 为 ONVIF IP 摄像头创建视频录制应用程序(NVR/DVR 软件)
https://codeproject.org.cn/Tips/826531/How-to-Create-a-Video-Recording-Application-NVR-DV

要了解有关如何在视频监控系统中归档事件的更多信息,请查看以下文章,其中简要介绍了如何使用 C# 拍摄 ONVIF IP 摄像头的快照并将此图像保存为 JPG 文件,以便能够保留/共享最重要的时刻。

如何使用 C#.NET 创建 ONVIF IP 摄像头快照作为 JPG 图像文件
https://codeproject.org.cn/Articles/829261/How-to-create-ONVIF-IP-camera-snapshot-as-a-JPG-im

参考文献

在本节中,我列出了编写本文所使用的所有网页。您还可以在下方找到所需软件的直接下载链接。

1. http://whatis.techtarget.com/definition/IP-camera

2. http://en.wikipedia.org/wiki/IP_camera

3. http://top10thebest.com/top-10-best-wireless-ip-network-cameras-2014-reviews/

4. http://www.onvif.org/

5. https://httpd.apache.ac.cn/download.cgi

6. http://www.camera-sdk.com/p_13-download-onvif.html

7. http://www.microsoft.com/en-US/download/details.aspx?id=17851

8. http://www.microsoft.com/en-us/download/details.aspx?id=43721

© . All rights reserved.