基于海康的SDK摄像头开发(C#源码)

💞哈喽!大家好,我是HappyGirl快乐女孩💞
😜是一位爱好技术的【技术Fans】!😜


💞💞💞 如果有对技术感兴趣的宅友,欢迎关注💞💞💞
❤️❤️❤️感谢各位❤️❤️❤️

目录

目录

1️⃣ 主界面源码

2️⃣ 摄像头部分代码

3️⃣ NetSDK库太大(六百多兆)

4️⃣注意事项


1️⃣ 主界面源码


using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            init();
        }
        public IntPtr PictureDev1Cam { get { return pictureBox1.Handle; } }
        public IntPtr PictureDev2Cam { get { return pictureBox2.Handle; } }
        IntPtr Cam1Handle = IntPtr.Zero;
        IntPtr Cam2Handle = IntPtr.Zero;
        Cam Camera1 = new Cam();
        Cam Camera2 = new Cam();
        static private string Cam1IP = "192.168.1.64";
        static private string Cam2IP = "192.168.1.61";
        void init()
        {
            Cam1Handle = PictureDev1Cam;
            Cam2Handle = PictureDev2Cam;
        }
        private void ConnectCamera(int whitch)
        {
            string userName = "admin";
            string password = "a123456.";
            int PortCamera = 8000;
            if (whitch == 1)
            {
                labelCamera1Status.Text = "摄像头连接中...";
                Task.Run(() =>
                {
                if (!Camera1.ConnectCamera(Cam1IP, PortCamera, userName, password))
                {
                        this.Invoke((Action)delegate { labelCamera1Status.Text = "摄像头连接失败"; });
                 }
                else
                {
                        this.Invoke((Action)delegate { labelCamera1Status.Text = ""; });
                        Camera1.Preview(Cam1Handle);
                        Camera1.AdjustMirrorPara(1);
                        Cam1ReconnectTimer.Stop();
                    }
                });
            }
            else
            {
                labelCamera2Status.Text = "摄像头连接中...";
                Task.Run(() =>
                {
                    if (!Camera2.ConnectCamera(Cam2IP, PortCamera, userName, password))
                    {
                        this.Invoke((Action)delegate { labelCamera2Status.Text = "摄像头连接失败"; });
                    }
                    else
                    {
                        this.Invoke((Action)delegate { labelCamera2Status.Text = ""; });
                        Camera2.Preview(Cam2Handle);
                        Camera2.AdjustMirrorPara(1);
                        Cam2ReconnectTimer.Stop();
                    }
                });
            }
        }
        private void Cam1ReconnectTimer_Tick(object sender, EventArgs e)
        {
            ConnectCamera(1);

        }
        private void Cam2ReconnectTimer_Tick(object sender, EventArgs e)
        {
            ConnectCamera(2);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
    }
}

2️⃣ 摄像头部分代码


using System;
using System.IO;
using System.Runtime.InteropServices;


