Halcon 进阶 三 C#实现 Halcon与工业相机(海康),视觉解析二维码

Halcon与工业相机(海康),视觉解析二维码

  • 图像结果
  • 工业相机连接
    • 1.查找设备
    • 2. 打开设备
    • 3. 设置软触发
    • 4. 设置硬触发
    • 5. 开始拍照
    • 6. 停止拍照
  • halcon解码
    • 1. 初始化
    • 2. 设置解码模版
    • 3. 设置窗体参数
    • 4. 解码
  • 软件操作
  • 下载链接

图像结果

Halcon 进阶 三 C#实现 Halcon与工业相机(海康),视觉解析二维码_第1张图片

工业相机连接

1.查找设备

   	public void Search_DeviceList()
       {
           int nRet;
           // ch:创建设备列表 en:Create Device List
           System.GC.Collect();
           nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);
           if (0 != nRet)
           {
               ShowErrorMsg("Enumerate devices fail!", 0);
               return;
           }
       }

2. 打开设备

 		public bool Open()
        {
            if (null == m_pMyCamera)
            {
                m_pMyCamera = new MyCamera();
                if (null == m_pMyCamera)
                {
                    return false;
                }
            }
            int nRet = -1;
            nRet = m_pMyCamera.MV_CC_CreateDevice_NET(ref mDevice);
            if (MyCamera.MV_OK != nRet)
            {
                return false;
            }
            nRet = m_pMyCamera.MV_CC_OpenDevice_NET();
            if (MyCamera.MV_OK != nRet)
            {
                m_pMyCamera.MV_CC_DestroyDevice_NET();
                return false;
            }
            // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
            if (mDevice.nTLayerType == MyCamera.MV_GIGE_DEVICE)
            {
                int nPacketSize = m_pMyCamera.MV_CC_GetOptimalPacketSize_NET();
                if (nPacketSize > 0)
                {
                    nRet = m_pMyCamera.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
                    if (nRet != MyCamera.MV_OK)
                    {
                        Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet);
                    }
                }
                else
                {
                    Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize);
                }
            }
            // ch:获取包大小 || en: Get Payload Size
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                return false;
            }
            g_nPayloadSize = stParam.nCurValue;

            // ch:获取高 || en: Get Height
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("Height", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                return false;
            }
            nHeight = stParam.nCurValue;

            // ch:获取宽 || en: Get Width
            nRet = m_pMyCamera.MV_CC_GetIntValue_NET("Width", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                return false;
            }
            nWidth = stParam.nCurValue;

            m_pDataForRed = new byte[nWidth * nHeight];
            m_pDataForGreen = new byte[nWidth * nHeight];
            m_pDataForBlue = new byte[nWidth * nHeight];
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 1);//触发模式打开 
            m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", 0);//设置外部触发
            return true;
        }

3. 设置软触发

		public void TriggerExec()
        {
            if (m_pMyCamera != null)
            {
                int nRet = m_pMyCamera.MV_CC_SetCommandValue_NET("TriggerSoftware");
                if (MyCamera.MV_OK != nRet)
                {
                    ShowErrorMsg("Trigger Fail", nRet);
                }
            }
        }

4. 设置硬触发

 		public void SetTriggerMode(uint TriggerSource)
        {
            if (m_pMyCamera != null)
            {
                m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerMode", 1);
                m_pMyCamera.MV_CC_SetEnumValue_NET("TriggerSource", TriggerSource);
            }
        }

