本文提供 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 內部訊息。