实例介绍
【实例简介】抓拍机抓拍图片demo
【实例截图】
【核心代码】ystem;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
namespace PreviewDemo
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainFrame : System.Windows.Forms.Form
{
private CHCNetSDK.REALDATACALLBACK m_fRealData = null;
private CHCNetSDK.MSGCallBack m_falarmData = null;
public CHCNetSDK.NET_DVR_DEVICEINFO_V30 m_struDeviceInfo;
public CHCNetSDK.NET_DVR_SNAPCFG struSnapCfg;
public Int32 m_lUserID = -1;
private bool m_bInitSDK = false;
private Int32 m_lRealHandle = -1;
private Int32 m_lPort = -1;
private bool m_bJpegCapture = false;
private bool m_bGuard = false;
private bool m_bOpenSound = false;
private Int32 m_lFortifyHandle = -1;
private bool logoutRtn = false;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Button btnPreview;
private System.Windows.Forms.TextBox textBoxPort;
private System.Windows.Forms.TextBox textBoxIP;
private System.Windows.Forms.TextBox textBoxPassword;
private System.Windows.Forms.TextBox textBoxUserName;
private System.Windows.Forms.PictureBox RealPlayWnd;
private Button btnJpegCapture;
private IntPtr m_ptrRealHandle;
private Button btnFortify;
private RichTextBox TextBoxInfo;
private Label label5;
private Button btnOpenSound;
private Button Logout;
private Label label6;
private Button btnStopPre;
private Button Btn_CANCEL;
private Label label7;
private Label label8;
private Label label9;
private Label label10;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public MainFrame()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
MessageBox.Show("NET_DVR_Init error!");
return;
}
else
{
}
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (m_lRealHandle >= 0)
{
CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
}
if (m_lUserID >= 0)
{
CHCNetSDK.NET_DVR_Logout_V30(m_lUserID);
}
if (m_bInitSDK == true)
{
CHCNetSDK.NET_DVR_Cleanup();
}
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainFrame));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.btnLogin = new System.Windows.Forms.Button();
this.btnPreview = new System.Windows.Forms.Button();
this.textBoxPort = new System.Windows.Forms.TextBox();
this.textBoxIP = new System.Windows.Forms.TextBox();
this.textBoxPassword = new System.Windows.Forms.TextBox();
this.textBoxUserName = new System.Windows.Forms.TextBox();
this.RealPlayWnd = new System.Windows.Forms.PictureBox();
this.btnJpegCapture = new System.Windows.Forms.Button();
this.btnFortify = new System.Windows.Forms.Button();
this.TextBoxInfo = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.btnOpenSound = new System.Windows.Forms.Button();
this.Logout = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.btnStopPre = new System.Windows.Forms.Button();
this.Btn_CANCEL = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.RealPlayWnd)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(2, 342);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Device IP Address";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.Location = new System.Drawing.Point(1, 411);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 0;
this.label2.Text = "User Name";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.Location = new System.Drawing.Point(2, 440);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 16);
this.label3.TabIndex = 0;
this.label3.Text = "Password";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 376);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(113, 16);
this.label4.TabIndex = 0;
this.label4.Text = "Device Port Number";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(286, 338);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(88, 28);
this.btnLogin.TabIndex = 1;
this.btnLogin.Text = " Login";
this.btnLogin.Click = new System.EventHandler(this.btnLogin_Click);
//
// btnPreview
//
this.btnPreview.Location = new System.Drawing.Point(286, 376);
this.btnPreview.Name = "btnPreview";
this.btnPreview.Size = new System.Drawing.Size(88, 30);
this.btnPreview.TabIndex = 2;
this.btnPreview.Text = "Live view";
this.btnPreview.Click = new System.EventHandler(this.btnPreview_Click);
//
// textBoxPort
//
this.textBoxPort.Location = new System.Drawing.Point(133, 374);
this.textBoxPort.Name = "textBoxPort";
this.textBoxPort.Size = new System.Drawing.Size(103, 22);
this.textBoxPort.TabIndex = 3;
this.textBoxPort.Text = "8000";
this.textBoxPort.TextChanged = new System.EventHandler(this.textBox1_TextChanged);
//
// textBoxIP
//
this.textBoxIP.Location = new System.Drawing.Point(133, 341);
this.textBoxIP.Name = "textBoxIP";
this.textBoxIP.Size = new System.Drawing.Size(103, 22);
this.textBoxIP.TabIndex = 3;
this.textBoxIP.Text = "172.8.4.76";
this.textBoxIP.TextChanged = new System.EventHandler(this.textBoxIP_TextChanged);
//
// textBoxPassword
//
this.textBoxPassword.Location = new System.Drawing.Point(133, 438);
this.textBoxPassword.Name = "textBoxPassword";
this.textBoxPassword.Size = new System.Drawing.Size(103, 22);
this.textBoxPassword.TabIndex = 3;
this.textBoxPassword.Text = "12345";
this.textBoxPassword.UseSystemPasswordChar = true;
//
// textBoxUserName
//
this.textBoxUserName.Location = new System.Drawing.Point(133, 409);
this.textBoxUserName.Name = "textBoxUserName";
this.textBoxUserName.Size = new System.Drawing.Size(103, 22);
this.textBoxUserName.TabIndex = 3;
this.textBoxUserName.Text = "admin";
//
// RealPlayWnd
//
this.RealPlayWnd.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("RealPlayWnd.BackgroundImage")));
this.RealPlayWnd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.RealPlayWnd.Location = new System.Drawing.Point(3, 1);
this.RealPlayWnd.Name = "RealPlayWnd";
this.RealPlayWnd.Size = new System.Drawing.Size(423, 322);
this.RealPlayWnd.TabIndex = 4;
this.RealPlayWnd.TabStop = false;
//
// btnJpegCapture
//
this.btnJpegCapture.Location = new System.Drawing.Point(499, 376);
this.btnJpegCapture.Name = "btnJpegCapture";
this.btnJpegCapture.Size = new System.Drawing.Size(92, 30);
this.btnJpegCapture.TabIndex = 5;
this.btnJpegCapture.Text = "Manual Snap";
this.btnJpegCapture.UseVisualStyleBackColor = true;
this.btnJpegCapture.Click = new System.EventHandler(this.btnJpegCapture_Click);
//
// btnFortify
//
this.btnFortify.Location = new System.Drawing.Point(499, 338);
this.btnFortify.Name = "btnFortify";
this.btnFortify.Size = new System.Drawing.Size(90, 29);
this.btnFortify.TabIndex = 7;
this.btnFortify.Text = "Arm or Disarm";
this.btnFortify.UseVisualStyleBackColor = true;
this.btnFortify.Click = new System.EventHandler(this.btnFortify_Click);
//
// TextBoxInfo
//
this.TextBoxInfo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.TextBoxInfo.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TextBoxInfo.Location = new System.Drawing.Point(432, 1);
this.TextBoxInfo.Name = "TextBoxInfo";
this.TextBoxInfo.Size = new System.Drawing.Size(270, 322);
this.TextBoxInfo.TabIndex = 8;
this.TextBoxInfo.Text = "";
//
// label5
//
this.label5.AutoSize = true;
this.label5.BackColor = System.Drawing.SystemColors.Control;
this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label5.Location = new System.Drawing.Point(588, 308);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(127, 17);
this.label5.TabIndex = 9;
this.label5.Text = "Debug Info Display";
//
// btnOpenSound
//
this.btnOpenSound.Location = new System.Drawing.Point(286, 418);
this.btnOpenSound.Name = "btnOpenSound";
this.btnOpenSound.Size = new System.Drawing.Size(90, 25);
this.btnOpenSound.TabIndex = 13;
this.btnOpenSound.Text = "Open Sound";
this.btnOpenSound.UseVisualStyleBackColor = true;
this.btnOpenSound.Click = new System.EventHandler(this.btnOpenSound_Click);
//
// Logout
//
this.Logout.Location = new System.Drawing.Point(379, 338);
this.Logout.Name = "Logout";
this.Logout.Size = new System.Drawing.Size(98, 29);
this.Logout.TabIndex = 16;
this.Logout.Text = "Logout";
this.Logout.UseVisualStyleBackColor = true;
this.Logout.Click = new System.EventHandler(this.Logout_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label6.Location = new System.Drawing.Point(2, 308);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(88, 17);
this.label6.TabIndex = 17;
this.label6.Text = "Play Window";
//
// btnStopPre
//
this.btnStopPre.Location = new System.Drawing.Point(377, 376);
this.btnStopPre.Name = "btnStopPre";
this.btnStopPre.Size = new System.Drawing.Size(98, 30);
this.btnStopPre.TabIndex = 18;
this.btnStopPre.Text = "Stop Live View";
this.btnStopPre.UseVisualStyleBackColor = true;
this.btnStopPre.Click = new System.EventHandler(this.btnStopPre_Click);
//
// Btn_CANCEL
//
this.Btn_CANCEL.Location = new System.Drawing.Point(382, 419);
this.Btn_CANCEL.Name = "Btn_CANCEL";
this.Btn_CANCEL.Size = new System.Drawing.Size(209, 24);
this.Btn_CANCEL.TabIndex = 20;
this.Btn_CANCEL.Text = "Exit";
this.Btn_CANCEL.UseVisualStyleBackColor = true;
this.Btn_CANCEL.Click = new System.EventHandler(this.Btn_CANCEL_Click);
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(593, 343);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(103, 14);
this.label7.TabIndex = 21;
this.label7.Text = "布防获取抓拍图片";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(595, 385);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(79, 14);
this.label8.TabIndex = 22;
this.label8.Text = "手动触发连拍";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(248, 346);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(31, 14);
this.label9.TabIndex = 23;
this.label9.Text = "登录";
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(248, 384);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(31, 14);
this.label10.TabIndex = 24;
this.label10.Text = "预览";
//
// MainFrame
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(705, 466);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.Btn_CANCEL);
this.Controls.Add(this.btnStopPre);
this.Controls.Add(this.label6);
this.Controls.Add(this.Logout);
this.Controls.Add(this.btnOpenSound);
this.Controls.Add(this.label5);
this.Controls.Add(this.TextBoxInfo);
this.Controls.Add(this.btnFortify);
this.Controls.Add(this.btnJpegCapture);
this.Controls.Add(this.RealPlayWnd);
this.Controls.Add(this.textBoxPort);
this.Controls.Add(this.btnPreview);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBoxIP);
this.Controls.Add(this.textBoxPassword);
this.Controls.Add(this.textBoxUserName);
this.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Location = new System.Drawing.Point(50, 0);
this.Name = "MainFrame";
this.Text = "Hikvison Client SDK Demo";
((System.ComponentModel.ISupportInitialize)(this.RealPlayWnd)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainFrame());
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void btnLogin_Click(object sender, System.EventArgs e)
{
if (textBoxIP.Text == "" || textBoxPort.Text == "" ||
textBoxUserName.Text == "" || textBoxPassword.Text == "")
{
MessageBox.Show("Please input prarameters: ");
return;
}
string DVRIPAddress = textBoxIP.Text;
Int16 DVRPortNumber = Int16.Parse(textBoxPort.Text);
string DVRUserName = textBoxUserName.Text;
string DVRPassword = textBoxPassword.Text;
m_lUserID = CHCNetSDK.NET_DVR_Login_V30(DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref m_struDeviceInfo);
if (m_lUserID == -1)
{
MessageBox.Show("login error!");
return;
}
else
{
logoutRtn = false;
MessageBox.Show("Login Success!");
}
}
private void btnPreview_Click(object sender, System.EventArgs e)
{
CHCNetSDK.NET_DVR_CLIENTINFO lpClientInfo = new CHCNetSDK.NET_DVR_CLIENTINFO();
lpClientInfo.lChannel = 1;
lpClientInfo.lLinkMode = 0x0000;
lpClientInfo.sMultiCastIP = "";
lpClientInfo.hPlayWnd = RealPlayWnd.Handle;
m_ptrRealHandle = RealPlayWnd.Handle;
m_fRealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);
IntPtr pUser = new IntPtr();
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V30(m_lUserID, ref lpClientInfo, m_fRealData, pUser, 1);
if (m_lRealHandle == -1)
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_RealPlay fail %d!");
return;
}
}
public void RemoteDisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReserved)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
if (!m_bJpegCapture)
{
return;
}
else
{
uint nLastErr = 100;
/*capture JPEG image and save into local disk*/
if (!PlayCtrl.PlayM4_ConvertToJpegFile(pBuf, nSize, nWidth, nHeight, nType, "S:/Capture.jpg"))
{
//Debug.WriteLine("PlayM4_ConvertToJpegFile fail");
nLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
this.BeginInvoke(AlarmInfo, "Jpeg Capture failed");
}
else
{
this.BeginInvoke(AlarmInfo, "Jpeg Capture Succeed");
//Debug.WriteLine("PlayM4_ConvertToJpegFile Succ");
}
}
m_bJpegCapture = false;
}
public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, ref byte pBuffer, UInt32 dwBufSize, IntPtr pUser)
{
// MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
// switch (dwDataType)
// {
// case CHCNetSDK.NET_DVR_SYSHEAD: // sys head
// if (!PlayCtrl.PlayM4_GetPort(ref m_lPort))
// {
// MessageBox.Show("Get Port Fail");
// }
// if (dwBufSize > 0)
// {
// //set as stream mode, real-time stream under preview
// if (!PlayCtrl.PlayM4_SetStreamOpenMode(m_lPort, PlayCtrl.STREAME_REALTIME))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_SetStreamOpenMode fail");
// }
// //start player
// if (!PlayCtrl.PlayM4_OpenStream(m_lPort, ref pBuffer, dwBufSize, 1024 * 1024))
// {
// m_lPort = -1;
// this.BeginInvoke(AlarmInfo, "PlayM4_OpenStream fail");
// break;
// }
//set soft decode display callback function to capture
// m_fDisplayFun = new PlayCtrl.DISPLAYCBFUN(RemoteDisplayCBFun);
// if (!PlayCtrl.PlayM4_SetDisplayCallBack(m_lPort, m_fDisplayFun))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_SetDisplayCallBack fail");
// }
//start play, set play window
// this.BeginInvoke(AlarmInfo, "About to call PlayM4_Play");
// if (!PlayCtrl.PlayM4_Play(m_lPort, m_ptrRealHandle))
// {
// m_lPort = -1;
// this.BeginInvoke(AlarmInfo, "PlayM4_Play fail");
// break;
// }
//set frame buffer number
// if (!PlayCtrl.PlayM4_SetDisplayBuf(m_lPort, 15))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_SetDisplayBuf fail");
// }
//set display mode
// if (!PlayCtrl.PlayM4_SetOverlayMode(m_lPort, 0, 0/* COLORREF(0)*/))//play off screen // todo!!!
// {
// this.BeginInvoke(AlarmInfo, " PlayM4_SetOverlayMode fail");
// }
// }
// break;
// case CHCNetSDK.NET_DVR_STREAMDATA: // video stream data
// if (dwBufSize > 0 && m_lPort != -1)
// {
// if (!PlayCtrl.PlayM4_InputData(m_lPort, ref pBuffer, dwBufSize))
// {
// this.BeginInvoke(AlarmInfo, " PlayM4_InputData fail");
// }
// }
// break;
// case CHCNetSDK.NET_DVR_AUDIOSTREAMDATA: // Audio Stream Data
// if (dwBufSize > 0 && m_lPort != -1)
// {
// if (!PlayCtrl.PlayM4_InputVideoData(m_lPort, ref pBuffer, dwBufSize))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_InputVideoData Fail");
// }
// }
// break;
// default:
// break;
// } */
}
public void MsgCallback(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
switch (lCommand)
{
case CHCNetSDK.COMM_ALARM:
ProcessCommAlarm(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
case CHCNetSDK.COMM_ALARM_V30:
ProcessCommAlarm_V30(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
case CHCNetSDK.COMM_UPLOAD_PLATE_RESULT:
ProcessCommAlarm_Plate(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
// case CHCNetSDK.COMM_ALARM_RULE:
// this.BeginInvoke(AlarmInfo, "COMM_ALARM_RULE");
// break;
// case CHCNetSDK.COMM_TRADEINFO:
// this.BeginInvoke(AlarmInfo, "COMM_TRADEINFO");
// break;
// case CHCNetSDK.COMM_IPCCFG:
// this.BeginInvoke(AlarmInfo, "COMM_IPCCFG");
// break;
// case CHCNetSDK.COMM_IPCCFG_V31:
// this.BeginInvoke(AlarmInfo, "COMM_IPCCFG_V31");
// break;
default:
break;
}
}
public void ProcessCommAlarm(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
CHCNetSDK.NET_DVR_ALARMINFO struAlarmInfo = new CHCNetSDK.NET_DVR_ALARMINFO();
struAlarmInfo = (CHCNetSDK.NET_DVR_ALARMINFO)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_ALARMINFO));
string str;
switch (struAlarmInfo.dwAlarmType)
{
case 0:
this.BeginInvoke(AlarmInfo, "sensor alarm");
break;
case 1:
this.BeginInvoke(AlarmInfo, "hard disk full");
break;
case 2:
this.BeginInvoke(AlarmInfo, "video lost");
break;
case 3:
str = "";
str = pAlarmer.sDeviceIP;
str = " motion detection";
this.BeginInvoke(AlarmInfo, str);
m_bJpegCapture = true;
break;
case 4:
this.BeginInvoke(AlarmInfo, "hard disk unformatted");
break;
case 5:
this.BeginInvoke(AlarmInfo, "hard disk error");
break;
case 6:
this.BeginInvoke(AlarmInfo, "tampering detection");
break;
case 7:
this.BeginInvoke(AlarmInfo, "unmatched video output standard");
break;
case 8:
this.BeginInvoke(AlarmInfo, "illegal operation");
break;
default:
this.BeginInvoke(AlarmInfo, "Unknow alarm");
break;
}
}
private void ProcessCommAlarm_V30(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
CHCNetSDK.NET_DVR_ALARMINFO_V30 struAlarmInfoV30 = new CHCNetSDK.NET_DVR_ALARMINFO_V30();
struAlarmInfoV30 = (CHCNetSDK.NET_DVR_ALARMINFO_V30)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_ALARMINFO_V30));
string str;
switch (struAlarmInfoV30.dwAlarmType)
{
case 0:
this.BeginInvoke(AlarmInfo, "sensor alarm");
break;
case 1:
this.BeginInvoke(AlarmInfo, "hard disk full");
break;
case 2:
this.BeginInvoke(AlarmInfo, "video lost");
break;
case 3:
str = "";
str = pAlarmer.sDeviceIP;
str = " motion detection";
this.BeginInvoke(AlarmInfo, str);
break;
case 4:
this.BeginInvoke(AlarmInfo, "hard disk unformatted");
break;
case 5:
this.BeginInvoke(AlarmInfo, "hard disk error");
break;
case 6:
this.BeginInvoke(AlarmInfo, "tampering detection");
break;
case 7:
this.BeginInvoke(AlarmInfo, "unmatched video output standard");
break;
case 8:
this.BeginInvoke(AlarmInfo, "illegal operation");
break;
case 9:
this.BeginInvoke(AlarmInfo, "videl Signal abnormal");
break;
case 10:
this.BeginInvoke(AlarmInfo, "record abnormal");
break;
default:
this.BeginInvoke(AlarmInfo, "Unknow alarm");
break;
}
}
private void ProcessCommAlarm_Plate(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
CHCNetSDK.NET_DVR_PLATE_RESULT struAlarmInfoV30 = new CHCNetSDK.NET_DVR_PLATE_RESULT();
uint dwSize = (uint)Marshal.SizeOf(struAlarmInfoV30);
struAlarmInfoV30 = (CHCNetSDK.NET_DVR_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_PLATE_RESULT));
FileStream fs = new FileStream("C:/DEMOSnapJPEG.jpg", FileMode.Create);
int iLen = (int)struAlarmInfoV30.dwPicLen;
byte[] by = new byte[iLen];
Marshal.Copy(struAlarmInfoV30.pBuffer1, by, 0, iLen);
fs.Write(by, 0, iLen);
fs.Close();
}
private void btnJpegCapture_Click(object sender, EventArgs e)
{
CHCNetSDK.NET_DVR_SNAPCFG struSnapCfg = new CHCNetSDK.NET_DVR_SNAPCFG();
struSnapCfg.wIntervalTime = new ushort[4];
struSnapCfg.dwSize = (uint)Marshal.SizeOf(struSnapCfg);
struSnapCfg.byRelatedDriveWay = 0;
struSnapCfg.bySnapTimes = 1;
struSnapCfg.wSnapWaitTime = 100;
struSnapCfg.wIntervalTime[0] = 100;
bool bManualSnap = CHCNetSDK.NET_DVR_ContinuousShoot(m_lUserID, ref struSnapCfg);
if (!bManualSnap)
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_ContinuousShoot fail!");
return;
}
}
public delegate void MyDebugInfo(string str);
public void DebugInfo(string str)
{
if (str.Length > 0)
{
str = "\n";
TextBoxInfo.AppendText(str);
}
}
private void btnFortify_Click(object sender, EventArgs e)
{
if (m_bGuard)
{
if (m_lFortifyHandle != -1)
{
if (CHCNetSDK.NET_DVR_CloseAlarmChan_V30(m_lFortifyHandle))
{
DebugInfo("NET_DVR_CloseAlarmChan_V30 Succ");
btnFortify.Text = "Alarm Guard";
m_bGuard = !m_bGuard;
}
else
{
DebugInfo("NET_DVR_CloseAlarmChan_V30 Fail");
}
}
}
else
{
m_lFortifyHandle = CHCNetSDK.NET_DVR_SetupAlarmChan_V30(m_lUserID);
if (m_lFortifyHandle != -1)
{
btnFortify.Text = "Unguard";
m_bGuard = !m_bGuard;
DebugInfo("NET_DVR_SetupAlarmChan_V30 Succeed");
}
else
{
DebugInfo("NET_DVR_SetupAlarmChan_V30 Succeed");
}
m_falarmData = new CHCNetSDK.MSGCallBack(MsgCallback);
if (CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(m_falarmData, IntPtr.Zero))
{
DebugInfo("NET_DVR_SetDVRMessageCallBack_V30 Succeed");
}
else
{
uint i = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_SetDVRMessageCallBack_V30 Failed");
}
}
}
private void btnOpenSound_Click(object sender, EventArgs e)
{
// private Int32 m_iPreviewType = 0;
if (m_lRealHandle < 0)
{
MessageBox.Show("Please start preview at first");
}
else
{
if (!m_bOpenSound)
{
if (CHCNetSDK.NET_DVR_OpenSound(m_lRealHandle))
{
m_bOpenSound = true;
btnOpenSound.Text = "Stop sound";
}
else
{
uint i = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_OpenSound Failed");
}
}
else
{
if (CHCNetSDK.NET_DVR_CloseSound())
{
m_bOpenSound = false;
btnOpenSound.Text = "Open sound";
}
else
{
uint i = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_CloseSound Failed");
}
}
}
}
private void Logout_Click(object sender, EventArgs e)
{
logoutRtn = CHCNetSDK.NET_DVR_Logout_V30(m_lUserID);
Console.WriteLine("The error number is:" CHCNetSDK.NET_DVR_GetLastError());
if (logoutRtn)
MessageBox.Show("Successful to logout the current device.");
}
private void textBoxIP_TextChanged(object sender, EventArgs e)
{
}
private void btnStopPre_Click(object sender, EventArgs e)
{
bool bStopReal = CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
RealPlayWnd.Refresh();
if (!bStopReal)
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_StopRealPlay fail!");
return;
}
}
private void Btn_CANCEL_Click(object sender, EventArgs e)
{
if (!logoutRtn)
{
MessageBox.Show("Please logout the current device firstly.");
return;
}
bool cleanupRtn = CHCNetSDK.NET_DVR_Cleanup();
this.Close();
}
}
【实例截图】

