HDCodeHelper.cs 6.3 KB
using HalconDotNet;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeTest
{
    public class HDCodeHelper
    { 
       /// <summary>
       /// 显示的窗口
       /// </summary>
        public static HWindow HalconWindow = null;
        /// <summary>
        /// 扫码时是否使用参数文件
        /// </summary>
        public static bool IsUseParam = true;
        public static List<string> DecodeCode(string filePath, int codeCount, params string[] paramType)
        {
            HObject ho_Image;
            HOperatorSet.GenEmptyObj(out ho_Image);
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, filePath);
            if (HalconWindow != null)
            {
                HOperatorSet.DispObj(ho_Image, HalconWindow);
            }
            return DecodeCode(ho_Image, codeCount, paramType);
        }
        public static List<string> DecodeCode(Bitmap map, int codeCount, params string[] paramType)
        {
            HObject ho_image = Bitmap2HObjectBpp24(map);
            return DecodeCode(ho_image, codeCount, paramType  ) ;
        }
        public static List<string> DecodeCode(HObject ho_Image, int codeCount, params string[] paramType)
        {
            List<string> codeType = new List<string>(paramType.ToList());
            if (codeType.Count<string>() <= 0)
            {
                codeType.Add("Data Matrix ECC 200");
            }
            List<string> codeList = new List<string>();

            foreach (string t in codeType)
            {
                string[] array = GetCode(ho_Image, t, codeCount);
                codeList.AddRange(array.ToArray());
            }
            return codeList;
        }
        private static string[] GetCode(HObject ho_Image, string symbolType,int codeCount)
        {
            try
            { 
                HObject ho_SymbolXLDs;
                 
                HTuple hv_ResultHandles = null; 
                HTuple hv_DecodedDataStrings = null;
                HTuple hv_DataCodeHandle = null; 
                HOperatorSet.GenEmptyObj(out ho_SymbolXLDs); 
                HOperatorSet.CreateDataCode2dModel(symbolType, "default_parameters", "maximum_recognition", out hv_DataCodeHandle);

                string hv_model_path = GetCodeParamFilePath(symbolType);
                if (!hv_model_path.Equals("")&&IsUseParam)
                {
                    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);
                }
                if (HalconWindow != null)
                {
                    ShowImage(HalconWindow, ho_Image, ho_SymbolXLDs);
                }
                HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
                string[] resultList = hv_DecodedDataStrings.SArr;
                return resultList;
            }
            catch (Exception ex)
            {
                return new string[] { };
            }
        }
        //private static  int dWidth = 0;
        //private static  int dHeight = 0;
        //public static void ClearShowInfo()
        //{
        //    dWidth = 0;
        //    dHeight = 0;
        //}
        private static void ShowImage(HWindow window, HObject ho_Image, HObject ho_SymbolXLDs)
        {
            if (window == null || ho_Image == null)
            {
                return;
            }
            Task.Factory.StartNew(delegate ()
            {
                try
                {

                    HTuple width, height;
                    //int dWidth = 0; int dHeight = 0;
                    HOperatorSet.GetImageSize(ho_Image, out width, out height);

                    int dWidth = (int)width.D;
                    int dHeight = (int)height.D;
                    HalconWindow.SetPart(0, 0, dHeight, dWidth);

                    HOperatorSet.SetColor(window, "red");
                    HOperatorSet.SetLineWidth(window, new HTuple(2));
                    HOperatorSet.DispObj(ho_Image, window);
                    HOperatorSet.DispObj(ho_SymbolXLDs, window);
                }
                catch (Exception ex)
                {

                }
            });
        }
        public static string GetParamFilePathName(string codeType)
        {
            string appPath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
            if (!Directory.Exists(appPath))
            {
                Directory.CreateDirectory(appPath);
            }

            string filePath = appPath + codeType + ".dcm";
            return filePath;
        }
        public static string GetCodeParamFilePath(string codeType)
        {
            string filePath = GetParamFilePathName(codeType);
            if (File.Exists(filePath))
            {
                return filePath;
            }
            else
            {
                return "";
            }
        }
        public static HObject Bitmap2HObjectBpp24(Bitmap bmp)
        {
            HObject ho_Image = null;
            try
            {
                HOperatorSet.GenEmptyObj(out ho_Image);
                ho_Image.Dispose();
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgrx", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                bmp.UnlockBits(srcBmpData);

            }
            catch (Exception ex)
            {
                ho_Image = null;
            }
            return ho_Image;
        }
    }
}