namespace WindowsFormsApp1
{
    class Cam
    {
        private bool initSDK = false;
        private uint lastErr = 0;
        private string debugMessage = "";
        private bool err = false;
        private Int32 userID = -1;
        public bool Isconnected { get; private set; }
        UInt32 dwReturn = 0;
        Int32 nSize = 0;
        IntPtr ptrPicCfg;
        private CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX camPara = new CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX();
        private CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
        private Int32 realPlayHandle = -1;
        public bool ConnectCamera(string deviceIP, int port, string userName, string password)
        {

            initSDK = CHCNetSDK.NET_DVR_Init();
            if (initSDK == false)
            {
                lastErr = CHCNetSDK.NET_DVR_GetLastError();
                debugMessage = "初始化过程失败, 错误码:" + lastErr;

                err = true;
                return false;
            }
            else
            {

                CHCNetSDK.NET_DVR_SetLogToFile(3, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SdkLog\\"), true);
            }

            CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();


            userID = CHCNetSDK.NET_DVR_Login_V30(deviceIP, port, userName, password, ref DeviceInfo);
            if (userID < 0)
            {
                lastErr = CHCNetSDK.NET_DVR_GetLastError();
                debugMessage = "注册失败, 错误码:" + lastErr;
                err = true;

                return false;
            }


            Isconnected = true;
            return true;
        }
        public void AdjustMirrorPara(byte type)
        {
            dwReturn = 0;
            nSize = Marshal.SizeOf(camPara);
            ptrPicCfg = Marshal.AllocHGlobal(nSize);
            Marshal.StructureToPtr(camPara, ptrPicCfg, false);
            if (CHCNetSDK.NET_DVR_GetDVRConfig(userID, 3368, DeviceInfo.byStartChan, ptrPicCfg, (uint)nSize, ref dwReturn))
            {
                camPara = (CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX)Marshal.PtrToStructure(ptrPicCfg, typeof(CHCNetSDK.NET_DVR_CAMERAPARAMCFG_EX));
                Marshal.FreeHGlobal(ptrPicCfg);
                camPara.byMirror = type;

                ptrPicCfg = Marshal.AllocHGlobal(nSize);
                Marshal.StructureToPtr(camPara, ptrPicCfg, false);


                if (!CHCNetSDK.NET_DVR_SetDVRConfig(userID, 3369, DeviceInfo.byStartChan, ptrPicCfg, (uint)nSize))
                {
                    lastErr = CHCNetSDK.NET_DVR_GetLastError();
                    debugMessage = "设置镜像关闭失败, 错误码:" + lastErr;
                    err = true;

                }
            }
            else
            {
                lastErr = CHCNetSDK.NET_DVR_GetLastError();
                debugMessage = "获取参数失败, 错误码:" + lastErr;
                err = true;

            }
            Marshal.FreeHGlobal(ptrPicCfg);
        }
        public void Preview(IntPtr handle)
        {
            CHCNetSDK.NET_DVR_PREVIEWINFO previewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
            previewInfo.hPlayWnd = handle;//预览窗口
            previewInfo.lChannel = 1;//(int)DeviceInfo.byStartChan;//预览的设备通道
            previewInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推
            previewInfo.dwLinkMode = 0;//连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP 
            previewInfo.bBlocked = true; //0- 非阻塞取流,1- 阻塞取流
            previewInfo.dwDisplayBufNum = 0; //播放库播放缓冲区最大缓冲帧数

            //CHCNetSDK.REALDATACALLBACK RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);//预览实时流回调函数
            IntPtr pUser = new IntPtr();//用户数据

            //打开预览 Start live view 
            realPlayHandle = CHCNetSDK.NET_DVR_RealPlay_V40(userID, ref previewInfo, null/*RealData*/, pUser);
            if (realPlayHandle < 0)
            {
                lastErr = CHCNetSDK.NET_DVR_GetLastError();
                debugMessage = "预览失败, 错误码:" + lastErr; //预览失败,输出错误号
                err = true;
                //MessageBox.Show(str);
                return;
            }
        }
    }
}

3️⃣ NetSDK库太大(六百多兆)

见大佬连接:

海康球机技术+DLL库文件+播放器+各种开发文件+C#、C++、Java源码demo、球机云台控制、球机网络配置软件-互联网文档类资源-CSDN文库

4️⃣注意事项

1. 更新设备网络SDK时,SDK开发包【库文件】里的HCNetSDK.dll、HCCore.dll、PlayCtrl.dll、SuperRender.dll、AudioRender.dll、HCNetSDKCom文件夹、ssleay32.dll、libeay32.dll、hlog.dll、hpr.dll、zlib1.dll、log4cxx.properties等文件均要加载到程序里面,【HCNetSDKCom文件夹】(包含里面的功能组件dll库文件)需要和HCNetSDK.dll、HCCore.dll一起加载,放在同一个目录下,且HCNetSDKCom文件夹名不能修改。

2. 如果自行开发软件不能正常实现相应功能,而且程序没有指定加载的dll库路径,请在程序运行的情况下尝试删除HCNetSDK.dll。如果可以删除,说明程序可能调用到系统盘Windows->System32目录下的dll文件,建议删除或者更新该目录下的相关dll文件;如果不能删除,dll文件右键选择属性确认SDK库版本。

3. 如按上述步骤操作后还是不能实现相应功能,请根据NET_DVR_GetLastError返回的错误号判断原因。


 

走过路过,就点个赞吧呗!送你小心心哦💞💞💞

  • 2
    点赞
  • 2
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:像素格子 设计师:CSDN官方博客 返回首页
评论

打赏作者

HappyGirl快乐女孩

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值