文章目录
- 一、海康摄像头SDK官网下载
- 二、海康摄像头SDK在WinForm/WPF中使用
- 三、常用接口函数,封装调用函数
一、海康摄像头SDK官网下载
1、进入海康ws官网,下载官网SDK和相关文档
2、根据自己应用程序是32位还是64位下载相应的SDK文件。
3、解压下载的SDK到本地,最好是非C盘。
在线SDK工作手册HaiK的SDK工作手册
二、海康摄像头SDK在WinForm/WPF中使用
1、下载的库文件如下图,HCNetSDKCom文件夹和txt文档中介绍的库文件都要放在项目路径(bin路径下的Release或者Debug文件夹)下。
2、在项目中加入CHCNetSDK类。SDK 有一万多行,以下只列举CHCNetSDK类的开始部分和结束部分。
以下仅为示例的代码片段,要查看全部CHCNetSDK类,参考前面第一部分从官网下载SDK包,其中有CHCNetSDK类
using System;
using System.Runtime.InteropServices;
namespace NVRCsharpDemo
{
public class CHCNetSDK
{
public CHCNetSDK()
{
}
public const int SDK_PLAYMPEG4 = 1;
public const int SDK_HCNETSDK = 2;
public const int NAME_LEN = 32;
public const int PASSWD_LEN = 16;
public const int GUID_LEN = 16;
public const int DEV_TYPE_NAME_LEN = 24;
public const int MAX_NAMELEN = 16;
public const int MAX_RIGHT = 32;
public const int SERIALNO_LEN = 48;
public const int MACADDR_LEN = 6;
public const int MAX_ETHERNET = 2;
public const int MAX_NETWORK_CARD = 4;
public const int PATHNAME_LEN = 128;
public const int MAX_NUMBER_LEN = 32;
public const int MAX_NAME_LEN = 128;
public const int MAX_TIMESEGMENT_V30 = 8;
public const int MAX_TIMESEGMENT = 4;
public const int MAX_ICR_NUM = 8;
public const int MAX_SHELTERNUM = 4;
public const int PHONENUMBER_LEN = 32;
public const int MAX_DISKNUM = 16;
public const int MAX_DISKNUM_V10 = 8;
public const int MAX_WINDOW_V30 = 32;
public const int MAX_WINDOW = 16;
public const int MAX_VGA_V30 = 4;
public const int MAX_VGA = 1;
public const int MAX_USERNUM_V30 = 32;
public const int MAX_USERNUM = 16;
public const int MAX_EXCEPTIONNUM_V30 = 32;
public const int MAX_EXCEPTIONNUM = 16;
public const int MAX_LINK = 6;
public const int MAX_ITC_EXCEPTIONOUT = 32;
public const int MAX_DECPOOLNUM = 4;
public const int MAX_DECNUM = 4;
public const int MAX_TRANSPARENTNUM = 2;
public const int MAX_CYCLE_CHAN = 16;
public const int MAX_CYCLE_CHAN_V30 = 64;
public const int MAX_DIRNAME_LENGTH = 80;
public const int MAX_STRINGNUM_V30 = 8;
public const int MAX_STRINGNUM = 4;
public const int MAX_STRINGNUM_EX = 8;
public const int MAX_AUXOUT_V30 = 16;
public const int MAX_AUXOUT = 4;
public const int MAX_HD_GROUP = 16;
public const int MAX_NFS_DISK = 8;
public const int IW_ESSID_MAX_SIZE = 32;
public const int IW_ENCODING_TOKEN_MAX = 32;
public const int WIFI_WEP_MAX_KEY_COUNT = 4;
public const int WIFI_WEP_MAX_KEY_LENGTH = 33;
public const int WIFI_WPA_PSK_MAX_KEY_LENGTH = 63;
public const int WIFI_WPA_PSK_MIN_KEY_LENGTH = 8;
public const int WIFI_MAX_AP_COUNT = 20;
public const int MAX_SERIAL_NUM = 64;
public const int MAX_DDNS_NUMS = 10;
public const int MAX_EMAIL_ADDR_LEN = 48;
public const int MAX_EMAIL_PWD_LEN = 32;
public const int MAXPROGRESS = 100;
public const int MAX_SERIALNUM = 2;
public const int CARDNUM_LEN = 20;
public const int CARDNUM_LEN_OUT = 32;
public const int MAX_VIDEOOUT_V30 = 4;
public const int MAX_VIDEOOUT = 2;
public const int MAX_PRESET_V30 = 256;
public const int MAX_TRACK_V30 = 256;
public const int MAX_CRUISE_V30 = 256;
public const int MAX_PRESET = 128;
public const int MAX_TRACK = 128;
public const int MAX_CRUISE = 128;
public const int CRUISE_MAX_PRESET_NUMS = 32;
public const int MAX_SERIAL_PORT = 8;
public const int MAX_PREVIEW_MODE = 8;
public const int MAX_MATRIXOUT = 16;
public const int LOG_INFO_LEN = 11840;
public const int DESC_LEN = 16;
public const int PTZ_PROTOCOL_NUM = 200;
public const int MAX_AUDIO = 1;
public const int MAX_AUDIO_V30 = 2;
public const int MAX_CHANNUM = 16;
public const int MAX_ALARMIN = 16;
public const int MAX_ALARMOUT = 4;
public const int MAX_ANALOG_CHANNUM = 32;
public const int MAX_ANALOG_ALARMOUT = 32;
public const int MAX_ANALOG_ALARMIN = 32;
public const int MAX_IP_DEVICE = 32;
public const int MAX_IP_DEVICE_V40 = 64;
public const int MAX_IP_CHANNEL = 32;
public const int MAX_IP_ALARMIN = 128;
public const int MAX_IP_ALARMOUT = 64;
public const int MAX_IP_ALARMIN_V40 = 4096;
public const int MAX_IP_ALARMOUT_V40 = 4096;
public const int MAX_RECORD_FILE_NUM = 20;
public const int MAX_ATM_NUM = 1;
public const int MAX_ACTION_TYPE = 12;
public const int ATM_FRAMETYPE_NUM = 4;
public const int MAX_ATM_PROTOCOL_NUM = 1025;
public const int ATM_PROTOCOL_SORT = 4;
public const int ATM_DESC_LEN = 32;
public const int MAX_CHANNUM_V30 = MAX_ANALOG_CHANNUM + MAX_IP_CHANNEL;
public const int MAX_ALARMOUT_V30 = MAX_ANALOG_ALARMOUT + MAX_IP_ALARMOUT;
public const int MAX_ALARMIN_V30 = MAX_ANALOG_ALARMIN + MAX_IP_ALARMIN;
public const int MAX_CHANNUM_V40 = 512;
public const int MAX_ALARMOUT_V40 = MAX_IP_ALARMOUT_V40 + MAX_ANALOG_ALARMOUT;
public const int MAX_ALARMIN_V40 = MAX_IP_ALARMIN_V40 + MAX_ANALOG_ALARMOUT;
public const int MAX_MULTI_AREA_NUM = 24;
public const int MAX_HUMAN_PICTURE_NUM = 10;
public const int MAX_HUMAN_BIRTHDATE_LEN = 10;
public const int MAX_LAYERNUMS = 32;
public const int MAX_ROIDETECT_NUM = 8;
public const int MAX_LANERECT_NUM = 5;
public const int MAX_FORTIFY_NUM = 10;
public const int MAX_INTERVAL_NUM = 4;
public const int MAX_CHJC_NUM = 3;
public const int MAX_VL_NUM = 5;
public const int MAX_DRIVECHAN_NUM = 16;
public const int MAX_COIL_NUM = 3;
public const int MAX_SIGNALLIGHT_NUM = 6;
public const int LEN_32 = 32;
public const int LEN_31 = 31;
public const int MAX_CABINET_COUNT = 8;
public const int MAX_ID_LEN = 48;
public const int MAX_PARKNO_LEN = 16;
public const int MAX_ALARMREASON_LEN = 32;
public const int MAX_UPGRADE_INFO_LEN= 48;
public const int MAX_CUSTOMDIR_LEN = 32;
public const int MAX_TRANSPARENT_CHAN_NUM = 4;
public const int MAX_TRANSPARENT_ACCESS_NUM = 4;
public const int MAX_PARKING_STATUS = 8;
public const int MAX_PARKING_NUM = 4;
public const int MAX_ITS_SCENE_NUM = 16;
public const int MAX_SCENE_TIMESEG_NUM = 16;
public const int MAX_IVMS_IP_CHANNEL = 128;
public const int DEVICE_ID_LEN = 48;
public const int MONITORSITE_ID_LEN = 48;
public const int MAX_AUXAREA_NUM = 16;
public const int MAX_SLAVE_CHANNEL_NUM = 16;
public const int MAX_SCH_TASKS_NUM = 10;
public const int MAX_SERVERID_LEN = 64;
public const int MAX_SERVERDOMAIN_LEN = 128;
public const int MAX_AUTHENTICATEID_LEN = 64;
public const int MAX_AUTHENTICATEPASSWD_LEN = 32;
public const int MAX_SERVERNAME_LEN = 64;
public const int MAX_COMPRESSIONID_LEN = 64;
public const int MAX_SIPSERVER_ADDRESS_LEN = 128;
public const int MAX_PlATE_NO_LEN = 32;
public const int UPNP_PORT_NUM = 12;
public const int MAX_LOCAL_ADDR_LEN = 96;
public const int MAX_COUNTRY_NAME_LEN = 4;
public const int THERMOMETRY_ALARMRULE_NUM = 40;
public const int ACS_CARD_NO_LEN = 32;
public const int MAX_ID_NUM_LEN = 32;
public const int MAX_ID_NAME_LEN = 128;
public const int MAX_ID_ADDR_LEN = 280;
public const int MAX_ID_ISSUING_AUTHORITY_LEN = 128;
public const int MAX_CARD_RIGHT_PLAN_NUM = 4;
public const int MAX_GROUP_NUM_128 = 128;
public const int MAX_CARD_READER_NUM = 64;
public const int MAX_SNEAK_PATH_NODE = 8;
public const int MAX_MULTI_DOOR_INTERLOCK_GROUP = 8;
public const int MAX_INTER_LOCK_DOOR_NUM = 8;
public const int MAX_CASE_SENSOR_NUM = 8;
public const int MAX_DOOR_NUM_256 = 256;
public const int MAX_READER_ROUTE_NUM = 16;
public const int MAX_FINGER_PRINT_NUM = 10;
public const int MAX_CARD_READER_NUM_512 = 512;
public const int NET_SDK_MULTI_CARD_GROUP_NUM_20 = 20;
public const int CARD_PASSWORD_LEN = 8;
public const int MAX_DOOR_CODE_LEN = 8;
public const int MAX_LOCK_CODE_LEN = 8;
public const int MAX_NOTICE_NUMBER_LEN = 32;
public const int MAX_NOTICE_THEME_LEN = 64;
public const int MAX_NOTICE_DETAIL_LEN = 1024;
public const int MAX_NOTICE_PIC_NUM = 6;
public const int MAX_DEV_NUMBER_LEN = 32;
public const int LOCK_NAME_LEN = 32;
public const int NET_SDK_EMPLOYEE_NO_LEN = 32;
public const int VCA_MAX_POLYGON_POINT_NUM = 10;
public const int MAX_RULE_NUM = 8;
public const int MAX_TARGET_NUM = 30;
public const int MAX_CALIB_PT = 6;
public const int MIN_CALIB_PT = 4;
public const int MAX_TIMESEGMENT_2 = 2;
public const int MAX_LICENSE_LEN = 16;
public const int MAX_PLATE_NUM = 3;
public const int MAX_MASK_REGION_NUM = 4;
public const int MAX_SEGMENT_NUM = 6;
public const int MIN_SEGMENT_NUM = 3;
public const int MAX_CATEGORY_LEN = 8;
public const int SERIAL_NO_LEN = 16;
public const int NORMALCONNECT = 1;
public const int MEDIACONNECT = 2;
public const int HCDVR = 1;
public const int MEDVR = 2;
public const int PCDVR = 3;
public const int HC_9000 = 4;
public const int HF_I = 5;
public const int PCNVR = 6;
public const int HC_76NVR = 8;
public const int DS8000HC_NVR = 0;
public const int DS9000HC_NVR = 1;
public const int DS8000ME_NVR = 2;
#region 录像库
public const int DATASTREAM_HEAD = 0;
public const int DATASTREAM_BITBLOCK = 1;
public const int DATASTREAM_KEYFRAME = 2;
public const int DATASTREAM_NORMALFRAME = 3;
public const int MESSAGEVALUE_DISKFULL = 0x01;
public const int MESSAGEVALUE_SWITCHDISK = 0x02;
public const int MESSAGEVALUE_CREATEFILE = 0x03;
public const int MESSAGEVALUE_DELETEFILE = 0x04;
public const int MESSAGEVALUE_SWITCHFILE = 0x05;
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct STOREINFO
{
public int iMaxChannels;
public int iDiskGroup;
public int iStreamType;
public bool bAnalyze;
public bool bCycWrite;
public uint uiFileSize;
public CALLBACKFUN_MESSAGE funCallback;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct CREATEFILE_INFO
{
public int iHandle;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string strCameraid;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string strFileName;
public BLOCKTIME tFileCreateTime;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct CLOSEFILE_INFO
{
public int iHandle;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string strCameraid;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string strFileName;
public BLOCKTIME tFileSwitchTime;
}
public delegate int CALLBACKFUN_MESSAGE(int iMessageType, System.IntPtr pBuf, int iBufLen);
[DllImport("RecordDLL.dll")]
public static extern int Initialize(STOREINFO struStoreInfo);
[DllImport("RecordDLL.dll")]
public static extern int Release();
[DllImport("RecordDLL.dll")]
public static extern int OpenChannelRecord(string strCameraid, IntPtr pHead, uint dwHeadLength);
[DllImport("RecordDLL.dll")]
public static extern bool CloseChannelRecord(int iRecordHandle);
[DllImport("RecordDLL.dll")]
public static extern int GetData(int iHandle, int iDataType, IntPtr pBuf, uint uiSize);
#endregion
}
3、主程序中调用需要加入命名空间using NVRCsharpDemo;
三、常用接口函数,封装调用函数
摄像头的连接、显示、 控制等功能在CHCNetSDK类中都有,以下为常见功能接口的调用。这里是开辟了一个摄像头模块,可以单独放在一个类中,也可以放在使用摄像头的类中。
这些接口的调用是放在新建的函数中,而非直接使用SDK中的接口函数。下面的代码可直接使用,一些地方比如显示摄像头的控件名、文件存放的路径等可能需要修改。
这段代码是在WPF中使用的,在WinForm中也可使用,需微调。
#region 摄像机
#region 变量
private bool m_bInitSDK = false;
private bool m_bRecord = false;
private uint iLastErr = 0;
private Int32 m_lUserID = -1;
private Int32 m_lRealHandle = -1;
private string str1;
private string str2;
private Int32 i = 0;
private Int32 m_lTree = 0;
private string str;
private long iSelIndex = 0;
private uint dwAChanTotalNum = 0;
private uint dwDChanTotalNum = 0;
private Int32 m_lPort = -1;
private IntPtr m_ptrRealHandle;
private int[] iIPDevID = new int[96];
private int[] iChannelNum = new int[96];
private CHCNetSDK.REALDATACALLBACK RealData = null;
public CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
public CHCNetSDK.NET_DVR_IPPARACFG_V40 m_struIpParaCfgV40;
public CHCNetSDK.NET_DVR_STREAM_MODE m_struStreamMode;
public CHCNetSDK.NET_DVR_IPCHANINFO m_struChanInfo;
public CHCNetSDK.NET_DVR_PU_STREAM_URL m_struStreamURL;
public CHCNetSDK.NET_DVR_IPCHANINFO_V40 m_struChanInfoV40;
private PlayCtrl.DECCBFUN m_fDisplayFun = null;
public delegate void MyDebugInfo(string str);
#endregion 变量
#region ip配置
public string DVRIPAddress1 = "172.16.0.53";
public Int16 DVRPortNumber1 = 8000;
public string DVRUserName1 = "admin";
public string DVRPassword1 = "******";
public string DVRIPAddress2 = "172.16.0.50";
public Int16 DVRPortNumber2 = 8000;
public string DVRUserName2 = "admin";
public string DVRPassword2 = "******";
public string DVRIPAddress3 = "172.16.0.52";
public Int16 DVRPortNumber3 = 8000;
public string DVRUserName3 = "admin";
public string DVRPassword3 = "******";
#endregion ip配置
#region 报警信息
string InfoWarning = "";
public void DebugInfo(string str)
{
if (str.Length > 0)
{
str += "\n";
InfoWarning = InfoWarning + str + "\n";
}
}
#endregion 报警信息
#region 初始化相机
public int InitCamera()
{
try
{
XDocument document = XDocument.Load("Parameter.xml");
XElement root = document.Root;
DVRIPAddress1 = root.Element("CameraIP1").Value.ToString();
DVRPortNumber1 = Convert.ToInt16(root.Element("CameraPort1").Value.ToString());
DVRUserName1 = root.Element("CameraUser1").Value.ToString();
DVRPassword1 = root.Element("CameraPwd1").Value.ToString();
DVRIPAddress2 = root.Element("CameraIP2").Value.ToString();
DVRPortNumber2 = Convert.ToInt16(root.Element("CameraPort2").Value.ToString());
DVRUserName2 = root.Element("CameraUser2").Value.ToString();
DVRPassword2 = root.Element("CameraPwd2").Value.ToString();
DVRIPAddress3 = root.Element("CameraIP3").Value.ToString();
DVRPortNumber3 = Convert.ToInt16(root.Element("CameraPort3").Value.ToString());
DVRUserName3 = root.Element("CameraUser3").Value.ToString();
DVRPassword3 = root.Element("CameraPwd3").Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show("用户登录信息错误!");
return -1;
}
return 0;
}
#endregion 初始化相机
#region 登录Login
public void Login(string ip, int port, string username, string password)
{
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
MessageBox.Show("NET_DVR_Init error!");
return;
}
else
{
CHCNetSDK.NET_DVR_SetLogToFile(3, "C:\\CameraSdkLog\\", true);
comboBoxView.SelectedIndex = 0;
for (int i = 0; i < 64; i++)
{
iIPDevID[i] = -1;
iChannelNum[i] = -1;
}
}
if (m_lUserID < 0)
{
m_lUserID = CHCNetSDK.NET_DVR_Login_V30(ip, port, username, password, ref DeviceInfo);
if (m_lUserID < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = " NET_DVR_Login_V30 failed, error code= " + iLastErr;
DebugInfo(str);
return;
}
else
{
DebugInfo(" NET_DVR_Login_V30 succ!");
MessageBox.Show("相机登录成功!");
dwAChanTotalNum = (uint)DeviceInfo.byChanNum;
dwDChanTotalNum = (uint)DeviceInfo.byIPChanNum + 256 * (uint)DeviceInfo.byHighDChanNum;
if (dwDChanTotalNum > 0)
{
InfoIPChannel();
}
else
{
for (i = 0; i < dwAChanTotalNum; i++)
{
ListAnalogChannel(i + 1, 1);
iChannelNum[i] = i + (int)DeviceInfo.byStartChan;
}
comboBoxView.SelectedItem = 1;
}
}
}
else
{
if (m_lRealHandle >= 0)
{
DebugInfo(" Please stop live view firstly");
return;
}
if (!CHCNetSDK.NET_DVR_Logout(m_lUserID))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = " NET_DVR_Logout failed, error code= " + iLastErr;
DebugInfo(str);
return;
}
DebugInfo(" NET_DVR_Logout succ!");
m_lUserID = -1;
MessageBox.Show("Login");
}
return;
}
public void InfoIPChannel()
{
uint dwSize = (uint)Marshal.SizeOf(m_struIpParaCfgV40);
IntPtr ptrIpParaCfgV40 = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40, ptrIpParaCfgV40, false);
uint dwReturn = 0;
int iGroupNo = 0;
if (!CHCNetSDK.NET_DVR_GetDVRConfig(m_lUserID, CHCNetSDK.NET_DVR_GET_IPPARACFG_V40, iGroupNo, ptrIpParaCfgV40, dwSize, ref dwReturn))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = " NET_DVR_GET_IPPARACFG_V40 failed, error code= " + iLastErr;
DebugInfo(str);
}
else
{
DebugInfo(" NET_DVR_GET_IPPARACFG_V40 succ!");
m_struIpParaCfgV40 = (CHCNetSDK.NET_DVR_IPPARACFG_V40)Marshal.PtrToStructure(ptrIpParaCfgV40, typeof(CHCNetSDK.NET_DVR_IPPARACFG_V40));
for (i = 0; i < dwAChanTotalNum; i++)
{
ListAnalogChannel(i + 1, m_struIpParaCfgV40.byAnalogChanEnable[i]);
iChannelNum[i] = i + (int)DeviceInfo.byStartChan;
}
byte byStreamType = 0;
uint iDChanNum = 64;
if (dwDChanTotalNum < 64)
{
iDChanNum = dwDChanTotalNum;
}
for (i = 0; i < iDChanNum; i++)
{
iChannelNum[i + dwAChanTotalNum] = i + (int)m_struIpParaCfgV40.dwStartDChan;
byStreamType = m_struIpParaCfgV40.struStreamMode[i].byGetStreamType;
dwSize = (uint)Marshal.SizeOf(m_struIpParaCfgV40.struStreamMode[i].uGetStream);
switch (byStreamType)
{
case 0:
IntPtr ptrChanInfo = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40.struStreamMode[i].uGetStream, ptrChanInfo, false);
m_struChanInfo = (CHCNetSDK.NET_DVR_IPCHANINFO)Marshal.PtrToStructure(ptrChanInfo, typeof(CHCNetSDK.NET_DVR_IPCHANINFO));
ListIPChannel(i + 1, m_struChanInfo.byEnable, m_struChanInfo.byIPID);
iIPDevID[i] = m_struChanInfo.byIPID + m_struChanInfo.byIPIDHigh * 256 - iGroupNo * 64 - 1;
Marshal.FreeHGlobal(ptrChanInfo);
break;
case 4:
IntPtr ptrStreamURL = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40.struStreamMode[i].uGetStream, ptrStreamURL, false);
m_struStreamURL = (CHCNetSDK.NET_DVR_PU_STREAM_URL)Marshal.PtrToStructure(ptrStreamURL, typeof(CHCNetSDK.NET_DVR_PU_STREAM_URL));
ListIPChannel(i + 1, m_struStreamURL.byEnable, m_struStreamURL.wIPID);
iIPDevID[i] = m_struStreamURL.wIPID - iGroupNo * 64 - 1;
Marshal.FreeHGlobal(ptrStreamURL);
break;
case 6:
IntPtr ptrChanInfoV40 = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40.struStreamMode[i].uGetStream, ptrChanInfoV40, false);
m_struChanInfoV40 = (CHCNetSDK.NET_DVR_IPCHANINFO_V40)Marshal.PtrToStructure(ptrChanInfoV40, typeof(CHCNetSDK.NET_DVR_IPCHANINFO_V40));
ListIPChannel(i + 1, m_struChanInfoV40.byEnable, m_struChanInfoV40.wIPID);
iIPDevID[i] = m_struChanInfoV40.wIPID - iGroupNo * 64 - 1;
Marshal.FreeHGlobal(ptrChanInfoV40);
break;
default:
break;
}
}
}
Marshal.FreeHGlobal(ptrIpParaCfgV40);
}
public void ListIPChannel(Int32 iChanNo, byte byOnline, int byIPID)
{
str1 = String.Format("IPCamera {0}", iChanNo);
m_lTree++;
if (byIPID == 0)
{
str2 = "X";
}
else
{
if (byOnline == 0)
{
str2 = "offline";
}
else
str2 = "online";
}
}
public void ListAnalogChannel(Int32 iChanNo, byte byEnable)
{
str1 = String.Format("Camera {0}", iChanNo);
m_lTree++;
if (byEnable == 0)
{
str2 = "Disabled";
}
else
{
str2 = "Enabled";
}
}
#endregion 登录Login
#region 打开预览摄像机
public int StartPreview(System.Windows.Forms.PictureBox pictureShow)
{
if (m_lUserID < 0)
{
MessageBox.Show("Please login the device firstly!");
return -1;
}
if (m_bRecord)
{
MessageBox.Show("Please stop recording firstly!");
return -1;
}
if (m_lRealHandle < 0)
{
CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
lpPreviewInfo.hPlayWnd = pictureShow.Handle;
lpPreviewInfo.lChannel = iChannelNum[(int)iSelIndex];
lpPreviewInfo.dwStreamType = 0;
lpPreviewInfo.dwLinkMode = 0;
lpPreviewInfo.bBlocked = true;
lpPreviewInfo.dwDisplayBufNum = 15;
IntPtr pUser = IntPtr.Zero;
if (comboBoxView.SelectedIndex == 0)
{
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, null, pUser);
}
else
{
lpPreviewInfo.hPlayWnd = IntPtr.Zero;
m_ptrRealHandle = pictureShow.Handle;
RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, RealData, pUser);
}
if (m_lRealHandle < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = " NET_DVR_RealPlay_V40 failed, error code= " + iLastErr;
DebugInfo(str);
return -1;
}
else
{
DebugInfo(" NET_DVR_RealPlay_V40 succ!");
MessageBox.Show("Stop View");
return 0;
}
}
else
{
if (!CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = " NET_DVR_StopRealPlay failed, error code= " + iLastErr;
DebugInfo(str);
return -1;
}
if ((comboBoxView.SelectedIndex == 1) && (m_lPort >= 0))
{
if (!PlayCtrl.PlayM4_Stop(m_lPort))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = " PlayM4_Stop failed, error code= " + iLastErr;
DebugInfo(str);
}
if (!PlayCtrl.PlayM4_CloseStream(m_lPort))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = " PlayM4_CloseStream failed, error code= " + iLastErr;
DebugInfo(str);
}
if (!PlayCtrl.PlayM4_FreePort(m_lPort))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = " PlayM4_FreePort failed, error code= " + iLastErr;
DebugInfo(str);
}
m_lPort = -1;
}
DebugInfo(" NET_DVR_StopRealPlay succ!");
m_lRealHandle = -1;
MessageBox.Show("Live View");
pictureShow.Invalidate();
}
return 0;
}
#endregion 打开预览摄像机
#region 停止预览摄像机
public void StopPreview()
{
if (m_lRealHandle >= 0)
{
CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
m_lRealHandle = -1;
}
}
#endregion 停止预览摄像机
#region 退出登录
public void ExitLogin()
{
if (m_lUserID >= 0)
{
CHCNetSDK.NET_DVR_Logout(m_lUserID);
m_lUserID = -1;
}
CHCNetSDK.NET_DVR_Cleanup();
}
#endregion 退出登录
#region 回调函数
private void DecCallbackFUN(int nPort, IntPtr pBuf, int nSize, ref PlayCtrl.FRAME_INFO pFrameInfo, int nReserved1, int nReserved2)
{
if (pFrameInfo.nType == 3)
{
}
}
public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
switch (dwDataType)
{
case CHCNetSDK.NET_DVR_SYSHEAD:
if (dwBufSize > 0)
{
if (m_lPort >= 0)
{
return;
}
if (!PlayCtrl.PlayM4_GetPort(ref m_lPort))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_GetPort failed, error code= " + iLastErr;
this.Dispatcher.BeginInvoke(AlarmInfo, str);
break;
}
if (!PlayCtrl.PlayM4_SetStreamOpenMode(m_lPort, PlayCtrl.STREAME_REALTIME))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "Set STREAME_REALTIME mode failed, error code= " + iLastErr;
this.Dispatcher.BeginInvoke(AlarmInfo, str);
}
if (!PlayCtrl.PlayM4_OpenStream(m_lPort, pBuffer, dwBufSize, 2 * 1024 * 1024))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_OpenStream failed, error code= " + iLastErr;
this.Dispatcher.BeginInvoke(AlarmInfo, str);
break;
}
if (!PlayCtrl.PlayM4_SetDisplayBuf(m_lPort, 15))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_SetDisplayBuf failed, error code= " + iLastErr;
this.Dispatcher.BeginInvoke(AlarmInfo, str);
}
if (!PlayCtrl.PlayM4_SetOverlayMode(m_lPort, 0, 0))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_SetOverlayMode failed, error code= " + iLastErr;
this.Dispatcher.BeginInvoke(AlarmInfo, str);
}
m_fDisplayFun = new PlayCtrl.DECCBFUN(DecCallbackFUN);
if (!PlayCtrl.PlayM4_SetDecCallBackEx(m_lPort, m_fDisplayFun, IntPtr.Zero, 0))
{
this.Dispatcher.BeginInvoke(AlarmInfo, "PlayM4_SetDisplayCallBack fail");
}
if (!PlayCtrl.PlayM4_Play(m_lPort, m_ptrRealHandle))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_Play failed, error code= " + iLastErr;
this.Dispatcher.BeginInvoke(AlarmInfo, str);
break;
}
}
break;
case CHCNetSDK.NET_DVR_STREAMDATA:
if (dwBufSize > 0 && m_lPort != -1)
{
for (int i = 0; i < 999; i++)
{
if (!PlayCtrl.PlayM4_InputData(m_lPort, pBuffer, dwBufSize))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_InputData failed, error code= " + iLastErr;
Thread.Sleep(2);
}
else
{
break;
}
}
}
break;
default:
if (dwBufSize > 0 && m_lPort != -1)
{
for (int i = 0; i < 999; i++)
{
if (!PlayCtrl.PlayM4_InputData(m_lPort, pBuffer, dwBufSize))
{
iLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
str = "PlayM4_InputData failed, error code= " + iLastErr;
Thread.Sleep(2);
}
else
{
break;
}
}
}
break;
}
}
#endregion 回调函数
#region 控制视频方向
public bool TopMouseDown()
{
bool a = CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.TILT_UP, 0);
return a;
}
public bool TopMouseUp()
{
bool b=CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.TILT_UP, 1);
return b;
}
public bool BottomMouseDown()
{
return CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.TILT_DOWN, 0);
}
public bool BottomMouseUp()
{
return CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.TILT_DOWN, 1);
}
public bool LeftMouseDown()
{
return CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.PAN_LEFT, 0);
}
public bool LeftMouseUp()
{
return CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.PAN_LEFT, 1);
}
public bool RightMouseDown()
{
return CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.PAN_RIGHT, 0);
}
public bool RightMouseUp()
{
return CHCNetSDK.NET_DVR_PTZControl(m_lRealHandle, CHCNetSDK.PAN_RIGHT, 1);
}
#endregion 控制视频方向
private void btnStartCameraMin_Click(object sender, RoutedEventArgs e)
{
if (txtStartStopMin.Text == "暂停")
{
BitmapImage img1 = new BitmapImage(new Uri("pack://application:,,,/Resource/资源0001.png"));
BitmapImage img = new BitmapImage(new Uri("pack://application:,,,/Resource/资源 99.png"));
imgStartStopMin.Source = img;
txtStartStopMin.Text = "播放";
StopPreview();
MessageBox.Show("暂停当前选中的视频!");
}
else if (txtStartStopMin.Text == "播放")
{
BitmapImage img = new BitmapImage(new Uri("pack://application:,,,/Resource/资源 102.png"));
imgStartStopMin.Source = img;
txtStartStopMin.Text = "暂停";
StartPreview(pnlCameraMinC1);
StartPreview(pnlCameraMinC2);
StartPreview(pnlCameraMinC3);
MessageBox.Show("播放当前选中的视频!");
}
}
#endregion 摄像机
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)