拌合楼软件开发(33)多品牌车牌识别摄像头引入

前言:

为了降本,原有的海康摄像头外,有必要引入其他的品牌车牌识别摄像头,那么如何在程序中能够根据配置文件来调用不同品牌的摄像头来进行车牌识别,拍照等的操作。本文的先通过一个抽象类,后续有文章讲解各品牌sdk如何来集成。

需要实现的功能清单:

一、车牌识别:

1. 支持触发识别:

通过触发线实现车辆的车牌自动识别并上报,程序进行处理。

2. 支持强制车牌识别:

通过程序去强制车牌识别摄像头进行车牌识别

二、 抓拍图片:

程序可以控制车牌识别摄像头,对场景图片进行抓拍。

三、视频流显示

视频流推送到程序,通过picturebox来显示,程序端可以看到实时监控画面。

四、道闸控制

车牌识别后输出控制信号到道闸,控制道闸的开启和关闭操作。

程序抽象化类的实现:

一、定义一个结构体实现车牌识别后推送的数据

cs 复制代码
   public struct LicensePlateInfo
   {
       public string licensePlate;    //车牌
       public DateTime getLicensePlateTime;   //获取到的时间
       public List<PhotoInfo> picNames;   //触发生成的图片信息
       public string ip;   //当前设备IP
   } 

   public struct PhotoInfo
   {
       public string fileName;   //文件名
       public DateTime getPhotoTime;   //图片获取到时间
       public string type;   //图片类型
   }

部分摄像头,触发生成图片时会生成两张图片,一张为车牌的小图,一张未抓拍的整体环境图片。所以在PhotoInfo中增加了type来做区别。

二、抽象类的定义:

cs 复制代码
 public abstract class LicensePlateDevice{

}

三、定义的一些属性:

cs 复制代码
 public int LPDHandle = -1;   //摄像头登录后返回值
 public int LPDViewHandle = -1;   //摄像头预览后返回值
 public IntPtr camerHandle;   //部分摄像头兼容保留
 public string ipAddress;   //IP地址
 public string userName;    //用户名
 public string password;    //密码
 public ushort port;   //端口
 public PictureBox picBox;   //显示的空间
 public bool deviceStatus = false;   //状态

四、车牌识别事件的定义:

cs 复制代码
    public event EventHandler<LicensePlateInfo> GetLicensePlateInfo;
    public event EventHandler<string> GetLicensePlate;

定义了两种类型的事件,一种抛出前面我们定义的结构体,一种直接就只抛出车牌了。

五、构造函数:

cs 复制代码
   public LicensePlateDevice(string _ip, string _userName = "", string _password = "", ushort _port = 0)
   {

       ipAddress = _ip;
       userName = _userName;
       password = _password;
       port = _port;

   }

六、定义的方法

cs 复制代码
        public abstract int ConnectToDevice();  //连接到车牌设备

        public abstract void StartView();  //开启监控预览

        public abstract string GetPic();  //抓拍图片

        public abstract string GetPlate();// 获取车牌

        public abstract void CloseConnect();  //关闭连接

        public abstract void OpenGate(); //开闸

        public abstract void CloseGate(); //关闸

        public abstract void CloseView();  //关闭监控预览

七、两个事件:

cs 复制代码
  protected virtual void onGetLicensePlateInfo(LicensePlateInfo e)
  {
      GetLicensePlateInfo?.Invoke(this, e);
  }

  protected virtual void onGetLicensePlate(string e)
  {

      GetLicensePlate?.Invoke(this, e);
  }

八、整个完整的代码:

cs 复制代码
    public abstract class LicensePlateDevice
    {
        public int LPDHandle = -1;
        public int LPDViewHandle = -1;
        public IntPtr camerHandle;
        public string ipAddress;
        public string userName;
        public string password;
        public ushort port;
        public PictureBox picBox;
        public bool deviceStatus = false;   
        public event EventHandler<LicensePlateInfo> GetLicensePlateInfo;
        public event EventHandler<string> GetLicensePlate;
        protected virtual void onGetLicensePlateInfo(LicensePlateInfo e)
        {
            GetLicensePlateInfo?.Invoke(this, e);
        }

        protected virtual void onGetLicensePlate(string e)
        {

            GetLicensePlate?.Invoke(this, e);
        }

        public LicensePlateDevice(string _ip, string _userName = "", string _password = "", ushort _port = 0)
        {

            ipAddress = _ip;
            userName = _userName;
            password = _password;
            port = _port;

        }

        public abstract int ConnectToDevice();
        public abstract void StartView();
        public abstract string GetPic();  //抓拍图片
        public abstract string GetPlate();// 获取车牌
        public abstract void CloseConnect();  //关闭连接
        public abstract void OpenGate(); //开闸
        public abstract void CloseGate(); //关闸
        public abstract void CloseView();
    }

总结:

车牌识别的摄像头品牌很多,各自提供的sdk 实现方法各不相同,本文抽取了项目中需要的功能编写了一个抽象类,为后续通过配置文件来调用不同品牌摄像头打下基础。