C# 海康威视平台API接入 和网页摄像头部署

04-13 1164阅读

文章目录

  • 前言
  • 相关网址
  • 综合安防管理平台
    • 网址
      • 获取Appkey和Secret/密码和密钥
      • 测试
      • 个人魔改工具类
      • 海康视频接入
        • 获取摄像头Id
        • 下载海康Web插件
        • 原生Html导入
          • 网页设置
          • JS封装
            • 封装代码
            • 使用
            • 设置成功!

              前言

              最近有个需求是将海康的摄像头视频画面传到我们平台上,作为一个全干工程师,网页后端都要我来做。

              相关网址

              海康开发平台 资源中心

              综合安防管理平台

              我这里用的是综合安防管理平台

              C# 海康威视平台API接入 和网页摄像头部署

              网址

              综合安防管理平台 API

              OpenApi 下载

              C# 海康威视平台API接入 和网页摄像头部署

              获取Appkey和Secret/密码和密钥

              具体步骤可以看这个

              python调用海康视频汇聚平台API,获得所有摄像头设备编号、实时播放rtsp地址、回放rtsp地址

              保证有默认合作方

              C# 海康威视平台API接入 和网页摄像头部署

              C# 海康威视平台API接入 和网页摄像头部署

              拿到Key和Secret

              测试

              随便找个Api测试

              C# 海康威视平台API接入 和网页摄像头部署

              测试成功,有返回信息就可以了

              个人魔改工具类

              还是不喜欢全局静态的写法

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Net.Security;
              using System.Net;
              using System.Security.Cryptography.X509Certificates;
              using System.Security.Cryptography;
              using System.Text;
              using System.Threading.Tasks;
              namespace HaiKang
              {
                  public class HaiKangService
                  {
                      /// 
                      /// 平台ip
                      /// 
                      private string _ip;
                      /// 
                      /// 平台端口
                      /// 
                      private int _port = 443;
                      /// 
                      /// 平台APPKey
                      /// 
                      private string _appkey;
                      /// 
                      /// 平台APPSecret
                      /// 
                      private string _secret;
                      /// 
                      /// 是否使用HTTPS协议
                      /// 
                      private bool _isHttps = true;
                      /// 
                      /// 设置信息参数
                      /// 
                      /// 合作方APPKey
                      /// 合作方APPSecret
                      /// 平台IP
                      /// 平台端口,默认HTTPS的443端口
                      /// 是否启用HTTPS协议,默认HTTPS
                      /// 
                      public HaiKangService()
                      {
                          _appkey = 你的key;
                          _secret = 你的密码;
                          _ip = 你的服务器的ip地址;
                          _port = 1443;//一般是1443端口
                          _isHttps = true;
                      }
                      /// 
                      /// HTTP GET请求
                      /// 
                      /// HTTP接口Url,不带协议和端口,如/artemis/api/resource/v1/cameras/indexCode?cameraIndexCode=a10cafaa777c49a5af92c165c95970e0
                      /// 请求超时时间,单位:秒
                      /// 
                      public (byte[]? bytes, WebResponse? response) HttpGet(string uri, int timeout)
                      {
                          Dictionary header = new Dictionary();
                          // 初始化请求:组装请求头,设置远程证书自动验证通过
                          initRequest(header, uri, "", false);
                          // build web request object
                          StringBuilder sb = new StringBuilder();
                          sb.Append(_isHttps ? "https://" : "http://").Append(_ip).Append(":").Append(_port.ToString()).Append(uri);
                          HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
                          req.KeepAlive = false;
                          req.ProtocolVersion = HttpVersion.Version11;
                          req.AllowAutoRedirect = false;   // 不允许自动重定向
                          req.Method = "GET";
                          req.Timeout = timeout * 1000;    // 传入是秒,需要转换成毫秒
                          req.Accept = header["Accept"];
                          req.ContentType = header["Content-Type"];
                          foreach (string headerKey in header.Keys)
                          {
                              if (headerKey.Contains("x-ca-"))
                              {
                                  req.Headers.Add(headerKey + ":" + header[headerKey]);
                              }
                          }
                          HttpWebResponse rsp = null;
                          try
                          {
                              rsp = (HttpWebResponse)req.GetResponse();
                              if (HttpStatusCode.OK == rsp.StatusCode)
                              {
                                  Stream rspStream = rsp.GetResponseStream();     // 响应内容字节流
                                  StreamReader sr = new StreamReader(rspStream);
                                  string strStream = sr.ReadToEnd();
                                  long streamLength = strStream.Length;
                                  byte[] response = System.Text.Encoding.UTF8.GetBytes(strStream);
                                  rsp.Close();
                                  return ( response,rsp);
                              }
                              else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode)  // 302/301 redirect
                              {
                                  string reqUrl = rsp.Headers["Location"].ToString();   // 获取重定向URL
                                  WebRequest wreq = WebRequest.Create(reqUrl);          // 重定向请求对象
                                  WebResponse wrsp = wreq.GetResponse();                // 重定向响应
                                  long streamLength = wrsp.ContentLength;               // 重定向响应内容长度
                                  Stream rspStream = wrsp.GetResponseStream();          // 响应内容字节流
                                  byte[] response = new byte[streamLength];
                                  rspStream.Read(response, 0, (int)streamLength);       // 读取响应内容至byte数组
                                  rspStream.Close();
                                  rsp.Close();
                                  return (response, wrsp);
                              }
                              rsp.Close();
                          }
                          catch (WebException e)
                          {
                              if (rsp != null)
                              {
                                  rsp.Close();
                              }
                          }
                          return (null,null);
                      }
                      /// 
                      /// HTTP Post请求
                      /// 
                      /// HTTP接口Url,不带协议和端口,如/artemis/api/resource/v1/org/advance/orgList
                      /// 请求参数
                      /// 请求超时时间,单位:秒
                      /// 请求结果
                      public (byte[]? bytes, string res_str) HttpPost(string uri, string body, int timeout)
                      {
                          Dictionary header = new Dictionary();
                          // 初始化请求:组装请求头,设置远程证书自动验证通过
                          initRequest(header, uri, body, true);
                          // build web request object
                          StringBuilder sb = new StringBuilder();
                          sb.Append(_isHttps ? "https://" : "http://").Append(_ip).Append(":").Append(_port.ToString()).Append(uri);
                          // 创建POST请求
                          HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
                          req.KeepAlive = false;
                          req.ProtocolVersion = HttpVersion.Version11;
                          req.AllowAutoRedirect = false;   // 不允许自动重定向
                          req.Method = "POST";
                          req.Timeout = timeout * 1000;    // 传入是秒,需要转换成毫秒
                          req.Accept = header["Accept"];
                          req.ContentType = header["Content-Type"];
                          foreach (string headerKey in header.Keys)
                          {
                              if (headerKey.Contains("x-ca-"))
                              {
                                  req.Headers.Add(headerKey + ":" + header[headerKey]);
                              }
                          }
                          if (!string.IsNullOrWhiteSpace(body))
                          {
                              byte[] postBytes = Encoding.UTF8.GetBytes(body);
                              req.ContentLength = postBytes.Length;
                              Stream reqStream = null;
                              try
                              {
                                  reqStream = req.GetRequestStream();
                                  reqStream.Write(postBytes, 0, postBytes.Length);
                                  reqStream.Close();
                              }
                              catch (WebException e)
                              {
                                  if (reqStream != null)
                                  {
                                      reqStream.Close();
                                  }
                                  return (null,null);
                              }
                          }
                          HttpWebResponse rsp = null;
                          try
                          {
                              rsp = (HttpWebResponse)req.GetResponse();
                              if (HttpStatusCode.OK == rsp.StatusCode)
                              {
                                  Stream rspStream = rsp.GetResponseStream();
                                  StreamReader sr = new StreamReader(rspStream);
                                  string strStream = sr.ReadToEnd();
                                  long streamLength = strStream.Length;
                                  byte[] response = System.Text.Encoding.UTF8.GetBytes(strStream);
                                  rsp.Close();
                                  return (response,ResponseToStr(response));
                              }
                              else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode)  // 302/301 redirect
                              {
                                  try
                                  {
                                      string reqUrl = rsp.Headers["Location"].ToString();    // 如需要重定向URL,请自行修改接口返回此参数
                                      WebRequest wreq = WebRequest.Create(reqUrl);
                                      rsp = (HttpWebResponse)wreq.GetResponse();
                                      Stream rspStream = rsp.GetResponseStream();
                                      long streamLength = rsp.ContentLength;
                                      int offset = 0;
                                      byte[] response = new byte[streamLength];
                                      while (streamLength > 0)
                                      {
                                          int n = rspStream.Read(response, offset, (int)streamLength);
                                          if (0 == n)
                                          {
                                              break;
                                          }
                                          offset += n;
                                          streamLength -= n;
                                      }
                                      return ( response, ResponseToStr(response));
                                  }
                                  catch (Exception e)
                                  {
                                      return (null,null);
                                  }
                              }
                              rsp.Close();
                          }
                          catch (WebException e)
                          {
                              if (rsp != null)
                              {
                                  rsp.Close();
                              }
                          }
                          return (null,null);
                      }
                      private void initRequest(Dictionary header, string url, string body, bool isPost)
                      {
                          // Accept                
                          string accept = "application/json";// "*/*";
                          header.Add("Accept", accept);
                          // ContentType  
                          string contentType = "application/json";
                          header.Add("Content-Type", contentType);
                          if (isPost)
                          {
                              // content-md5,be careful it must be lower case.
                              string contentMd5 = computeContentMd5(body);
                              header.Add("content-md5", contentMd5);
                          }
                          // x-ca-timestamp
                          string timestamp = ( ( DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks ) / 1000 ).ToString();
                          header.Add("x-ca-timestamp", timestamp);
                          // x-ca-nonce
                          string nonce = System.Guid.NewGuid().ToString();
                          header.Add("x-ca-nonce", nonce);
                          // x-ca-key
                          header.Add("x-ca-key", _appkey);
                          // build string to sign
                          string strToSign = buildSignString(isPost ? "POST" : "GET", url, header);
                          string signedStr = computeForHMACSHA256(strToSign, _secret);
                          // x-ca-signature
                          header.Add("x-ca-signature", signedStr);
                          if (_isHttps)
                          {
                              // set remote certificate Validation auto pass
                              ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(remoteCertificateValidate);
                              ServicePointManager.SecurityProtocol =  SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                          }
                      }
                      /// 
                      /// 计算content-md5
                      /// 
                      /// 
                      /// base64后的content-md5
                      private string computeContentMd5(string body)
                      {
                          MD5 md5 = new MD5CryptoServiceProvider();
                          byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(body));
                          return Convert.ToBase64String(result);
                      }
                      /// 
                      /// 远程证书验证
                      /// 
                      /// 
                      /// 
                      /// 
                      /// 
                      /// 验证是否通过,始终通过
                      private bool remoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
                      {
                          return true;
                      }
                      /// 
                      /// 计算HMACSHA265
                      /// 
                      /// 待计算字符串
                      /// 平台APPSecet
                      /// HMAXH265计算结果字符串
                      private string computeForHMACSHA256(string str, string secret)
                      {
                          var encoder = new System.Text.UTF8Encoding();
                          byte[] secretBytes = encoder.GetBytes(secret);
                          byte[] strBytes = encoder.GetBytes(str);
                          var opertor = new HMACSHA256(secretBytes);
                          byte[] hashbytes = opertor.ComputeHash(strBytes);
                          return Convert.ToBase64String(hashbytes);
                      }
                      /// 
                      /// 计算签名字符串
                      /// 
                      /// HTTP请求方法,如“POST”
                      /// 接口Url,如/artemis/api/resource/v1/org/advance/orgList
                      /// 请求头
                      /// 签名字符串
                      private string buildSignString(string method, string url, Dictionary header)
                      {
                          StringBuilder sb = new StringBuilder();
                          sb.Append(method.ToUpper()).Append("\n");
                          if (null != header)
                          {
                              if (null != header["Accept"])
                              {
                                  sb.Append((string)header["Accept"]);
                                  sb.Append("\n");
                              }
                              if (header.Keys.Contains("Content-MD5") && null != header["Content-MD5"])
                              {
                                  sb.Append((string)header["Content-MD5"]);
                                  sb.Append("\n");
                              }
                              if (null != header["Content-Type"])
                              {
                                  sb.Append((string)header["Content-Type"]);
                                  sb.Append("\n");
                              }
                              if (header.Keys.Contains("Date") && null != header["Date"])
                              {
                                  sb.Append((string)header["Date"]);
                                  sb.Append("\n");
                              }
                          }
                          // build and add header to sign
                          string signHeader = buildSignHeader(header);
                          sb.Append(signHeader);
                          sb.Append(url);
                          return sb.ToString();
                      }
                      /// 
                      /// 返回字符流
                      /// 
                      /// 
                      /// 
                      private string ResponseToStr(byte[] bytes)
                      {
                          var res = "";
                          res = System.Text.Encoding.UTF8.GetString(bytes);
                          return res;
                      }
                      /// 
                      /// 计算签名头
                      /// 
                      /// 请求头
                      /// 签名头
                      private string buildSignHeader(Dictionary header)
                      {
                          Dictionary sortedDicHeader = new Dictionary();
                          sortedDicHeader = header;
                          var dic = from objDic in sortedDicHeader orderby objDic.Key ascending select objDic;
                          StringBuilder sbSignHeader = new StringBuilder();
                          StringBuilder sb = new StringBuilder();
                          foreach (KeyValuePair kvp in dic)
                          {
                              if (kvp.Key.Replace(" ", "").Contains("x-ca-"))
                              {
                                  sb.Append(kvp.Key + ":");
                                  if (!string.IsNullOrWhiteSpace(kvp.Value))
                                  {
                                      sb.Append(kvp.Value);
                                  }
                                  sb.Append("\n");
                                  if (sbSignHeader.Length > 0)
                                  {
                                      sbSignHeader.Append(",");
                                  }
                                  sbSignHeader.Append(kvp.Key);
                              }
                          }
                          header.Add("x-ca-signature-headers", sbSignHeader.ToString());
                          return sb.ToString();
                      }
                  }
              }
              

              海康视频接入

              海康视频接入有两种方式,第一种是有web插件,第二种是无web插件。无web插件弄起来特别的麻烦,所以我们主要讲第一种。

              获取摄像头Id

              选择分页获取监控点资源

              C# 海康威视平台API接入 和网页摄像头部署

              选择个数多一点

              C# 海康威视平台API接入 和网页摄像头部署

              C# 海康威视平台API接入 和网页摄像头部署

              下载海康Web插件

              海康开发平台下载地址

              视频Web 插件 V1.5.2直链下载地址

              C# 海康威视平台API接入 和网页摄像头部署

              下载完成之后我们可以看到Demo,选择合适的自己试一下

              C# 海康威视平台API接入 和网页摄像头部署

              原生Html导入

              我这里已经封装好了,我直接拿出来了

              网页设置

              我将很多无关都隐藏了

              
              
                  
                  
                  
                  
              	........
              
              
              	.....
                   

              CSS

              .playWnd {
                  margin: 30px 0 0 150px;
                  width: 1000px; /*播放容器的宽和高设定*/
                  height: 600px;
                  border: 1px solid red;
              }
              

              JS封装

              注意,我这个是绑定id的,必须要div的id为playWnd

              封装代码
              //海康div示例
              //
              /**
               * 海康摄像头帮助类
               */
              let oWebControl = new WebControl();
              class Hik_Class {
                  /**
                   * 
                   * @param {Ip地址} ip
                   * @param {端口} port
                   * @param {密钥} appkey
                   * @param {密码} secret
                   * @param {摄像头Id} cameraIndexCode
                   */
                  constructor(ip, port, appkey, secret, cameraIndexCode) {
                      this.ip = ip;
                      this.port = port;
                      this.appkey = appkey;
                      this.secret = secret;
                      this.cameraIndexCode = cameraIndexCode
                  }
                  Init() {
                      var that = this
                      oWebControl = new WebControl({
                          szPluginContainer: "playWnd",                       // 指定容器id
                          iServicePortStart: 15900,                           // 指定起止端口号,建议使用该值
                          iServicePortEnd: 15900,
                          szClassId: "23BF3B0A-2C56-4D97-9C03-0CB103AA8F11",   // 用于IE10使用ActiveX的clsid
                          cbConnectSuccess: function () {                     // 创建WebControl实例成功											
                              oWebControl.JS_StartService("window", {         // WebControl实例创建成功后需要启动服务
                                  dllPath: "./VideoPluginConnect.dll"         // 值"./VideoPluginConnect.dll"写死 
                              }).then(function () {                           // 启动插件服务成功
                                  oWebControl.JS_SetWindowControlCallback({   // 设置消息回调
                                      cbIntegrationCallBack: cbIntegrationCallBack
                                  });
                                  oWebControl.oDocOffset.top = 95;//设置你的x,y偏移,因为海康插件无法识别多页面的浏览器位置
                                  oWebControl.oDocOffset.left = 280;
                                  oWebControl.JS_CreateWnd("playWnd", 1000, 600, { bEmbed: true }).then(function () { //JS_CreateWnd创建视频播放窗口,宽高可设定
                                      that.init();  // 创建播放实例成功后初始化
                                  });
                              }, function () { // 启动插件服务失败
                              });
                          },
                          cbConnectError: function () { // 创建WebControl实例失败
                              oWebControl = null;
                              $("#playWnd").html("插件未启动,正在尝试启动,请稍候...");
                              WebControl.JS_WakeUp("VideoWebPlugin://"); // 程序未启动时执行error函数,采用wakeup来启动程序
                              initCount++;
                              if (initCount  0) ? Math.round(oDivRect.right - iWidth) : 0;
                  var iCoverBottom = (oDivRect.bottom - iHeight > 0) ? Math.round(oDivRect.bottom - iHeight) : 0;
                  iCoverLeft = (iCoverLeft > 1000) ? 1000 : iCoverLeft;
                  iCoverTop = (iCoverTop > 600) ? 600 : iCoverTop;
                  iCoverRight = (iCoverRight > 1000) ? 1000 : iCoverRight;
                  iCoverBottom = (iCoverBottom > 600) ? 600 : iCoverBottom;
                  oWebControl.JS_RepairPartWindow(0, 0, 1001, 600);    // 多1个像素点防止还原后边界缺失一个像素条
                  if (iCoverLeft != 0) {
                      oWebControl.JS_CuttingPartWindow(0, 0, iCoverLeft, 600);
                  }
                  if (iCoverTop != 0) {
                      oWebControl.JS_CuttingPartWindow(0, 0, 1001, iCoverTop);    // 多剪掉一个像素条,防止出现剪掉一部分窗口后出现一个像素条
                  }
                  if (iCoverRight != 0) {
                      oWebControl.JS_CuttingPartWindow(1000 - iCoverRight, 0, iCoverRight, 600);
                  }
                  if (iCoverBottom != 0) {
                      oWebControl.JS_CuttingPartWindow(0, 600 - iCoverBottom, 1000, iCoverBottom);
                  }
              }
              //视频预览功能
              $("#startPreview").click(function () {
              });
              //停止全部预览
              $("#stopAllPreview").click(function () {
                  oWebControl.JS_RequestInterface({
                      funcName: "stopAllPreview"
                  });
              });
              // 标签关闭
              $(window).unload(function () {
                  if (oWebControl != null) {
                      oWebControl.JS_HideWnd();   // 先让窗口隐藏,规避可能的插件窗口滞后于浏览器消失问题 
                      oWebControl.JS_Disconnect().then(function () {  // 断开与插件服务连接成功
                      },
                          function () {  // 断开与插件服务连接失败
                          });
                  }
              });
              
              
              使用
              //声明
              let hik_Class = new Hik_Class(你的外网ip地址,
               你的端口,
              你的AppKey,
              你的Secret,
              你的摄像头Id)
              //使用
              hik_Class.Init()//创建窗体
              hik_Class.Connect()//连接摄像头并输入视频流
              hik_Class.Close()//关闭窗体
              
              设置成功!

              C# 海康威视平台API接入 和网页摄像头部署

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]