2020-08-26

使用C#对华为IPC摄像头二次开发(一)

使用WPF来对华为IPC摄像头进行二次开发,本文实现了登录IPC摄像头和自动预览摄像头视频图像以及采用WriteableBitmap来手动处理摄像头回调的视频流并流畅的展示在Image控件里。

开发环境:

操作系统:Win10 x64专业版2004

开发工具:VS2019 16.7.2

目标平台:x86

首先去下载IPC SDK(点击下载,需要华为授权账户。)

新建一个WPF的项目,Framework版本为4.7

把下载的sdk压缩包中的windows\output32目录中的HWPuSDK.dll和lib目录中的所有文件,都复制到项目的bin/debug目录中(和生成的exe同级),华为的这个SDK对64位支持不好,使用64位遇到不少问题,最终还是先采用32位的DLL。

项目中对图像的手动处理,经过对比,在Emgu CV和OpenCVSharp4中采用了OpenCVSharp4,个人感觉OpenCVSharp4使用起来更简洁方便。

项目中对视频流的回调手动处理展示,采用WriteableBitmap(参考吕毅大神的《WPF 高性能位图渲染 WriteableBitmap 及其高性能用法示例》),本来想采用D3D这种显卡加速的方法,无奈没有找到相关文章和资料,如果哪位大神有资料,还望告知一下。谢谢!

项目中引用了以下组件

在本次开发中,我们先实现自动预览摄像头视频和手动对摄像头视频流进行处理。

因为SDK自动播放需要传入一个控件的句柄,而WPF中窗体上所有控件的句柄都是窗体本身,所以我们还需要使用WindowsFromHost来使用Winform的一些控件来实现播放句柄的传入。

项目中引用WindowsFromsIntegeration

在项目的MainWindow.xaml中添加三个按钮、两个RadioButton、一个Winform的PictureBox和一个WPF的Image控件。大致布局如下:

详细的xaml代码:

<Window x:Class="HuaWeiCamera.MainWindow"  ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  ="http://schemas.microsoft.com/expression/blend/2008"  ="http://schemas.microsoft.com/winfx/2006/xaml"  ="http://schemas.open  ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  mc:Ignorable="d"  WindowStartupLocation="CenterScreen"  Loaded="MainWindow_OnLoaded"  Title="MainWindow" Height="800" Width="1200" Closed="MainWindow_OnClosed"> <Grid Margin="0,0,2,0">  <Grid.RowDefinitions>   <RowDefinition Height="60"></RowDefinition>   <RowDefinition Height="*"></RowDefinition>  </Grid.RowDefinitions>  <WrapPanel VerticalAlignment="Center">   <StackPanel Margin="30,5,0,0" VerticalAlignment="Center">    <Button Content="预览摄像头" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Height="30" Click="ButtonView_OnClick" />    <WrapPanel Margin="0,5,0,0">     <RadioButton Content="自动处理" VerticalAlignment="Center" IsChecked="True" GroupName="PlayMode" x:Name="RadioButtonAuto" />     <RadioButton Content="手动处理" VerticalAlignment="Center" GroupName="PlayMode" x:Name="RadioButtonManual" Margin="10,0,0,0" />    </WrapPanel>   </StackPanel>   <Button x:Name="ButtonSaveOne" Content="抓拍一张" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0" Width="75" Height="30" IsEnabled="False" Click="ButtonSave_OnClick" />
<Button Content="人脸抓拍" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0" Width="75" Height="30" /> </WrapPanel> <WrapPanel Grid.Row="1"> <WindowsFormsHost HorizontalAlignment="Center" Width="1200" Height="700" VerticalAlignment="Center" x:Name="FormsHostVideo"> <wf:PictureBox x:Name="ImagePlay"></wf:PictureBox> </WindowsFormsHost> <Image VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="CanvaVideo" Stretch="Fill" Source="{Binding Path=VideoWriteableBitmap}" /> </WrapPanel> </Grid></Window>

在App.cs中定义下日志记录类

 public partial class App : Application {  public static NLog.Logger NewNLog;  private void App_OnStartup(object sender, StartupEventArgs e)  {   DispatcherUnhandledException += App_DispatcherUnhandledException;   NewNLog = NLog.LogManager.GetLogger("HuaWeiCameraLoger");  } }

根据华为的《SDC 8.0.1 SDK开发指南》,我们要实现摄像头预览,需要先定义以下几个struct和enum:

sturct:PU_REAL_PLAY_INFO_S(视频实时预览结构体)、PU_TIME_S(时间结构体)

enum:PU_PROTOCOL_TYPE(传输协议类型)、PU_STREAM_TYPE(码流类型)、PU_VIDEO_TYPE(数据流类型)、PU_MEDIA_CRYPTO_TYPE(加密类型)、PU_MEDIA_CALLBACK_TYPE(回调类型)

