首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 視訊監控系列(7):服務端 SDK 封裝(下)——DS40xxSDK.dll 核心 API

     編輯:關於C#

本文提供 DS40xxSDK.dll(板卡底層控制)的 C# P/Invoke 封裝,涵蓋 DSP 初始化、通道開啟/關閉、視訊參數設定、碼流回調註冊、I 幀請求、OSD、擷取卡資訊等核心 API,供服務端開發直接引用。

封裝類別架構


using System;
using System.Runtime.InteropServices;

namespace HikVisionSDK
{
    public static class DS40xxSDK
    {
        // === 結構體 ===
        [StructLayout(LayoutKind.Sequential)]
        public struct DS_BOARD_DETAIL
        {
            public byte type;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
            public byte[] sn;
            // ... 其他欄位
        }

        // === 核心 API ===
        [DllImport("DS40xxSDK.dll")]
        public static extern int InitDSPs();

        [DllImport("DS40xxSDK.dll")]
        public static extern int GetTotalDSPs();

        [DllImport("DS40xxSDK.dll")]
        public static extern IntPtr ChannelOpen(int nChannel);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool ChannelClose(IntPtr hChannel);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool StartVideoCapture(IntPtr hChannel);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool StopVideoCapture(IntPtr hChannel);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool CaptureIFrame(IntPtr hChannel);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool SetIBPMode(IntPtr hChannel, int nIBP, int nBFrame, int nPFrame, int nFrameRate);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool SetDefaultQuant(IntPtr hChannel, int nIQuant, int nPQuant, int nBQuant);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool SetEncoderPictureFormat(IntPtr hChannel, PictureFormat format);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool StartVideoPreview(IntPtr hChannel, IntPtr hWnd, ref Rectangle rect, bool bOverlay, int nVideoFormat, int nFrameRate);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool StopVideoPreview(IntPtr hChannel);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool RegisterStreamDirectReadCallback(STREAM_DIRECT_READ_CALLBACK cb, IntPtr hWnd);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool GetJpegImage(IntPtr hChannel, byte[] imageBuf, out int size, uint quality);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool GetOriginalImage(IntPtr hChannel, byte[] imageBuf, out int size);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool SetOsdDisplayMode(IntPtr hChannel, byte nRegion, bool bEnable, int nFont, ushort[] format1, ushort[] format2);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool SetOsd(IntPtr hChannel, bool bEnable);

        [DllImport("DS40xxSDK.dll")]
        public static extern uint GetBoardCount();

        [DllImport("DS40xxSDK.dll")]
        public static extern bool GetBoardDetail(uint boardIndex, ref DS_BOARD_DETAIL detail);

        [DllImport("DS40xxSDK.dll")]
        public static extern bool SetDefaultVideoStandard(VideoStandard standard);

        // === 回調委派 ===
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate int STREAM_DIRECT_READ_CALLBACK(int channelNum, IntPtr dataBuf, int length, FrameType frameType, IntPtr context);

        // === 枚舉 ===
        public enum PictureFormat { ENC_QCIF_FORMAT, ENC_CIF_FORMAT, ENC_DCIF_FORMAT, ENC_4CIF_FORMAT }
        public enum VideoStandard { StandardNTSC = 0, StandardPAL = 1 }
        public enum FrameType { PktSysHeader = 1, PktIFrames = 2, PktPFrames = 3, PktBFrames = 4, PktAudioFrames = 5, PktSubSysHeader = 6, PktSubIFrames = 7, PktSubPFrames = 8, PktSubBBPFrames = 9, PktMotionDetection = 10, PktOrigImage = 11 }
        public enum VideoFormat { vdfYUV422Planar = 0, vdfRGB16 = 1, vdfRGB24 = 2, vdfRGB32 = 3, vdfYUV420Planar = 4 }
    }
}

使用提醒

  • 所有回調委派 必須標記 [UnmanagedFunctionPointer(CallingConvention.Cdecl)],否則參數堆疊會錯位。
  • 結構體 DS_BOARD_DETAIL 的 sn 陣列實際僅前 12 位有效,後 4 位為 0。
  • RegisterStreamDirectReadCallback 的 hWnd 傳遞表單 Handle,用於接收 SDK 內部訊息。
  • API 參數型別若有誤差,請參照 VC++ 範例與 SDK 文件微調,本封裝會隨系列文章持續更新。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved