首頁 > 軟體

C#中如何連線海康威視

2023-02-27 06:00:34

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#海康威視無法載入DLL“..binHCNetSDK.dll”

專案場景

c#海康威視:System.DllNotFoundException:“無法載入 DLL“…binHCNetSDK.dll”: 找不到指定的模組。 (異常來自 HRESULT:0x8007007E)。”

問題描述

將海康威視的SDK直接複製到我的專案中時,使用到SDK中的方法就出現這個異常。

將demo裡的dll放在bin資料夾和binDebug資料夾裡時也會出錯。

原因分析

這個肯定是路徑出錯了,不然不會報這個找不到指定模組的異常。

解決方案

在程式碼中選中方法函數,右鍵轉到定義:

將binHCNetSDK.dll改成DebugHCNetSDK,這樣就不會報錯了。

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com