文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

C#中怎么连接海康威视

2023-07-05 06:09

关注

这篇文章主要讲解了“C#中怎么连接海康威视”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中怎么连接海康威视”吧!

C# 连接海康威视

C#中怎么连接海康威视

 /// <summary>    /// 海康威视SDK    /// </summary>    public class SDK_Hikvision : SDK_Camera    {        /// <summary>        /// 相机名称        /// </summary>        private string Cam_name;        /// <summary>        /// 相机实例        /// </summary>        private MyCamera m_MyCamera;        /// <summary>        ///  图像回调 相机名称图源        /// </summary>        public override event ImageDelegate Image_Delegate;               IntPtr pBufForSaveImage = IntPtr.Zero;        //注册图像回调函数        MyCamera.cbOutputExdelegate ImageCallback;        /// <summary>        /// 是否已连接相机        /// </summary>        public override bool IsOpen {            get {                if (m_MyCamera!=null)                {                    return m_MyCamera.MV_CC_IsDeviceConnected_NET();                }                return false;            }        }        /// <summary>        ///         /// </summary>        ~SDK_Hikvision()        {        }        /// <summary>        /// 实时采集        /// </summary>        public override void Acquisition()        {            if (m_MyCamera == null) return;            if (GetTriggerMode()==TriggerMode.On)            {                SetTriggerMode(TriggerMode.Off);            }            m_MyCamera.MV_CC_StartGrabbing_NET();        }        /// <summary>        /// 关闭采集        /// </summary>        public override void CloseConsecutive()        {            if (m_MyCamera == null) return;            m_MyCamera.MV_CC_StopGrabbing_NET();        }        /// <summary>        /// 关闭相机        /// </summary>        public override void Close_Camera()        {            if (m_MyCamera == null) return;            m_MyCamera.MV_CC_StopGrabbing_NET();            if (pBufForSaveImage != IntPtr.Zero)            {                Marshal.Release(pBufForSaveImage);            }                         m_MyCamera.MV_CC_CloseDevice_NET();            m_MyCamera.MV_CC_DestroyDevice_NET();        }        /// <summary>        /// 获取曝光        /// </summary>        /// <returns></returns>        public override double GetExposureTime()        {            if (m_MyCamera!=null)            {                MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();                m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);                return stParam.fCurValue;            }          return 0;        }        /// <summary>        /// 获取增益        /// </summary>        /// <returns></returns>        public override double GetGainRaw()        {            if (m_MyCamera != null)            {                MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();                m_MyCamera.MV_CC_GetFloatValue_NET("Gain", ref stParam);                return stParam.fCurValue;            }          return 0;        }        /// <summary>        /// 获取触发模式        /// </summary>        /// <returns></returns>        public override TriggerMode GetTriggerMode()        {            if (m_MyCamera != null)            {                MyCamera.MVCC_ENUMVALUE MVCC_SOURCE = new MyCamera.MVCC_ENUMVALUE();                m_MyCamera.MV_CC_GetEnumValue_NET("TriggerMode", ref MVCC_SOURCE);                MyCamera.MV_CAM_TRIGGER_MODE Value = (MyCamera.MV_CAM_TRIGGER_MODE)MVCC_SOURCE.nCurValue;                return (TriggerMode)MVCC_SOURCE.nCurValue;            }            return TriggerMode.On;        }        /// <summary>        /// 获取触发源        /// </summary>        /// <returns></returns>        public override TriggerSource GetTriggerSource()        {            if (m_MyCamera != null)            {                MyCamera.MVCC_ENUMVALUE MVCC_SOURCE = new MyCamera.MVCC_ENUMVALUE();                m_MyCamera.MV_CC_GetEnumValue_NET("TriggerSource", ref MVCC_SOURCE);                MyCamera.MV_CAM_TRIGGER_SOURCE Value = (MyCamera.MV_CAM_TRIGGER_SOURCE)MVCC_SOURCE.nCurValue;                return (TriggerSource)MVCC_SOURCE.nCurValue;            }            return TriggerSource.Software;        }              /// <summary>        /// 打开相机        /// </summary>        /// <param name="Cam_Name">相机名称</param>        /// <returns></returns>        public override bool Open_Camera(string Cam_Name)        {            System.GC.Collect();            MyCamera.MV_CC_DEVICE_INFO_LIST m_stDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();            int nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_stDeviceList);            if (0 != nRet)            {                return false;            }            for (int i = 0; i < m_stDeviceList.nDeviceNum; i++)            {                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_stDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)                {                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);                    //MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));                    this.Cam_name = gigeInfo.chUserDefinedName;                }                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)                {                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));                    //MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(device.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));                    this.Cam_name = usbInfo.chUserDefinedName;                }                if (Cam_Name == this.Cam_name)                {                    //打开设备                    if (null == m_MyCamera)                    {                        m_MyCamera = new MyCamera();                    }                                       //创建设备                     nRet = m_MyCamera.MV_CC_CreateDevice_NET(ref device);                    if (MyCamera.MV_OK != nRet)                    {                        return false;                    }                    //打开设备                     nRet = m_MyCamera.MV_CC_OpenDevice_NET();                    if (MyCamera.MV_OK != nRet)                    {                        //打开设备失败 销毁设备句柄                         m_MyCamera.MV_CC_DestroyDevice_NET();                        return false;                    }                    // 探测网络最佳包大小(只对GigE相机有效)                    if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)                    {                        int nPacketSize = m_MyCamera.MV_CC_GetOptimalPacketSize_NET();                        if (nPacketSize > 0)                        {                            m_MyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);                                                    }                    }                    //设置相机模式                    //连续采集                    m_MyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", (uint)MyCamera.MV_CAM_ACQUISITION_MODE.MV_ACQ_MODE_CONTINUOUS);                    //触发模式                    m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON);                    //设置图像缓存节点个数                    m_MyCamera.MV_CC_SetImageNodeNum_NET(8);                    //注册图像回调函数                    ImageCallback = new MyCamera.cbOutputExdelegate(GrabImage);                    //来设置数据回调函数。                    nRet =m_MyCamera.MV_CC_RegisterImageCallBackEx_NET(ImageCallback, IntPtr.Zero);                    if (MyCamera.MV_OK != nRet)                    {                        return false;                    }                    //开启采集                    m_MyCamera.MV_CC_StartGrabbing_NET();                    return true;                }            }            return false;        }             /// <summary>        /// 图像注册事件        /// </summary>        /// <param name="pData"></param>        /// <param name="pFrameInfo"></param>        /// <param name="pUser"></param>        private void GrabImage(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)        {            byte[] SrcData = new byte[pFrameInfo.nHeight * pFrameInfo.nWidth];//图像数据          //  Marshal.Copy(pData, SrcData, 0, (int)pFrameInfo.nFrameLen);            HObject Hobj;            HOperatorSet.GenEmptyObj(out Hobj);            byte[] imageData = IntPtr2Bytes(pData, (int)(pFrameInfo.nFrameLen));            uint imageLen = pFrameInfo.nFrameLen;            uint imageWidth = pFrameInfo.nWidth;            uint imageHight = pFrameInfo.nHeight;            if (IsMonoPixelFormat(pFrameInfo.enPixelType))//黑白            {                Hobj.Dispose();                HOperatorSet.GenImage1(out Hobj, "byte", pFrameInfo.nWidth, pFrameInfo.nHeight, pData);            }            else            {                if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed)//RGB                {                    Hobj.Dispose();                    HOperatorSet.GenImageInterleaved(out  Hobj, pData, "rgb", (int)pFrameInfo.nWidth, (int)pFrameInfo.nHeight, 0, "byte", (int)pFrameInfo.nWidth, (int)pFrameInfo.nHeight, 0, 0, -1, 0);                }                else                {                                       if (pBufForSaveImage == IntPtr.Zero)                    {                        pBufForSaveImage = Marshal.AllocHGlobal((int)(pFrameInfo.nWidth * pFrameInfo.nHeight * 3 + 2048));                    }                    MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();                    stConverPixelParam.nWidth = pFrameInfo.nWidth;                     stConverPixelParam.nHeight = pFrameInfo.nHeight;                    stConverPixelParam.pSrcData = pData;                    stConverPixelParam.nSrcDataLen = pFrameInfo.nFrameLen;                    stConverPixelParam.enSrcPixelType = pFrameInfo.enPixelType;                    stConverPixelParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;//在此处选择需要转换的目标类型                    stConverPixelParam.pDstBuffer = pBufForSaveImage;                    stConverPixelParam.nDstBufferSize = (uint)(pFrameInfo.nWidth * pFrameInfo.nHeight * 3 + 2048);                    m_MyCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);                    Hobj.Dispose();                    HOperatorSet.GenImageInterleaved(out  Hobj, pBufForSaveImage, "rgb", (int)pFrameInfo.nWidth, (int)pFrameInfo.nHeight, 0, "byte", (int)pFrameInfo.nWidth, (int)pFrameInfo.nHeight, 0, 0, -1, 0);                }                           }            if (Hobj.CountObj() > 0)            {                Image_Delegate?.Invoke(Cam_name, Hobj?.Clone());            }                       Hobj?.Dispose();                        System.GC.Collect();        }        public static byte[] IntPtr2Bytes(IntPtr ptr, int size)        {             if (ptr == IntPtr.Zero)            {                return null;            }             byte[] bytes = new byte[size];            Marshal.Copy(ptr, bytes, 0, size);            return bytes;        }        /// <summary>        /// 黑白图像        /// </summary>        /// <param name="enType"></param>        /// <returns></returns>        private bool IsMonoPixelFormat(MyCamera.MvGvspPixelType enType)        {            switch (enType)            {                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:                    return true;                default:                    return false;            }        }        /// <summary>        /// 彩色图像        /// </summary>        /// <param name="enType"></param>        /// <returns></returns>        private bool IsColorPixelFormat(MyCamera.MvGvspPixelType enType)        {            switch (enType)            {                case MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_RGBA8_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BGRA8_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR8:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG8:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB8:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG8:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12_Packed:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12:                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12_Packed:                    return true;                default:                    return false;            }        }        /// <summary>        /// 设置曝光        /// </summary>        /// <param name="Value"></param>        /// <returns></returns>        public override double SetExposureTime(double Value)        {            m_MyCamera.MV_CC_SetEnumValue_NET("ExposureAuto", 0);            MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();            m_MyCamera.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);            if (Value <= stParam.fMax && Value >= stParam.fMin)            {                m_MyCamera.MV_CC_SetFloatValue_NET("ExposureTime", (float)Value);            }            return stParam.fCurValue;        }        /// <summary>        /// 设置增益        /// </summary>        /// <param name="Value"></param>        /// <returns></returns>        public override double SetGainRaw(double Value)        {            m_MyCamera.MV_CC_SetEnumValue_NET("GainAuto", 0);            MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();            m_MyCamera.MV_CC_GetFloatValue_NET("Gain", ref stParam);            if (Value <= stParam.fMax && Value >= stParam.fMin)            {                m_MyCamera.MV_CC_SetFloatValue_NET("Gain", (float)Value);            }            return stParam.fCurValue;        }        /// <summary>        /// 获取触发模式        /// </summary>        /// <param name="mode"></param>        public override void SetTriggerMode(TriggerMode mode)        {            if (mode == TriggerMode.Off)            {                m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF);            }            else            {                m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON);            }        }        /// <summary>        /// 设置触发源        /// </summary>        /// <param name="source"></param>        public override void SetTriggerSource(TriggerSource source)        {            m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON);            switch (source)            {                case TriggerSource.Line1:                    m_MyCamera.MV_CC_SetEnumValue_NET("TriggerSource", (uint)MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE0);                    break;                case TriggerSource.Line2:                    m_MyCamera.MV_CC_SetEnumValue_NET("TriggerSource", (uint)MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE1);                    break;                case TriggerSource.Software:                    m_MyCamera.MV_CC_SetEnumValue_NET("TriggerSource", (uint)MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_SOFTWARE);                    break;                default:                    break;            }        }        /// <summary>        /// 软触发一次        /// </summary>        public override bool TriggerSoftware()        {            if (m_MyCamera == null) return false;            if (GetTriggerMode()==TriggerMode.Off)            {                m_MyCamera.MV_CC_SetEnumValue_NET("TriggerMode", (uint)MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON);                m_MyCamera.MV_CC_SetEnumValue_NET("TriggerSource", (uint)MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_SOFTWARE);            }                       m_MyCamera.MV_CC_SetCommandValue_NET("TriggerSoftware");            return true;        }    }

C#海康威视无法加载DLL“..\bin\HCNetSDK.dll”

项目场景

c#海康威视:System.DllNotFoundException:“无法加载 DLL“&hellip;\bin\HCNetSDK.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”

问题描述

将海康威视的SDK直接复制到我的项目中时,使用到SDK中的方法就出现这个异常。

将demo里的dll放在bin文件夹和bin\Debug文件夹里时也会出错。

C#中怎么连接海康威视

原因分析

这个肯定是路径出错了,不然不会报这个找不到指定模块的异常。

解决方案

在代码中选中方法函数,右键转到定义:

C#中怎么连接海康威视

将\bin\HCNetSDK.dll改成\Debug\HCNetSDK,这样就不会报错了。

感谢各位的阅读,以上就是“C#中怎么连接海康威视”的内容了,经过本文的学习后,相信大家对C#中怎么连接海康威视这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