CodeManager.cs 11.1 KB
 
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Windows.Forms;
using CodeLibrary;
namespace OnlineStore.DeviceLibrary
{
    public class CodeManager
    { 
        public static List<string> cameraNameList = new List<string>();
        public static List<string> codeTypeList = new List<string>();

        public static List<string> balserNameList = new List<string>();
        public static List<string> hikNameList = new List<string>();

        private static char spiltChar = '#';
        /// <summary>
        /// 初始化摄像机名称和二维码类型
        /// </summary> 
        public static void LoadConfig()
        {
            string nameStr = ConfigAppSettings.GetValue(Setting_Init.CameraName);
            string codeStr = ConfigAppSettings.GetValue(Setting_Init.CodeType);
            cameraNameList = new List<string>();
            codeTypeList = new List<string>();
            HDLogUtil.LogName = "RollingLogFileAppender";
            try
            {
                string[] nameArray = nameStr.Split(spiltChar);
                foreach (string str in nameArray)
                {
                    if (str.Trim().Equals(""))
                    {
                        continue;
                    }
                    //LogUtil.info("加载到配置摄像机名称:" + str.Trim());
                    cameraNameList.Add(str.Trim());
                }
                string[] codeArray = codeStr.Split(spiltChar);
                foreach (string str in codeArray)
                {
                    if (str.Trim().Equals(""))
                    {
                        continue;
                    }
                    LogUtil.info("加载到配置二维码类型:" + str.Trim());
                    codeTypeList.Add(str.Trim());
                }

                LoadCamera(false);
                CodeLibrary.HDCodeLearnHelper.LoadConfig(nameStr, codeStr);

                //如果未加载到配置相机,自动配置
                if (cameraNameList.Count <= 0 && hikNameList.Count > 0)
                {
                    cameraNameList = new List<string>(hikNameList);
                    string cameraStr = "";
                    foreach (string name in hikNameList)
                    {
                        cameraStr += name + spiltChar;
                    }
                    cameraStr = cameraStr.Substring(0, cameraStr.Length - 1);
                    ConfigAppSettings.SaveValue(Setting_Init.CameraName, cameraStr);
                    LogUtil.info("未配置扫码相机,默认【" + Setting_Init.CameraName + "】=【" + cameraStr + "】");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("解析摄像机配置出错:" + ex.StackTrace);
            }
        }
        private static void LoadCamera(bool isReLoad)
        {
            if (isReLoad || Camera._cam == null)
            {
                try
                {
                    if (Camera._cam != null)
                    {
                        Camera._cam.CloseAll();
                    }
                    Camera.Type = CameraType.HIK;
                    Camera._cam.Load();
                }
                catch (Exception ex)
                {
                    LogUtil.error("加载HIK相机出错:" + ex.ToString());
                }
            }
            string[] names = Camera._cam.Name;


            if (names != null)
            {
                foreach (string n in names)
                {
                    if (!hikNameList.Contains(n))
                    {
                        hikNameList.Add(n);
                    }
                }
                // hikNameList.AddRange(names);
                foreach (string name in hikNameList)
                {
                    LogUtil.info("加载到HIK相机:" + name);
                }
            }
        }

        public static void CloseCamera(string cameraName)
        {
            Camera._cam.Close(cameraName);
        }
        public static void CloseAllCamera()
        {
            Camera._cam.CloseAll();
        }


        [HandleProcessCorruptedStateExceptions]
        public static List<CodeInfo> CameraScan()
        {
            List<CodeInfo> codeList = new List<CodeInfo>();
            if (cameraNameList == null || cameraNameList.Count <= 0)
            {
                return codeList;
            }
            try
            {
                foreach (string cameraName in cameraNameList)
                {
                    if (cameraName.Trim().Equals(""))
                    {
                        continue;
                    }
                    DateTime startTime = DateTime.Now;
                    LogUtil.info(" 【" + cameraName + "】开始取图片");

                    HalconDotNet.HObject ho_Image = null;
                    try
                    {
                        ho_Image = Camera._cam.CaptureOnImage(cameraName);
                        if (ho_Image == null)
                        {
                            LogUtil.error("  【" + cameraName + "】取图片失败[" + Camera._cam.ErrInfo + "],关闭相机");
                            CloseCamera(cameraName);
                            continue;
                        }
                        LogUtil.info("  【" + cameraName + "】取图片完成,开始扫码");
                        List<CodeInfo> cc = new List<CodeInfo>();
                        foreach (string codeType in codeTypeList)
                        {
                            //判断是否是一维码
                            if (codeType.ToLower().Equals("barcode"))
                            {
                                cc = HDCodeHelper.DecodeBarCode(ho_Image);
                            }
                            else
                            {
                                cc = HDCodeHelper.DecodeCode(ho_Image, 0, GetCodeParamFilePath(codeType), codeType);
                            }
                            if (cc.Count > 0)
                            {
                                string r = "";
                                foreach (CodeInfo c in cc)
                                {
                                    codeList.Add(c);
                                    r = r + "##" + c.CodeStr;
                                }
                                LogUtil.info(" 【" + cameraName + "】【" + codeType + "】扫码完成:" + r);
                            }
                        }

                    }
                    catch (AccessViolationException e)
                    {
                        LogUtil.error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
                        Camera._cam.Close(cameraName);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.error(" 扫码出错:" + ex.ToString());
                    }
                    finally
                    {
                        if (ho_Image != null)
                        {
                            ho_Image.Dispose();
                        }
                    }
                }

            }
            catch (AccessViolationException e)
            {
                LogUtil.error("扫码出现AccessViolationException异常:" + e.ToString());
                GC.Collect();
            }
            catch (Exception ex)
            {
                LogUtil.error("扫码出错:" + ex.ToString());
            }
            return codeList;
        }


        public static string GetCodeParamFilePath(string codePath)
        {
            string appPath = Application.StartupPath;
            string path = appPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
            string filePath = path + codePath + ".dcm";
            if (File.Exists(filePath))
            {
                return filePath;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 西安料仓二维码解析处理
        /// </summary> 
        public static string GetBarcode(string codeStr)
        {
            if (!string.IsNullOrEmpty(codeStr))
            {
                string[] codeInfos = codeStr.Replace("\r", "").Split('\n');
                string ri = "";
                string qty = "";
                string youxiao = "";
                foreach (string codeInfo in codeInfos)
                {
                    if (codeInfo.StartsWith("物料编码:") || codeInfo.StartsWith("物料编码:"))
                    {
                        ri = codeInfo.Substring(5);
                    }
                    else if (codeInfo.StartsWith("入库数量:") || codeInfo.StartsWith("入库数量:"))
                    {
                        qty = codeInfo.Substring(5);
                    }
                    else if (codeInfo.StartsWith("有效期限:") || codeInfo.StartsWith("有效期限:"))
                    {
                        youxiao = codeInfo.Substring(5);
                    }
                }

                if (!string.IsNullOrEmpty(ri))
                {
                    string pn = ri.Split(' ')[0];
                    return pn + ";" + qty + ";" + ri + ";" + youxiao;
                }
            }
            return "";
        }

        public static string ProcessChengDuCode(List<string> codeList)
        {
            string split = ";";
            string result = "";
            try
            {
                List<string> rCodes = new List<string>();

                rCodes.Add(GetCodeByStart(codeList, "RI"));
                rCodes.Add(GetCodeByStart(codeList, "PN"));

                //P条码改为	P;H;Q或者	D;H;Q的格式
                foreach (string s in rCodes)
                {
                    if (!s.Equals(""))
                    {
                        if (result.Equals(""))
                        {
                            result = s;
                        }
                        else
                        {
                            result = result + split + s;
                        }
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                LogUtil.error("处理二维码出错:" + ex.ToString());
            }
            return result;
        }


        private static string GetCodeByStart(List<string> codeList, string start)
        {
            List<string> pCode = (from m in codeList where m.StartsWith(start) select m).ToList<string>();
            if (pCode.Count > 0)
            {
                int subCount = start.Length;
                if (pCode[0].Substring(start.Length, 1).Equals(":"))
                {
                    subCount = start.Length + 1;
                }
                return pCode[0].Substring(subCount, pCode[0].Length - subCount);
            }
            return "";
        }
    }
}