實現客戶端截圖功能:設置回調、回調中保存 BMP、立即取消回調防止重複觸發。
private const int T_UYVY = 1, T_YV12 = 3, T_RGB32 = 7;
private CapPicFun _capCallback; // 必須為成員變量防 GC
private void Form1_Load(object sender, EventArgs e) {
_capCallback = new CapPicFun(OnCapPic);
}
private void btnSnapshot_Click(object sender, EventArgs e) {
PcHikClient.MP4_ClientSetCapPicCallBack(cs, _capCallback);
}
public void OnCapPic(int handle, IntPtr pBuf, int size, int w, int h, int stamp, int type, int reserved) {
string file = type switch {
T_UYVY => @"c:\uyvy.bmp",
T_YV12 => @"c:\yv12.bmp",
T_RGB32 => @"c:\rgb32.bmp",
_ => null
};
if (file != null) {
PcHikClient.MP4_ClientSavePicFile(pBuf, size, w, h, type, file);
// 立即取消回調
PcHikClient.MP4_ClientSetCapPicCallBack(cs, null);
}
}
早期用 `byte[]` 封送導致保存圖片全黑,修正為 `IntPtr` 後正常。同步更新了封裝層代碼。