使用C#對華為IPC攝像頭二次開發(一)

踏平扶桑發表於2020-08-26

開發環境:

作業系統: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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wf="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>
    /// 視訊實時預覽結構體,http://www.cnblogs.com/wdw984
    /// </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:XML,1:後設資料
        /// szReserved[23]表示後設資料請求型別,取值參考列舉 PU_METADATA_REQUEST_TYPE_E定義
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] szReserved;
    }
}
namespace HuaWeiCamera.Enums
{
    /// <summary>
    /// 視訊流型別,http://www.cnblogs.com/wdw984
    /// </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>
    /// 碼流型別,http://www.cnblogs.com/wdw984
    /// </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>
    /// 資料傳輸型別,http://www.cnblogs.com/wdw984
    /// </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>
    /// 媒體回撥型別,http://www.cnblogs.com/wdw984
    /// </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>
    /// 時間結構體,http://www.cnblogs.com/wdw984
    /// </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二次開發使用,http://www.cnblogs.com/wdw984
    /// </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()}";
            }
        }

        #endregion

        #region 預覽相關
        /// <summary>
        /// 實時預覽
        /// </summary>
        /// <param name="ulIdentifyId">登入成功後返回的使用者編號</param>
        /// <param name="pstRealPlayInfo">播放結構體</param>
        /// <param name="fRealDataCallBack">回撥實現播放</param>
        /// <param name="pUsrData">傳入碼流資料 回撥函式作為引數</param>
        /// <returns></returns>
        [DllImport(SdkPath, EntryPoint = "IVS_PU_RealPlay")]
        public static extern uint IVS_PU_RealPlay(uint ulIdentifyID, PU_REAL_PLAY_INFO_S[] pstRealPlayInfo, PfRealDataCallBack fRealDataCallBack, ref IntPtr pUsrData);

        public delegate void PfRealDataCallBack(IntPtr szBuffer, int lSize, IntPtr pUsrData);
        /// <summary>
        /// 停止實時預覽
        /// </summary>
        /// <param name="ulIdentifyId">登入成功後返回的使用者編號</param>
        /// <param name="ulRealHandle">實時播放控制程式碼</param>
        /// <returns></returns>
        [DllImport(SdkPath, EntryPoint = "IVS_PU_StopRealPlay", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]
        public static extern uint IVS_PU_StopRealPlay(uint ulIdentifyId, uint ulRealHandle);
        #endregion

        #region 錯誤資訊相關

        /// <summary>
        /// 獲取最後一次錯誤程式碼
        /// </summary>
        /// <returns></returns>
        [DllImport(SdkPath, EntryPoint = "IVS_PU_GetLastError", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]
        private static extern int IVS_PU_GetLastError();

        /// <summary>
        /// 根據錯誤程式碼返回錯誤資訊
        /// </summary>
        /// <param name="ulErrorNo">錯誤編號</param>
        /// <returns></returns>
        [DllImport(SdkPath, EntryPoint = "IVS_PU_GetErrorMsg", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr IVS_PU_GetErrorMsg(int ulErrorNo);

        /// <summary>
        /// 返回最後一次錯誤碼和錯誤資訊
        /// </summary>
        /// <returns></returns>
        public static string GetLastErrorInfo()
        {
            var lastErrorCode = IVS_PU_GetLastError();
            var lastErrorMsg = Marshal.PtrToStringAnsi(IVS_PU_GetErrorMsg(lastErrorCode));

            return $"錯誤碼:{IVS_PU_GetLastError()},錯誤資訊:{lastErrorMsg}";
        }

        #endregion
        
        #region 退出登入

        /// <summary>
        /// 退出登入
        /// </summary>
        /// <param name="ulIdentifyId">登入成功後返回的控制程式碼編號</param>
        /// <returns></returns>
        [DllImport(SdkPath, EntryPoint = "IVS_PU_Logout", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool IVS_PU_Logout(uint ulIdentifyId);
        /// <summary>
        /// 反註冊裝置,退出時進行裝置釋放
        /// </summary>
        /// <returns></returns>
        [DllImport(SdkPath, EntryPoint = "IVS_PU_Cleanup", CharSet = CharSet.Ansi, SetLastError = true, PreserveSig = true, CallingConvention = CallingConvention.StdCall)]
        public static extern bool IVS_PU_Cleanup();

        #endregion

    }
}

 在MainWindow.cs中我們需要定義一些變數:

        private uint _ulIdentifyId;//登入攝像頭後返回的編號
        private uint _ulRealHandleId;//呼叫預覽SDK返回的結果值
        private static bool _isSave;//是否儲存當前幀為圖片
        private static bool _isExit;//是否退出
private static HuaWeiSdkHelper.PfRealDataCallBack _fedRealPlayCallbackWithYUV;//手動處理攝像頭回撥時的委託事件
private const uint ByteLength = 1920 * 1080 * 4;//點陣圖的大小 private static readonly VideoYuvModelView VideoYuvModelView = new VideoYuvModelView();//Image的資料來源,用來手動處理視訊時候展示影像 private IntPtr _videoPlayHandle = IntPtr.Zero;//自動預覽時的控制元件控制程式碼 [DllImport("kernel32.dll")] private static extern void CopyMemory(IntPtr destination, IntPtr source, uint length);//用來複制記憶體中的資料

在窗體初始化和載入事件中,我們來初始化一些資料的繫結

public MainWindow()
        {
            InitializeComponent();
            VideoYuvModelView.VideoWriteableBitmap = new WriteableBitmap(1920, 1800, 96.0, 96.0, PixelFormats.Bgr32, null);//因為攝像頭返回的圖片大小時1920*1080,所以這裡定義的大小要和返回的圖片大小一致
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            CanvaVideo.DataContext = VideoYuvModelView;//手動處理時,Image控制元件資料來源
            _videoPlayHandle = ImagePlay.Handle;//自動處理時,使用Winform的控制元件控制程式碼
        }

預覽攝像頭按鈕事件,中間根據選中的自動處理和手動處理來分別做出不同的相應

#region 視訊預覽(自動和手動處理視訊流)

        private void ButtonView_OnClick(object sender, RoutedEventArgs e)
        {
            if (0 == _ulIdentifyId)
            {
//這裡通過網路登入到攝像頭,具體埠、使用者名稱、密碼請參考開發手冊 HuaWeiSdkHelper.InitAndLogin(
"192.168.2.250", 6060, "ApiAdmin", "HuaWei123", out _ulIdentifyId, out string errMsg); if (0 == _ulIdentifyId) { MessageBox.Show(errMsg); return; } } var prpInfos = new PU_REAL_PLAY_INFO_S[1]; var clientInfo = new PU_REAL_PLAY_INFO_S { ulChannelId = 101, hPlayWnd = _videoPlayHandle, enProtocolType = PU_PROTOCOL_TYPE.PU_PROTOCOL_TYPE_TCP, enStreamType = PU_STREAM_TYPE.PU_VIDEO_MAIN_STREAM, enVideoType = PU_VIDEO_TYPE.PU_VIDEO_TYPE_VIDEO, enMediaCryptoType = PU_MEDIA_CRYPTO_TYPE.PU_MEDIA_CRYPTO_NONE, enMediaCallbackType = PU_MEDIA_CALLBACK_TYPE.PU_MEDIA_CALLBACK_TYPE_RTP, szReserved = new byte[32], bKeepLive = true }; IntPtr pUsrData = (IntPtr)_ulIdentifyId; if (RadioButtonManual.IsChecked == true) { //手動處理視訊預覽 FormsHostVideo.Visibility = Visibility.Collapsed; CanvaVideo.Visibility = Visibility.Visible; clientInfo.hPlayWnd = IntPtr.Zero; clientInfo.enMediaCallbackType = PU_MEDIA_CALLBACK_TYPE.PU_MEDIA_CALLBACK_TYPE_YUV; prpInfos[0] = clientInfo; _fedRealPlayCallbackWithYUV = FedRealPlayCallbackWithYUV; //手動處理回撥 _ulRealHandleId = HuaWeiSdkHelper.IVS_PU_RealPlay(_ulIdentifyId, prpInfos, _fedRealPlayCallbackWithYUV, ref pUsrData); if (0 == _ulRealHandleId) { MessageBox.Show(HuaWeiSdkHelper.GetLastErrorInfo()); } ButtonSaveOne.IsEnabled = true;//點選 抓拍一張 按鈕,會把_isSave變數設定為true,從而在回撥事件中可以儲存當前幀為圖片 } else {
ButtonSaveOne.IsEnabled = false; CanvaVideo.Visibility
= Visibility.Collapsed; FormsHostVideo.Visibility = Visibility.Visible; prpInfos[0] = clientInfo; //傳入控制程式碼,自動預覽 _ulRealHandleId = HuaWeiSdkHelper.IVS_PU_RealPlay(_ulIdentifyId, prpInfos, null, ref pUsrData); if (0 == _ulRealHandleId) { MessageBox.Show(HuaWeiSdkHelper.GetLastErrorInfo()); } } } #region 手動解析YUV資料並展示在介面上 private static void FedRealPlayCallbackWithYUV(IntPtr szBuffer, int lSize, IntPtr pUsrData) { if (_isExit) return; try { Span<byte> nativeSpan; unsafe { nativeSpan = new Span<byte>(szBuffer.ToPointer(), lSize); } if (nativeSpan.Length > 0) { #region 處理視訊流YUV影像 Mat yuvImg = new Mat(1080 * 3 / 2, 1920, MatType.CV_8UC1); Mat rgbImg = new Mat(1080, 1920, MatType.CV_8UC4); Marshal.Copy(nativeSpan.ToArray(), 0, yuvImg.Data, nativeSpan.Length); Cv2.CvtColor(yuvImg, rgbImg, ColorConversionCodes.YUV2RGBA_I420); Application.Current.Dispatcher?.InvokeAsync(() => { VideoYuvModelView.VideoWriteableBitmap.Lock(); unsafe { CopyMemory(VideoYuvModelView.VideoWriteableBitmap.BackBuffer, new IntPtr(rgbImg.DataPointer), ByteLength); } VideoYuvModelView.VideoWriteableBitmap.AddDirtyRect(new Int32Rect(0, 0, 1920, 1080)); VideoYuvModelView.VideoWriteableBitmap.Unlock(); }); if (_isSave) { Cv2.ImWrite(Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}", "jpg",$"{Guid.NewGuid()}.jpg"), rgbImg); _isSave = false; } #endregion } } catch (Exception e) { App.NewNLog.Error($"解析視訊流出錯:{e}"); } } #endregion #endregion

在程式關閉的時候進行資源釋放。

        private void MainWindow_OnClosed(object sender, EventArgs e)
        {
            _isExit = true;if (_ulRealHandleId > 0)
            {
                HuaWeiSdkHelper.IVS_PU_StopRealPlay(_ulIdentifyId, _ulRealHandleId);//停止預覽
            }
            if (_ulIdentifyId > 0)
            {
                HuaWeiSdkHelper.IVS_PU_Logout(_ulIdentifyId);//退出登入
            }
            HuaWeiSdkHelper.IVS_PU_Cleanup();//SDK資源釋放
        }

具體效果如下:

1、自動預覽

2、手動處理

但是這裡的YUV回撥填充到WriteableBitmap遇到個問題,就是無法填滿WriteableBitmap(把WriteableBitmap中的資料儲存下來也是和預覽的效果一樣,估計是填充的演算法不對),只能填充一部分,這裡涉及到本人的知識盲區,暫時沒法解決掉。

3、一幀影像儲存為本地圖片

 

 

至此我們使用C#初步實現了華為IPC攝像頭的預覽和資料流處理(也可以把處理型別換成視訊流,然後存本地視訊檔案,因為預設時h265編碼格式,播放時使用PotPlayer播放器來播放),算是入門了。

下一章我們將實現呼叫SDK來實現人臉自動抓拍效果。

相關文章