DHBarCodeHelper.cs 4.2 KB
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeLibrary
{
  public   class DHBarCodeHelper
    { /// <summary>
      /// 根据图片解析二维码
      /// </summary>
      /// <param name="ho_Image">Halcon中的图片对象</param>
      /// <param name="codeCount">二维码数量</param>
      /// <param name="codeParamPath">二维码参数路径,""表示不使用参数</param>
      /// <param name="paramType">二维码类型,不传类型默认Data Matrix ECC 200</param>
      /// <returns>解析到的二维码</returns>
        public static List<CodeInfo> DecodeCode(HObject ho_Image, int codeCount, string codeParamPath, params string[] paramType)
        {
            List<string> codeType = new List<string>(paramType.ToList());
            if (codeType.Count<string>() <= 0)
            {
                codeType.Add("CODE_39");
            }
            List<CodeInfo> codeList = new List<CodeInfo>();

            foreach (string t in codeType)
            {
                List<CodeInfo> array = GetCode(ho_Image, t, codeParamPath, codeCount);
                codeList.AddRange(array.ToArray<CodeInfo>());
            }
            return codeList;
        }
        private static List<CodeInfo> GetCode(HObject ho_Image, string symbolType, string hv_model_path, int codeCount)
        {
            List<CodeInfo> codeList = new List<CodeInfo>();
            try
            {
                HTuple hv_Area = null;
                HTuple hv_Row1 = null;
                HTuple hv_Column = null;
                HTuple hv_PointOrder = null;
                HObject ho_SymbolXLDs;

                HTuple hv_ResultHandles = null;
                HTuple hv_DecodedDataStrings = null;
                HTuple hv_DataCodeHandle = null;
                HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
                HOperatorSet.CreateBarCodeModel(symbolType, "default_parameters", "maximum_recognition", out hv_DataCodeHandle);

                //string hv_model_path = GetCodeParamFilePath(symbolType);
                if (!hv_model_path.Equals("") && File.Exists(hv_model_path))
                {
                    HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
                }

                ho_SymbolXLDs.Dispose();
                if (codeCount <= 0)
                {
                    HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
                    new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
                }
                else
                {
                    HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
                    "stop_after_result_num", codeCount, out hv_ResultHandles, out hv_DecodedDataStrings);
                }
                HOperatorSet.AreaCenterXld(ho_SymbolXLDs, out hv_Area, out hv_Row1, out hv_Column, out hv_PointOrder);
                if (HalconWindow != null)
                {
                    ShowImage(HalconWindow, ho_Image, ho_SymbolXLDs);
                }
                HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
                string[] resultList = hv_DecodedDataStrings.SArr;
                if (resultList.Length > 0)
                {
                    for (int i = 0; i < hv_DecodedDataStrings.SArr.Length; i++)
                    {
                        try
                        {
                            int x = (int)Math.Round(hv_Column.DArr[i]);
                            int y = (int)Math.Round(hv_Row1.DArr[i]);
                            string str = hv_DecodedDataStrings.SArr[i];
                            CodeInfo code = new CodeInfo(str, x, y);
                            codeList.Add(code);
                        }
                        catch (Exception ex)
                        {
                            HDLogUtil.error("处理二维码出错:索引=" + i + "," + resultList.ToString());
                        }
                    }
                }
                return codeList;
            }
            catch (Exception ex)
            {
                return codeList;
            }
        }
    }
}