【核心代码】ystem;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
namespace PreviewDemo
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainFrame : System.Windows.Forms.Form
{
private CHCNetSDK.REALDATACALLBACK m_fRealData = null;
private CHCNetSDK.MSGCallBack m_falarmData = null;
public CHCNetSDK.NET_DVR_DEVICEINFO_V30 m_struDeviceInfo;
public CHCNetSDK.NET_DVR_SNAPCFG struSnapCfg;
public Int32 m_lUserID = -1;
private bool m_bInitSDK = false;
private Int32 m_lRealHandle = -1;
private Int32 m_lPort = -1;
private bool m_bJpegCapture = false;
private bool m_bGuard = false;
private bool m_bOpenSound = false;
private Int32 m_lFortifyHandle = -1;
private bool logoutRtn = false;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Button btnPreview;
private System.Windows.Forms.TextBox textBoxPort;
private System.Windows.Forms.TextBox textBoxIP;
private System.Windows.Forms.TextBox textBoxPassword;
private System.Windows.Forms.TextBox textBoxUserName;
private System.Windows.Forms.PictureBox RealPlayWnd;
private Button btnJpegCapture;
private IntPtr m_ptrRealHandle;
private Button btnFortify;
private RichTextBox TextBoxInfo;
private Label label5;
private Button btnOpenSound;
private Button Logout;
private Label label6;
private Button btnStopPre;
private Button Btn_CANCEL;
private Label label7;
private Label label8;
private Label label9;
private Label label10;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public MainFrame()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
MessageBox.Show("NET_DVR_Init error!");
return;
}
else
{
}
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (m_lRealHandle >= 0)
{
CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
}
if (m_lUserID >= 0)
{
CHCNetSDK.NET_DVR_Logout_V30(m_lUserID);
}
if (m_bInitSDK == true)
{
CHCNetSDK.NET_DVR_Cleanup();
}
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainFrame));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.btnLogin = new System.Windows.Forms.Button();
this.btnPreview = new System.Windows.Forms.Button();
this.textBoxPort = new System.Windows.Forms.TextBox();
this.textBoxIP = new System.Windows.Forms.TextBox();
this.textBoxPassword = new System.Windows.Forms.TextBox();
this.textBoxUserName = new System.Windows.Forms.TextBox();
this.RealPlayWnd = new System.Windows.Forms.PictureBox();
this.btnJpegCapture = new System.Windows.Forms.Button();
this.btnFortify = new System.Windows.Forms.Button();
this.TextBoxInfo = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.btnOpenSound = new System.Windows.Forms.Button();
this.Logout = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.btnStopPre = new System.Windows.Forms.Button();
this.Btn_CANCEL = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.RealPlayWnd)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(2, 342);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Device IP Address";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.Location = new System.Drawing.Point(1, 411);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 0;
this.label2.Text = "User Name";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.Location = new System.Drawing.Point(2, 440);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(65, 16);
this.label3.TabIndex = 0;
this.label3.Text = "Password";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.Location = new System.Drawing.Point(0, 376);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(113, 16);
this.label4.TabIndex = 0;
this.label4.Text = "Device Port Number";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(286, 338);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(88, 28);
this.btnLogin.TabIndex = 1;
this.btnLogin.Text = " Login";
this.btnLogin.Click = new System.EventHandler(this.btnLogin_Click);
//
// btnPreview
//
this.btnPreview.Location = new System.Drawing.Point(286, 376);
this.btnPreview.Name = "btnPreview";
this.btnPreview.Size = new System.Drawing.Size(88, 30);
this.btnPreview.TabIndex = 2;
this.btnPreview.Text = "Live view";
this.btnPreview.Click = new System.EventHandler(this.btnPreview_Click);
//
// textBoxPort
//
this.textBoxPort.Location = new System.Drawing.Point(133, 374);
this.textBoxPort.Name = "textBoxPort";
this.textBoxPort.Size = new System.Drawing.Size(103, 22);
this.textBoxPort.TabIndex = 3;
this.textBoxPort.Text = "8000";
this.textBoxPort.TextChanged = new System.EventHandler(this.textBox1_TextChanged);
//
// textBoxIP
//
this.textBoxIP.Location = new System.Drawing.Point(133, 341);
this.textBoxIP.Name = "textBoxIP";
this.textBoxIP.Size = new System.Drawing.Size(103, 22);
this.textBoxIP.TabIndex = 3;
this.textBoxIP.Text = "172.8.4.76";
this.textBoxIP.TextChanged = new System.EventHandler(this.textBoxIP_TextChanged);
//
// textBoxPassword
//
this.textBoxPassword.Location = new System.Drawing.Point(133, 438);
this.textBoxPassword.Name = "textBoxPassword";
this.textBoxPassword.Size = new System.Drawing.Size(103, 22);
this.textBoxPassword.TabIndex = 3;
this.textBoxPassword.Text = "12345";
this.textBoxPassword.UseSystemPasswordChar = true;
//
// textBoxUserName
//
this.textBoxUserName.Location = new System.Drawing.Point(133, 409);
this.textBoxUserName.Name = "textBoxUserName";
this.textBoxUserName.Size = new System.Drawing.Size(103, 22);
this.textBoxUserName.TabIndex = 3;
this.textBoxUserName.Text = "admin";
//
// RealPlayWnd
//
this.RealPlayWnd.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("RealPlayWnd.BackgroundImage")));
this.RealPlayWnd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.RealPlayWnd.Location = new System.Drawing.Point(3, 1);
this.RealPlayWnd.Name = "RealPlayWnd";
this.RealPlayWnd.Size = new System.Drawing.Size(423, 322);
this.RealPlayWnd.TabIndex = 4;
this.RealPlayWnd.TabStop = false;
//
// btnJpegCapture
//
this.btnJpegCapture.Location = new System.Drawing.Point(499, 376);
this.btnJpegCapture.Name = "btnJpegCapture";
this.btnJpegCapture.Size = new System.Drawing.Size(92, 30);
this.btnJpegCapture.TabIndex = 5;
this.btnJpegCapture.Text = "Manual Snap";
this.btnJpegCapture.UseVisualStyleBackColor = true;
this.btnJpegCapture.Click = new System.EventHandler(this.btnJpegCapture_Click);
//
// btnFortify
//
this.btnFortify.Location = new System.Drawing.Point(499, 338);
this.btnFortify.Name = "btnFortify";
this.btnFortify.Size = new System.Drawing.Size(90, 29);
this.btnFortify.TabIndex = 7;
this.btnFortify.Text = "Arm or Disarm";
this.btnFortify.UseVisualStyleBackColor = true;
this.btnFortify.Click = new System.EventHandler(this.btnFortify_Click);
//
// TextBoxInfo
//
this.TextBoxInfo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
this.TextBoxInfo.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TextBoxInfo.Location = new System.Drawing.Point(432, 1);
this.TextBoxInfo.Name = "TextBoxInfo";
this.TextBoxInfo.Size = new System.Drawing.Size(270, 322);
this.TextBoxInfo.TabIndex = 8;
this.TextBoxInfo.Text = "";
//
// label5
//
this.label5.AutoSize = true;
this.label5.BackColor = System.Drawing.SystemColors.Control;
this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label5.Location = new System.Drawing.Point(588, 308);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(127, 17);
this.label5.TabIndex = 9;
this.label5.Text = "Debug Info Display";
//
// btnOpenSound
//
this.btnOpenSound.Location = new System.Drawing.Point(286, 418);
this.btnOpenSound.Name = "btnOpenSound";
this.btnOpenSound.Size = new System.Drawing.Size(90, 25);
this.btnOpenSound.TabIndex = 13;
this.btnOpenSound.Text = "Open Sound";
this.btnOpenSound.UseVisualStyleBackColor = true;
this.btnOpenSound.Click = new System.EventHandler(this.btnOpenSound_Click);
//
// Logout
//
this.Logout.Location = new System.Drawing.Point(379, 338);
this.Logout.Name = "Logout";
this.Logout.Size = new System.Drawing.Size(98, 29);
this.Logout.TabIndex = 16;
this.Logout.Text = "Logout";
this.Logout.UseVisualStyleBackColor = true;
this.Logout.Click = new System.EventHandler(this.Logout_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label6.Location = new System.Drawing.Point(2, 308);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(88, 17);
this.label6.TabIndex = 17;
this.label6.Text = "Play Window";
//
// btnStopPre
//
this.btnStopPre.Location = new System.Drawing.Point(377, 376);
this.btnStopPre.Name = "btnStopPre";
this.btnStopPre.Size = new System.Drawing.Size(98, 30);
this.btnStopPre.TabIndex = 18;
this.btnStopPre.Text = "Stop Live View";
this.btnStopPre.UseVisualStyleBackColor = true;
this.btnStopPre.Click = new System.EventHandler(this.btnStopPre_Click);
//
// Btn_CANCEL
//
this.Btn_CANCEL.Location = new System.Drawing.Point(382, 419);
this.Btn_CANCEL.Name = "Btn_CANCEL";
this.Btn_CANCEL.Size = new System.Drawing.Size(209, 24);
this.Btn_CANCEL.TabIndex = 20;
this.Btn_CANCEL.Text = "Exit";
this.Btn_CANCEL.UseVisualStyleBackColor = true;
this.Btn_CANCEL.Click = new System.EventHandler(this.Btn_CANCEL_Click);
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(593, 343);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(103, 14);
this.label7.TabIndex = 21;
this.label7.Text = "布防获取抓拍图片";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(595, 385);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(79, 14);
this.label8.TabIndex = 22;
this.label8.Text = "手动触发连拍";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(248, 346);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(31, 14);
this.label9.TabIndex = 23;
this.label9.Text = "登录";
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(248, 384);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(31, 14);
this.label10.TabIndex = 24;
this.label10.Text = "预览";
//
// MainFrame
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(705, 466);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.Btn_CANCEL);
this.Controls.Add(this.btnStopPre);
this.Controls.Add(this.label6);
this.Controls.Add(this.Logout);
this.Controls.Add(this.btnOpenSound);
this.Controls.Add(this.label5);
this.Controls.Add(this.TextBoxInfo);
this.Controls.Add(this.btnFortify);
this.Controls.Add(this.btnJpegCapture);
this.Controls.Add(this.RealPlayWnd);
this.Controls.Add(this.textBoxPort);
this.Controls.Add(this.btnPreview);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBoxIP);
this.Controls.Add(this.textBoxPassword);
this.Controls.Add(this.textBoxUserName);
this.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Location = new System.Drawing.Point(50, 0);
this.Name = "MainFrame";
this.Text = "Hikvison Client SDK Demo";
((System.ComponentModel.ISupportInitialize)(this.RealPlayWnd)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainFrame());
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void btnLogin_Click(object sender, System.EventArgs e)
{
if (textBoxIP.Text == "" || textBoxPort.Text == "" ||
textBoxUserName.Text == "" || textBoxPassword.Text == "")
{
MessageBox.Show("Please input prarameters: ");
return;
}
string DVRIPAddress = textBoxIP.Text;
Int16 DVRPortNumber = Int16.Parse(textBoxPort.Text);
string DVRUserName = textBoxUserName.Text;
string DVRPassword = textBoxPassword.Text;
m_lUserID = CHCNetSDK.NET_DVR_Login_V30(DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref m_struDeviceInfo);
if (m_lUserID == -1)
{
MessageBox.Show("login error!");
return;
}
else
{
logoutRtn = false;
MessageBox.Show("Login Success!");
}
}
private void btnPreview_Click(object sender, System.EventArgs e)
{
CHCNetSDK.NET_DVR_CLIENTINFO lpClientInfo = new CHCNetSDK.NET_DVR_CLIENTINFO();
lpClientInfo.lChannel = 1;
lpClientInfo.lLinkMode = 0x0000;
lpClientInfo.sMultiCastIP = "";
lpClientInfo.hPlayWnd = RealPlayWnd.Handle;
m_ptrRealHandle = RealPlayWnd.Handle;
m_fRealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);
IntPtr pUser = new IntPtr();
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V30(m_lUserID, ref lpClientInfo, m_fRealData, pUser, 1);
if (m_lRealHandle == -1)
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_RealPlay fail %d!");
return;
}
}
public void RemoteDisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReserved)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
if (!m_bJpegCapture)
{
return;
}
else
{
uint nLastErr = 100;
/*capture JPEG image and save into local disk*/
if (!PlayCtrl.PlayM4_ConvertToJpegFile(pBuf, nSize, nWidth, nHeight, nType, "S:/Capture.jpg"))
{
//Debug.WriteLine("PlayM4_ConvertToJpegFile fail");
nLastErr = PlayCtrl.PlayM4_GetLastError(m_lPort);
this.BeginInvoke(AlarmInfo, "Jpeg Capture failed");
}
else
{
this.BeginInvoke(AlarmInfo, "Jpeg Capture Succeed");
//Debug.WriteLine("PlayM4_ConvertToJpegFile Succ");
}
}
m_bJpegCapture = false;
}
public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, ref byte pBuffer, UInt32 dwBufSize, IntPtr pUser)
{
// MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
// switch (dwDataType)
// {
// case CHCNetSDK.NET_DVR_SYSHEAD: // sys head
// if (!PlayCtrl.PlayM4_GetPort(ref m_lPort))
// {
// MessageBox.Show("Get Port Fail");
// }
// if (dwBufSize > 0)
// {
// //set as stream mode, real-time stream under preview
// if (!PlayCtrl.PlayM4_SetStreamOpenMode(m_lPort, PlayCtrl.STREAME_REALTIME))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_SetStreamOpenMode fail");
// }
// //start player
// if (!PlayCtrl.PlayM4_OpenStream(m_lPort, ref pBuffer, dwBufSize, 1024 * 1024))
// {
// m_lPort = -1;
// this.BeginInvoke(AlarmInfo, "PlayM4_OpenStream fail");
// break;
// }
//set soft decode display callback function to capture
// m_fDisplayFun = new PlayCtrl.DISPLAYCBFUN(RemoteDisplayCBFun);
// if (!PlayCtrl.PlayM4_SetDisplayCallBack(m_lPort, m_fDisplayFun))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_SetDisplayCallBack fail");
// }
//start play, set play window
// this.BeginInvoke(AlarmInfo, "About to call PlayM4_Play");
// if (!PlayCtrl.PlayM4_Play(m_lPort, m_ptrRealHandle))
// {
// m_lPort = -1;
// this.BeginInvoke(AlarmInfo, "PlayM4_Play fail");
// break;
// }
//set frame buffer number
// if (!PlayCtrl.PlayM4_SetDisplayBuf(m_lPort, 15))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_SetDisplayBuf fail");
// }
//set display mode
// if (!PlayCtrl.PlayM4_SetOverlayMode(m_lPort, 0, 0/* COLORREF(0)*/))//play off screen // todo!!!
// {
// this.BeginInvoke(AlarmInfo, " PlayM4_SetOverlayMode fail");
// }
// }
// break;
// case CHCNetSDK.NET_DVR_STREAMDATA: // video stream data
// if (dwBufSize > 0 && m_lPort != -1)
// {
// if (!PlayCtrl.PlayM4_InputData(m_lPort, ref pBuffer, dwBufSize))
// {
// this.BeginInvoke(AlarmInfo, " PlayM4_InputData fail");
// }
// }
// break;
// case CHCNetSDK.NET_DVR_AUDIOSTREAMDATA: // Audio Stream Data
// if (dwBufSize > 0 && m_lPort != -1)
// {
// if (!PlayCtrl.PlayM4_InputVideoData(m_lPort, ref pBuffer, dwBufSize))
// {
// this.BeginInvoke(AlarmInfo, "PlayM4_InputVideoData Fail");
// }
// }
// break;
// default:
// break;
// } */
}
public void MsgCallback(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
switch (lCommand)
{
case CHCNetSDK.COMM_ALARM:
ProcessCommAlarm(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
case CHCNetSDK.COMM_ALARM_V30:
ProcessCommAlarm_V30(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
case CHCNetSDK.COMM_UPLOAD_PLATE_RESULT:
ProcessCommAlarm_Plate(ref pAlarmer, pAlarmInfo, dwBufLen, pUser);
break;
// case CHCNetSDK.COMM_ALARM_RULE:
// this.BeginInvoke(AlarmInfo, "COMM_ALARM_RULE");
// break;
// case CHCNetSDK.COMM_TRADEINFO:
// this.BeginInvoke(AlarmInfo, "COMM_TRADEINFO");
// break;
// case CHCNetSDK.COMM_IPCCFG:
// this.BeginInvoke(AlarmInfo, "COMM_IPCCFG");
// break;
// case CHCNetSDK.COMM_IPCCFG_V31:
// this.BeginInvoke(AlarmInfo, "COMM_IPCCFG_V31");
// break;
default:
break;
}
}
public void ProcessCommAlarm(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
CHCNetSDK.NET_DVR_ALARMINFO struAlarmInfo = new CHCNetSDK.NET_DVR_ALARMINFO();
struAlarmInfo = (CHCNetSDK.NET_DVR_ALARMINFO)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_ALARMINFO));
string str;
switch (struAlarmInfo.dwAlarmType)
{
case 0:
this.BeginInvoke(AlarmInfo, "sensor alarm");
break;
case 1:
this.BeginInvoke(AlarmInfo, "hard disk full");
break;
case 2:
this.BeginInvoke(AlarmInfo, "video lost");
break;
case 3:
str = "";
str = pAlarmer.sDeviceIP;
str = " motion detection";
this.BeginInvoke(AlarmInfo, str);
m_bJpegCapture = true;
break;
case 4:
this.BeginInvoke(AlarmInfo, "hard disk unformatted");
break;
case 5:
this.BeginInvoke(AlarmInfo, "hard disk error");
break;
case 6:
this.BeginInvoke(AlarmInfo, "tampering detection");
break;
case 7:
this.BeginInvoke(AlarmInfo, "unmatched video output standard");
break;
case 8:
this.BeginInvoke(AlarmInfo, "illegal operation");
break;
default:
this.BeginInvoke(AlarmInfo, "Unknow alarm");
break;
}
}
private void ProcessCommAlarm_V30(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
CHCNetSDK.NET_DVR_ALARMINFO_V30 struAlarmInfoV30 = new CHCNetSDK.NET_DVR_ALARMINFO_V30();
struAlarmInfoV30 = (CHCNetSDK.NET_DVR_ALARMINFO_V30)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_ALARMINFO_V30));
string str;
switch (struAlarmInfoV30.dwAlarmType)
{
case 0:
this.BeginInvoke(AlarmInfo, "sensor alarm");
break;
case 1:
this.BeginInvoke(AlarmInfo, "hard disk full");
break;
case 2:
this.BeginInvoke(AlarmInfo, "video lost");
break;
case 3:
str = "";
str = pAlarmer.sDeviceIP;
str = " motion detection";
this.BeginInvoke(AlarmInfo, str);
break;
case 4:
this.BeginInvoke(AlarmInfo, "hard disk unformatted");
break;
case 5:
this.BeginInvoke(AlarmInfo, "hard disk error");
break;
case 6:
this.BeginInvoke(AlarmInfo, "tampering detection");
break;
case 7:
this.BeginInvoke(AlarmInfo, "unmatched video output standard");
break;
case 8:
this.BeginInvoke(AlarmInfo, "illegal operation");
break;
case 9:
this.BeginInvoke(AlarmInfo, "videl Signal abnormal");
break;
case 10:
this.BeginInvoke(AlarmInfo, "record abnormal");
break;
default:
this.BeginInvoke(AlarmInfo, "Unknow alarm");
break;
}
}
private void ProcessCommAlarm_Plate(ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
MyDebugInfo AlarmInfo = new MyDebugInfo(DebugInfo);
CHCNetSDK.NET_DVR_PLATE_RESULT struAlarmInfoV30 = new CHCNetSDK.NET_DVR_PLATE_RESULT();
uint dwSize = (uint)Marshal.SizeOf(struAlarmInfoV30);
struAlarmInfoV30 = (CHCNetSDK.NET_DVR_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_PLATE_RESULT));
FileStream fs = new FileStream("C:/DEMOSnapJPEG.jpg", FileMode.Create);
int iLen = (int)struAlarmInfoV30.dwPicLen;
byte[] by = new byte[iLen];
Marshal.Copy(struAlarmInfoV30.pBuffer1, by, 0, iLen);
fs.Write(by, 0, iLen);
fs.Close();
}
private void btnJpegCapture_Click(object sender, EventArgs e)
{
CHCNetSDK.NET_DVR_SNAPCFG struSnapCfg = new CHCNetSDK.NET_DVR_SNAPCFG();
struSnapCfg.wIntervalTime = new ushort[4];
struSnapCfg.dwSize = (uint)Marshal.SizeOf(struSnapCfg);
struSnapCfg.byRelatedDriveWay = 0;
struSnapCfg.bySnapTimes = 1;
struSnapCfg.wSnapWaitTime = 100;
struSnapCfg.wIntervalTime[0] = 100;
bool bManualSnap = CHCNetSDK.NET_DVR_ContinuousShoot(m_lUserID, ref struSnapCfg);
if (!bManualSnap)
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_ContinuousShoot fail!");
return;
}
}
public delegate void MyDebugInfo(string str);
public void DebugInfo(string str)
{
if (str.Length > 0)
{
str = "\n";
TextBoxInfo.AppendText(str);
}
}
private void btnFortify_Click(object sender, EventArgs e)
{
if (m_bGuard)
{
if (m_lFortifyHandle != -1)
{
if (CHCNetSDK.NET_DVR_CloseAlarmChan_V30(m_lFortifyHandle))
{
DebugInfo("NET_DVR_CloseAlarmChan_V30 Succ");
btnFortify.Text = "Alarm Guard";
m_bGuard = !m_bGuard;
}
else
{
DebugInfo("NET_DVR_CloseAlarmChan_V30 Fail");
}
}
}
else
{
m_lFortifyHandle = CHCNetSDK.NET_DVR_SetupAlarmChan_V30(m_lUserID);
if (m_lFortifyHandle != -1)
{
btnFortify.Text = "Unguard";
m_bGuard = !m_bGuard;
DebugInfo("NET_DVR_SetupAlarmChan_V30 Succeed");
}
else
{
DebugInfo("NET_DVR_SetupAlarmChan_V30 Succeed");
}
m_falarmData = new CHCNetSDK.MSGCallBack(MsgCallback);
if (CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(m_falarmData, IntPtr.Zero))
{
DebugInfo("NET_DVR_SetDVRMessageCallBack_V30 Succeed");
}
else
{
uint i = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_SetDVRMessageCallBack_V30 Failed");
}
}
}
private void btnOpenSound_Click(object sender, EventArgs e)
{
// private Int32 m_iPreviewType = 0;
if (m_lRealHandle < 0)
{
MessageBox.Show("Please start preview at first");
}
else
{
if (!m_bOpenSound)
{
if (CHCNetSDK.NET_DVR_OpenSound(m_lRealHandle))
{
m_bOpenSound = true;
btnOpenSound.Text = "Stop sound";
}
else
{
uint i = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_OpenSound Failed");
}
}
else
{
if (CHCNetSDK.NET_DVR_CloseSound())
{
m_bOpenSound = false;
btnOpenSound.Text = "Open sound";
}
else
{
uint i = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_CloseSound Failed");
}
}
}
}
private void Logout_Click(object sender, EventArgs e)
{
logoutRtn = CHCNetSDK.NET_DVR_Logout_V30(m_lUserID);
Console.WriteLine("The error number is:" CHCNetSDK.NET_DVR_GetLastError());
if (logoutRtn)
MessageBox.Show("Successful to logout the current device.");
}
private void textBoxIP_TextChanged(object sender, EventArgs e)
{
}
private void btnStopPre_Click(object sender, EventArgs e)
{
bool bStopReal = CHCNetSDK.NET_DVR_StopRealPlay(m_lRealHandle);
RealPlayWnd.Refresh();
if (!bStopReal)
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
DebugInfo("NET_DVR_StopRealPlay fail!");
return;
}
}
private void Btn_CANCEL_Click(object sender, EventArgs e)
{
if (!logoutRtn)
{
MessageBox.Show("Please logout the current device firstly.");
return;
}
bool cleanupRtn = CHCNetSDK.NET_DVR_Cleanup();
this.Close();
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论