C# 与 Halcon 联合编程连接海康威视相机

以下内容来自海康威视SDK:

C# 与 Halcon 联合编程连接海康威视相机_第1张图片

 

   #region 海康威视 全局
        MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList;
        private MyCamera m_pMyCamera;
        bool m_bGrabbing;
        byte[] m_pBufForSaveImage = new byte[50 * 1024 * 1024];
        HWindow m_Window;
        MyCamera.cbOutputExdelegate ImageCallback;
        #endregion
        #region 初始化
        public Camera2()
        {
            InitializeComponent();

        }
        #endregion      
        #region 海康威视
        private void DisplayWindowsInitial()
        {
            // ch: 定义显示的起点和宽高 || en: Definition the width and height of the display window
            HTuple hWindowRow, hWindowColumn, hWindowWidth, hWindowHeight;

            // ch: 设置显示窗口的起点和宽高 || en: Set the width and height of the display window
            hWindowRow = 0;
            hWindowColumn = 0;
            hWindowWidth = hWindowControl1.Width;
            hWindowHeight = hWindowControl1.Height;

            try
            {
                HTuple hWindowID = (HTuple)hWindowControl1.HalconWindow;
                m_Window.OpenWindow(hWindowRow, hWindowColumn, hWindowWidth, hWindowHeight, hWindowID, "visible", "");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

        }
        private void DeviceListAcq()
        {
            int nRet;
            // ch:创建设备列表 || en: Create device list
            System.GC.Collect();
            cbDeviceList.Items.Clear();
            nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
            if (0 != nRet)
            {
                MessageBox.Show("Enum Devices Fail");
                return;
            }

            // ch:在窗体列表中显示设备名 || Display the device'name on window's list
            for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.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)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                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));
                    if (usbInfo.chUserDefinedName != "")
                    {
                        cbDeviceList.Items.Add("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        cbDeviceList.Items.Add("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }
            }

            //.ch: 选择第一项 || en: Select the first item
            if (m_pDeviceList.nDeviceNum != 0)
            {
                cbDeviceList.SelectedIndex = 0;
            }
        }
        private void GrabImage(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
        {
            if (pData != null)
            {
                uint nWidth = pFrameInfo.nWidth;

                uint nHeight = pFrameInfo.nHeight;

                Marshal.Copy(pData, m_pBufForSaveImage, 0, (int)nWidth * (int)nHeight * 3);

                HObject Hobj = new HObject();
                UInt32 nSupWidth = (pFrameInfo.nWidth + (UInt32)3) & 0xfffffffc;
                Int32 nLength = (Int32)pFrameInfo.nWidth * (Int32)pFrameInfo.nHeight;

                byte[] m_pDataForRed = new byte[(int)nWidth * (int)nHeight];
                byte[] m_pDataForGreen = new byte[(int)nWidth * (int)nHeight];
                byte[] m_pDataForBlue = new byte[(int)nWidth * (int)nHeight];

                for (int nRow = 0; nRow < pFrameInfo.nHeight; nRow++)
                {
                    for (int col = 0; col < pFrameInfo.nWidth; col++)
                    {
                        m_pDataForRed[nRow * nSupWidth + col] = m_pBufForSaveImage[nRow * pFrameInfo.nWidth * 3 + (3 * col)];
                        m_pDataForGreen[nRow * nSupWidth + col] = m_pBufForSaveImage[nRow * pFrameInfo.nWidth * 3 + (3 * col + 1)];
                        m_pDataForBlue[nRow * nSupWidth + col] = m_pBufForSaveImage[nRow * pFrameInfo.nWidth * 3 + (3 * col + 2)];
                    }
                }

                IntPtr RedPtr = BytesToIntptr(m_pDataForRed);
                IntPtr GreenPtr = BytesToIntptr(m_pDataForGreen);
                IntPtr BluePtr = BytesToIntptr(m_pDataForBlue);
                try
                {
                    HOperatorSet.GenImage3(out Hobj, (HTuple)"byte", pFrameInfo.nWidth, pFrameInfo.nHeight,
                                        (new HTuple(RedPtr)), (new HTuple(GreenPtr)), (new HTuple(BluePtr)));
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                // ch: 显示 || display
                HTuple hImageWidth = 0;
                HTuple hImageHeight = 0;
                HTuple point = null;
                HTuple type = null;

                try
                {
                    HOperatorSet.GetImagePointer1(Hobj, out point, out type, out hImageWidth, out hImageHeight);//.ch: 得到图像的宽高和指针 || en: Get the width and height of the image
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                try
                {
                    HOperatorSet.SetPart(m_Window, 0, 0, hImageHeight, hImageWidth);// ch: 使图像显示适应窗口大小 || en: Make the image adapt the window size
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                if (m_bGrabbing)
                {
                    if (m_Window == null)
                    {
                        return;
                    }
                    try
                    {
                        HOperatorSet.DispObj(Hobj, m_Window);// ch 显示 || en: display
                        Marshal.FreeHGlobal(RedPtr);// ch 释放空间 || en: release space
                        Marshal.FreeHGlobal(GreenPtr);// ch 释放空间 || en: release space
                        Marshal.FreeHGlobal(BluePtr);// ch 释放空间 || en: release space
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                }
            }
            return;
        }
        private void SetCtrlWhenOpen()
        {
            bnOpen.Enabled = false;

            bnClose.Enabled = true;
            bnStartGrab.Enabled = true;
            bnStopGrab.Enabled = false;
            bnContinuesMode.Enabled = true;
            bnContinuesMode.Checked = true;
            bnTriggerMode.Enabled = true;
            cbSoftTrigger.Enabled = false;
            bnTriggerExec.Enabled = false;

        }
        private void SetCtrlWhenStopGrab()
        {
            bnStartGrab.Enabled = true;
            bnStopGrab.Enabled = false;

            bnTriggerExec.Enabled = false;
        }
        private void SetCtrlWhenClose()
        {
            bnOpen.Enabled = true;

            bnClose.Enabled = false;
            bnStartGrab.Enabled = false;
            bnStopGrab.Enabled = false;
            bnContinuesMode.Enabled = false;
            bnTriggerMode.Enabled = false;
            cbSoftTrigger.Enabled = false;
            bnTriggerExec.Enabled = false;

        }
        public static IntPtr BytesToIntptr(byte[] bytes)
        {
            int size = bytes.Length;
            IntPtr buffer = Marshal.AllocHGlobal(size);
            Marshal.Copy(bytes, 0, buffer, size);
            return buffer;
        }
        private void SetCtrlWhenStartGrab()
        {


            bnStartGrab.Enabled = false;
            bnStopGrab.Enabled = true;

            if (bnTriggerMode.Checked && cbSoftTrigger.Checked)
            {
                bnTriggerExec.Enabled = true;
            }

        }
        #endregion
        #region 采集 
        //采集
        private void bnStartGrab_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                int nRet;

                // ch:开启抓图 | en:start grab
                nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    MessageBox.Show("Start Grabbing Fail");
                    return;
                }

                // ch: 控件操作 || en: Control operation
                SetCtrlWhenStartGrab();
                m_bGrabbing = true;
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion
        #region 停止采集
        //停止采集
        private void bnStopGrab_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                int nRet = -1;
                // ch:停止抓图 || en:Stop grab image
                nRet = m_pMyCamera.MV_CC_StopGrabbing_NET();
                if (nRet != MyCamera.MV_OK)
                {
                    MessageBox.Show("Stop Grabbing Fail");
                }
                m_bGrabbing = false;
                // ch: 控件操作 || en: Control operation
                SetCtrlWhenStopGrab();
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion
        #region   软触发一次

        //软触发一次
        private void bnTriggerExec_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                int nRet;

                // ch: 触发命令 || en: Trigger command
                nRet = m_pMyCamera.MV_CC_SetCommandValue_NET("TriggerSoftware");
                if (MyCamera.MV_OK != nRet)
                {
                    MessageBox.Show("Trigger Fail");
                }
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion
        #region  选择相机
        //选择相机
        private void radioButton3_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                m_pDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
                m_pMyCamera = new MyCamera();
                m_bGrabbing = false;
                m_Window = new HWindow();
                DisplayWindowsInitial();
                DeviceListAcq();
            }
            else if (radioButton2.Checked == true)//大华
            {
                /* 设备搜索 */
                List li = Enumerator.EnumerateDevices();
                if (li.Count > 0)
                {
                    for (int i = 0; i < li.Count; i++)
                    {
                        cbDeviceList.Items.Add(li[i].Key);
                    }
                    cbDeviceList.Text = li[0].Key;
                    bnOpen.Enabled = true;
                }
            }
            else if (radioButton2.Checked == true)//basler
            {
                //UpdateDeviceList();
            }
        }
        #endregion
        #region 其他事件
        private void bnContinuesMode_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                if (bnContinuesMode.Checked)
                {
                    m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);
                    cbSoftTrigger.Enabled = false;
                    bnTriggerExec.Enabled = false;
                }
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }

        private void bnTriggerMode_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                if (bnTriggerMode.Checked)
                {
                    m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 1);

                    if (cbSoftTrigger.Checked)
                    {
                        m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 7);
                        if (m_bGrabbing)
                        {
                            bnTriggerExec.Enabled = true;
                        }
                    }
                    else
                    {
                        m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 0);
                    }
                    cbSoftTrigger.Enabled = true;
                }
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }

        private void cbSoftTrigger_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                if (cbSoftTrigger.Checked)
                {

                    // ch: 触发源设为软触发 || en: set trigger mode as Software
                    m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 7);
                    if (m_bGrabbing)
                    {
                        bnTriggerExec.Enabled = true;
                    }
                }
                else
                {
                    m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 0);
                    bnTriggerExec.Enabled = false;
                }
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion      
        #region 关闭
        //关闭
        private void bnClose_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                if (m_bGrabbing)
                {
                    m_bGrabbing = false;
                    // ch:停止抓图 || en:Stop grab image
                    m_pMyCamera.MV_CC_StopGrabbing_NET();

                    // ch: 控件操作 || en: Control operation
                    SetCtrlWhenStopGrab();
                }

                // ch:关闭设备 || en: Close device
                m_pMyCamera.MV_CC_CloseDevice_NET();

                // ch: 控件操作 || en: Control operation
                SetCtrlWhenClose();

                m_bGrabbing = false;
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion
        #region 打开
        //打开
        private void bnOpen_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                if (m_pDeviceList.nDeviceNum == 0 || cbDeviceList.SelectedIndex == -1)
                {
                    MessageBox.Show("No device,please select");
                    return;
                }
                int nRet = -1;

                //ch:获取选择的设备信息 | en:Get selected device information
                MyCamera.MV_CC_DEVICE_INFO device =
                    (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[cbDeviceList.SelectedIndex],
                                                                  typeof(MyCamera.MV_CC_DEVICE_INFO));

                nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref device);
                if (MyCamera.MV_OK != nRet)
                {
                    return;
                }

                // ch:打开设备 | en:Open device
                nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    MessageBox.Show("Open Device Fail");
                    return;
                }

                // ch:设置触发模式为off || en:set trigger mode as off
                m_pMyCamera.MV_CC_SetEnumValue_NET("AcquisitionMode", 2);
                m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 0);

                /**********************************************************************************************************/
                // ch:注册回调函数 | en:Register image callback
                ImageCallback = new MyCamera.cbOutputExdelegate(GrabImage);

                nRet = m_pMyCamera.MV_CC_RegisterImageCallBackForRGB_NET(ImageCallback, IntPtr.Zero);
                if (MyCamera.MV_OK != nRet)
                {
                    MessageBox.Show("Register Image CallBack Fail");
                }
                /**********************************************************************************************************/
                SetCtrlWhenOpen();
            }
            else if (radioButton2.Checked == true)//大华
            {

            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion
        #region 设备
        //设备
        private void bnEnum_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)//海康威视
            {
                DeviceListAcq();
            }
            else if (radioButton2.Checked == true)//大华
            {
                equipment();
            }
            else if (radioButton2.Checked == true)//basler
            {

            }

        }
        #endregion      
        #region  大华
        private void equipment()
        {
            List li = Enumerator.EnumerateDevices();
            if (li.Count > 0)
            {
                for (int i = 0; i < li.Count; i++)
                {
                    cbDeviceList.Items.Add(li[i].Key);
                }
                cbDeviceList.Text = li[0].Key;
                bnOpen.Enabled = true;
            }
        }
        #endregion

 

你可能感兴趣的:(C# 与 Halcon 联合编程连接海康威视相机)