您好,登錄后才能下訂單哦!
這篇文章主要講解了“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_StopGrabbing_NET(); m_MyCamera.MV_CC_ClearImageBuffer_NET(); m_MyCamera.MV_CC_CloseDevice_NET(); //銷毀設備句柄 m_MyCamera.MV_CC_DestroyDevice_NET();*/ 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#海康威視:System.DllNotFoundException:“無法加載 DLL“…\bin\HCNetSDK.dll”: 找不到指定的模塊。 (異常來自 HRESULT:0x8007007E)。”
將海康威視的SDK直接復制到我的項目中時,使用到SDK中的方法就出現這個異常。
將demo里的dll放在bin文件夾和bin\Debug文件夾里時也會出錯。
這個肯定是路徑出錯了,不然不會報這個找不到指定模塊的異常。
在代碼中選中方法函數,右鍵轉到定義:
將\bin\HCNetSDK.dll改成\Debug\HCNetSDK,這樣就不會報錯了。
感謝各位的閱讀,以上就是“C#中怎么連接海康威視”的內容了,經過本文的學習后,相信大家對C#中怎么連接海康威視這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。