Net Core教程

C#调用大华摄像头

本文主要是介绍C#调用大华摄像头,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

大华SDK包地址:https://support.dahuatech.com/tools/sdkExploit

效果图:

 

 

        //32位SDK 大华摄像机打开方法
        IntPtr _LoginID = IntPtr.Zero;
        NET_DEVICEINFO_Ex _DeviceInfo = new NET_DEVICEINFO_Ex();
        IntPtr _PlayID = IntPtr.Zero;
        IntPtr _PlayID2 = IntPtr.Zero;
        fAnalyzerDataCallBack _AnalyzerDataCallBack;
        TextInfo _TextInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
        fDisConnectCallBack _DisConnectCallBack; //重连函数
        fHaveReConnectCallBack _ReConnectCallBack;

1、登录方法

private void button_login_Click(object sender, EventArgs e)
        {
            //登录设备
            try
            {
                //自动重连
                NETClient.Init(_DisConnectCallBack, IntPtr.Zero, null);
                NETClient.SetAutoReconnect(_ReConnectCallBack, IntPtr.Zero);

                ushort port = Convert.ToUInt16(this.textBox_port.Text.Trim()); //端口
                string username = this.textBox_name.Text.Trim();//账号
                string password = this.textBox_password.Text.Trim(); //密码

                _LoginID = NETClient.Login(this.textBox_ip.Text.Trim(), port, username, password, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref _DeviceInfo);

                if (IntPtr.Zero == _LoginID)
                {
                    MessageBox.Show(NETClient.GetLastError());
                    return;
                }
                //MessageBox.Show("登陆成功!");
                //打开摄像头
                button_play_Click(null, null);
            }
            catch (Exception ex)
            {
                //异常提示
                MessageBox.Show(ex.Message);
                Process.GetCurrentProcess().Kill();
            }
        }
View Code

2.打开监控

 private void button1_Click(object sender, EventArgs e)
        {
            //打开监控
            if (button1.Text=="打开监控")
            {
                button_login_Click(null, null);
                button1.Text = "关闭监控";
            }
            else
            {
                //关闭窗体关闭摄像头
                _PlayID = IntPtr.Zero;
                _PlayID2 = IntPtr.Zero;
                this.pictureBox_play.Refresh();
                button1.Text = "打开监控";
            } 
        }
View Code

3.调用监控方法

 private void button_play_Click(object sender, EventArgs e)
        {
            //打开监视
            try
            {
                _PlayID = NETClient.RealPlay(_LoginID, _DeviceInfo.nChanNum - 1, this.pictureBox_play.Handle);
                _PlayID2 = NETClient.RealPlay(_LoginID, _DeviceInfo.nChanNum - 1, this.pictureBox_play2.Handle);
                if (IntPtr.Zero == _PlayID || IntPtr.Zero == _PlayID2)
                {
                    MessageBox.Show(NETClient.GetLastError());
                    return;
                }
                bool ret = NETClient.RenderPrivateData(_PlayID, true);
                if (!ret)
                {
                    MessageBox.Show(NETClient.GetLastError());
                    return;
                }
            }
            catch (Exception)
            {
                //异常提示
                throw;
            }
        }
View Code

4.关闭窗体时自动关闭监控

        private void DaHua_32_FormClosing(object sender, FormClosingEventArgs e)
        {
            //关闭窗体关闭摄像头
            _PlayID = IntPtr.Zero;
            _PlayID2 = IntPtr.Zero;
            this.pictureBox_play.Refresh();
        }
View Code

其他:

1.三个实体类

2.dll

这两个东西从https://support.dahuatech.com/tools/sdkExploit里面下载

 

 

这篇关于C#调用大华摄像头的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!