5. 开始拍照

		public bool Start()
        {
            int nRet;

            try
            {
                if (m_pMyCamera != null)
                {
                    nRet = m_pMyCamera.MV_CC_StartGrabbing_NET();
                    if (MyCamera.MV_OK != nRet)
                    {
                        return false;
                    }
                    m_bGrabbing = true;
                }
                return false;
                if (Show_Image)
                {
                    nRet = m_pMyCamera.MV_CC_Display_NET(hWnd);
                    if (MyCamera.MV_OK != nRet)
                    {
                        ShowErrorMsg("显示失败", nRet);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return true;
        }

6. 停止拍照

 		public bool Stop()
        {
            if (m_pMyCamera != null)
            {
                int nRet = -1;
                // ch:停止采集 | en:Stop Grabbing
                nRet = m_pMyCamera.MV_CC_StopGrabbing_NET();
                if (nRet != MyCamera.MV_OK)
                {
                    return false;
                }
                // ch:标志位设为false | en:Set flag bit false
                m_bGrabbing = false;
            }
            return true;
        }

halcon解码

1. 初始化

   		public HWindow Open_Window(IntPtr handle, int pbWidth, int pbHeight)
        {
            HTuple hWindowRow, hWindowColumn;
            hWindowRow = 0;
            hWindowColumn = 0;
            HTuple hWindowID = (HTuple)handle;
            hv_WindowHandle = new HWindow();
            hv_WindowHandle.OpenWindow(hWindowRow, hWindowColumn, pbWidth, pbHeight, hWindowID, "visible", "");
            return hv_WindowHandle;
        }

2. 设置解码模版

 		public HTuple Create_QR_Code_Model()
        {
            hv_DataCodeHandle = new HTuple();
            //创建第一个二维码模版
            HOperatorSet.CreateDataCode2dModel("QR Code", "default_parameters", "maximum_recognition",
        out hv_DataCodeHandle);
            return hv_DataCodeHandle;
        }

3. 设置窗体参数

 		 /// 
        /// 设置显示窗体参数
        /// 
        /// 
        /// 
        /// 
        public void SetH_Window(HWindow hWindow, int vWidth, int vHeight)
        {
            HOperatorSet.SetWindowAttr("background_color", "black");
            //set_display_font(hv_WindowHandle, 10, "mono", "false", "false");
            HOperatorSet.SetLineWidth(hWindow, 1);
            HOperatorSet.SetColor(hWindow, "cyan");
            HDevWindowStack.Push(hWindow);
            HOperatorSet.SetPart(hWindow, 0, 0, vHeight - 1, vWidth - 1);

        }

4. 解码

 		public void get_reader_data_all_result(HObject ho_Image, out string decodeStrings, out string time, out string polarity, out string mirrored, out string version, out string error_correction_level
        {
            time = "0"; decodeStrings = ""; polarity = ""; mirrored = ""; version = "";
            error_correction_level = "";
            HObject ho_SymbolXLDs;
            HTuple hv_T1 = new HTuple(), hv_ResultHandles = new HTuple(), hv_ResultMessage = null; ;
            HTuple hv_DecodedDataStrings = new HTuple(), hv_T2 = new HTuple();
            HTuple hv_Time = new HTuple();
            HTuple  hv_c = null, hv_ResultVariousValues = null;
            HTuple hv_ResultVariousNames = new HTuple();
            HTuple hv_ResultVariousNames_cn = new HTuple();
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);

            //HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            try
            {
                HOperatorSet.CountSeconds(out hv_T1);
                ho_SymbolXLDs.Dispose();
                    HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, new HTuple(),new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
                    decodeStrings = hv_DecodedDataStrings.Length == 0 ? "ERROR" : hv_DecodedDataStrings.S;
                    HOperatorSet.CountSeconds(out hv_T2);
                    hv_Time = 1000 * (hv_T2 - hv_T1);
                    //hv_ResultMessage = ("Time: " + (hv_Time.TupleString(".2f"))) + " ms";
                    time = hv_Time.TupleString(".2f");
                    hv_ResultMessage = "Time: " + time + " ms";
                    HOperatorSet.DispObj(ho_Image, hv_WindowHandle);
                  
            }
            catch (Exception EX)
            {
                ho_SymbolXLDs.Dispose();
            }
        }

软件操作

打开软件,自动查找设备,依次点击 打开设备、初始化,然后点击开始采集。
连续模式:相机自动拍照返回照片,软件解码,
触发模式:不勾选软触发,是采用外部传感器触发。
勾选软触发,是点击后面 软件触发一次,相机拍照一次

下载链接

Halcon与工业相机(海康),视觉解析二维码

你可能感兴趣的:(#,Halcon,+,C#,进阶,c#,计算机视觉)