编写代码对接海康威视的视频预览
时间: 2023-09-08 15:14:44 浏览: 88
对接海康威视的视频预览需要使用海康威视提供的 SDK,以下是一个简单的代码示例:
```
#include "stdafx.h"
#include "HCNetSDK.h"
#include "PlayM4.h"
#pragma comment(lib, "HCNetSDK.lib")
#pragma comment(lib, "PlayCtrl.lib")
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int main()
{
NET_DVR_Init();
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
NET_DVR_DEVICEINFO_V30 deviceInfo;
LONG lUserID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "12345", &deviceInfo);
if (lUserID < 0)
{
printf("Login failed. Error code: %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return 0;
}
NET_DVR_CLIENTINFO clientInfo = { 0 };
clientInfo.lChannel = 1;
clientInfo.lLinkMode = 0;
clientInfo.hPlayWnd = NULL;
clientInfo.sMultiCastIP = NULL;
LONG lRealPlayHandle = NET_DVR_RealPlay_V30(lUserID, &clientInfo, NULL, NULL, true);
if (lRealPlayHandle < 0)
{
printf("Real play failed. Error code: %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return 0;
}
HWND hWnd = ::CreateWindowEx(WS_EX_TRANSPARENT, TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 0, 0, 640, 480, NULL, NULL, ::GetModuleHandle(NULL), NULL);
::SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProc);
::ShowWindow(hWnd, SW_SHOW);
HWND hPlayWnd = ::GetDlgItem(hWnd, 0);
::PlayM4_SetDisplayBuf(0);
if (!::PlayM4_Play(hPlayWnd, lRealPlayHandle))
{
printf("Play failed. Error code: %d\n", ::PlayM4_GetLastError(lRealPlayHandle));
NET_DVR_StopRealPlay(lRealPlayHandle);
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return 0;
}
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
::PlayM4_Stop(lRealPlayHandle);
::PlayM4_CloseStream(lRealPlayHandle);
NET_DVR_StopRealPlay(lRealPlayHandle);
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
::PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
```
这段代码使用了海康威视提供的 SDK 连接到一个设备,并将设备的实时视频预览在窗口中显示。其中需要注意的是,这段代码使用了 Windows API 创建了一个窗口,并将实时视频渲染到该窗口中。你需要将代码中的设备 IP、用户名和密码修改为实际的值,同时也需要根据自己的需求修改窗口大小等参数。
阅读全文