using System;using System.Runtime.InteropServices;using HuaWeiCamera.Enums;using HuaWeiCamera.Enums.Media;using HuaWeiCamera.Enums.Video;namespace HuaWeiCamera.Struct{ /// <summary> /// 视频实时预览结构体 /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct PU_REAL_PLAY_INFO_S {  /// <summary>  /// 设备通道号,一般为101。  /// </summary>  public uint ulChannelId;  /// <summary>  /// 播放窗口句柄,为IntPtr.Zero表示用户自己处理视频数据流,不自动播放视频流  /// </summary>  public IntPtr hPlayWnd;  /// <summary>  /// 码流类型,主码流、子码  /// </summary>  public PU_STREAM_TYPE enStreamType;  /// <summary>  /// 流类型:视频流、音频流、复合流、录 像流、元数据  /// </summary>  public PU_VIDEO_TYPE enVideoType;  /// <summary>  /// 传输协议类型,UDP,TCP  /// </summary>  public PU_PROTOCOL_TYPE enProtocolType;  /// <summary>  /// 回调类型:0:RTP解密1:RTP不解密 2:Frame 3:YUV  /// </summary>  public PU_MEDIA_CALLBACK_TYPE enMediaCallbackType;  /// <summary>  /// 请求端IP,第三方平台可以不填,SDK会 自动获取  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]  public string szLocalIp;  /// <summary>  /// 是否保活  /// </summary>  public bool bKeepLive;  /// <summary>  /// 请求预录、录像开始时间(本地时 间)。   /// </summary>  public PU_TIME_S stStartTime;  /// <summary>  /// 请求预录、录像结束时间(本地时 间)。   /// </summary>  public PU_TIME_S stEndTime;  /// <summary>  /// 加密类型,只支持AES加密。  /// </summary>  public PU_MEDIA_CRYPTO_TYPE enMediaCryptoType;  /// <summary>  /// 加密密钥  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 44)]  public string szMediaCrypto;  /// <summary>  /// szReserved[0-15]表示组播IP地址  /// szReserved[16-19]表示组播端口  /// szReserved[22]表示智能分析数据打包 格式 0:/// szReserved[23]表示元数据请求类型,取值参考枚举 PU_METADATA_REQUEST_TYPE_E定义  /// </summary>  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]  public byte[] szReserved; }}
namespace HuaWeiCamera.Enums{ /// <summary> /// 视频流类型 /// </summary> public enum PU_STREAM_TYPE {  /// <summary>  /// 视频主码流  /// </summary>  PU_VIDEO_MAIN_STREAM=0,  /// <summary>  /// 视频子码流  /// </summary>  PU_VIDEO_SUB_STREAM1,  /// <summary>  /// 视频子码流2(VWareC01 不支持)  /// </summary>  PU_VIDEO_SUB_STREAM2,  /// <summary>  /// 视频子码流3(VWareC01 不支持)  /// </summary>  PU_VIDEO_SUB_STREAM3,  /// <summary>  /// 视频子码流4  /// </summary>  PU_VIDEO_SUB_STREAM4,  /// <summary>  /// 视频子码流5  /// </summary>  PU_VIDEO_SUB_STREAM5,  /// <summary>  /// 预留值  /// </summary>   PU_VIDEO_STREAM_MAX }}
namespace HuaWeiCamera.Enums.Video{ /// <summary> /// 码流类型 /// </summary> public enum PU_VIDEO_TYPE {  /// <summary>  /// 视频流  /// </summary>  PU_VIDEO_TYPE_VIDEO = 0,  /// <summary>  /// 音频流  /// </summary>  PU_VIDEO_TYPE_AUDIO,  /// <summary>  /// 复合流  /// </summary>  PU_VIDEO_TYPE_MUX,   /// <summary>  /// 录像流  /// </summary>  PU_VIDEO_TYPE_RECORD,   /// <summary>  /// 元数据流  /// </summary>  PU_VIDEO_TYPE_META,   /// <summary>  /// 视频+元数据流  /// </summary>  PU_VIDEO_TYPE_VIDEO_META,   /// <summary>  /// 预留值  /// </summary>  PU_VIDEO_TYPE_MAX }}
namespace HuaWeiCamera.Enums{ /// <summary> /// 数据传输类型 /// </summary> public enum PU_PROTOCOL_TYPE {  /// <summary>  /// UDP  /// </summary>  PU_PROTOCOL_TYPE_UDP = 0,   /// <summary>  /// TCP  /// </summary>  PU_PROTOCOL_TYPE_TCP,  /// <summary>  /// 组播方式  /// </summary>  PU_PROTOCOL_TYPE_MULTICAST,  /// <summary>  /// 预留值  /// </summary>  PU_PROTOCOL_TYPE_MAX }}
namespace HuaWeiCamera.Enums.Media{ /// <summary> /// 媒体回调类型 /// </summary> public enum PU_MEDIA_CALLBACK_TYPE {  /// <summary>  /// RTP包方式  /// </summary>  PU_MEDIA_CALLBACK_TYPE_RTP = 0,   /// <summary>  /// RTP包形式,不解密  /// </summary>  PU_MEDIA_CALLBACK_TYPE_RTP_CRYPTO,   /// <summary>  /// 帧回调方式  /// </summary>  PU_MEDIA_CALLBACK_TYPE_FRAME,   /// <summary>  /// YUV方式,Linux不支持  /// </summary>  PU_MEDIA_CALLBACK_TYPE_YUV,   /// <summary>  /// 把RTP包回调给控件方处理方式,Linux不支持  /// </summary>  PU_MEDIA_CALLBACK_TYPE_FOR_STORAGE,   /// <summary>  /// 智能元数据方式  /// </summary>  PU_MEDIA_CALLBACK_TYPE_META_FRAME,   /// <summary>  /// 预留值  /// </summary>  PU_MEDIA_CALLBACK_TYPE_MAX }}
using System.Runtime.InteropServices;namespace HuaWeiCamera.Struct{ /// <summary> /// 时间结构体 /// </summary> [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct PU_TIME_S {  /// <summary>  /// 年  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]  public string szYear;  /// <summary>  /// 月  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]  public string szMonth;  /// <summary>  /// 日  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]  public string szDay;  /// <summary>  /// 时  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]  public string szHour;  /// <summary>  /// 分  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]  public string szMinute;  /// <summary>  /// 秒  /// </summary>  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]  public string szSecond; }}

定义一个静态类,用来实现调用SDK和摄像头交互(HuaWeiSDKHelper)

using System;using System.Runtime.InteropServices;using HuaWeiCamera.Enums;using HuaWeiCamera.Enums.SnapShot;using HuaWeiCamera.Struct;namespace HuaWeiCamera.Class{ /// <summary> /// 华为HoloSens SDC二次开发使用 /// </summary> public static class HuaWeiSdkHelper {  private const string SdkPath= "HWPuSDK.dll";  #region 初始化和登录  /// <summary>  /// 初始化设备  /// </summary>  /// <param name="ulLinkMode">0自动 1手动 3混合模式</param>  /// <param name="szLocalIp">本地IP</param>  /// <param name="ulLocalPort">本地端口</param>  /// <returns></returns>  [DllImport(SdkPath, EntryPoint = "IVS_PU_Init", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]  public static extern bool IVS_Pu_Init(uint ulLinkMode, string szLocalIp, uint ulLocalPort);  /// <summary>  /// 远程登录设备  /// </summary>  /// <param name="szLoginIp">设备IP</param>  /// <param name="ulLoginPort">设备端口 6060</param>  /// <param name="szUserName">登录名 ApiAdmin</param>  /// <param name="szPasswd">登录密码 HuaWei123</param>  /// <returns></returns>  [DllImport(SdkPath, EntryPoint = "IVS_PU_Login", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]  public static extern uint IVS_PU_Login(string szLoginIp, uint ulLoginPort, string szUserName, string szPasswd);  /// <summary>  /// 初始化和登录设备  /// </summary>  /// <param name="sdcIp">SDC设备IP</param>  /// <param name="sdcPort">SDC设备端口</param>  /// <param name="sdcUser">SDC登录用户名</param>  /// <param name="sdcPwd">SDC登录密码</param>  /// <param name="errMsg">失败时的错误信息</param>  /// <param name="ulIdentifyId">登录成功后返回登录句柄</param>  public static void InitAndLogin(string sdcIp,uint sdcPort,string sdcUser,string sdcPwd,out uint ulIdentifyId,out string errMsg)  {   ulIdentifyId = 0;   errMsg = "";   //要开启TLS的情况时:初始化调用IVS_PU_InitEx接口,登录调用IVS_PU_Login 接口时端口号设置为6061   if (!IVS_Pu_Init(1, "192.168.2.144", 6060))   {    errMsg=($"设备初始化失败,{GetLastErrorInfo()}");    return;   }   ulIdentifyId = IVS_PU_Login(sdcIp, sdcPort, sdcUser, sdcPwd);   if (ulIdentifyId == 0)   {    errMsg=$"设备登录失败,{GetLastErrorInfo()}";   }  }  

No comments:

Post a Comment