CodeManager.cs 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
 
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 int DeCodeType = ConfigAppSettings.GetIntValue(Setting_Init.DeCodeType);

        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)
            {
                try
                {
                    CodeLibrary.HIKCamera.Instance.Load();
                }
                catch (Exception ex)
                {
                    LogUtil.error("加载HIK相机出错:" + ex.ToString());
                }
                try
                {
                    CodeLibrary.BaslerCamera.Instance.Load();
                }
                catch (Exception ex)
                {
                    LogUtil.error("加载Basler相机出错:" + ex.ToString());
                }
            }
            string[] names = CodeLibrary.HIKCamera.Instance.CameraName;


            if (names != null)
            {
                hikNameList.AddRange(names);
                foreach (string name in hikNameList)
                {
                    LogUtil.info("加载到HIK相机:" + name);
                }
            }
            names = CodeLibrary.BaslerCamera.Instance.CameraName;
            if (names != null)
            {
                balserNameList.AddRange(names);
                foreach (string name in balserNameList)
                {
                    LogUtil.info("加载到Balser相机:" + name);
                }
            }
        }
        public static void CloseCamera()
        {
            BaslerCamera.Instance.Close();
            HIKCamera.Instance.Close();
        }

        public static Bitmap GetCamerImage(string cameraName)
        {
            Bitmap bitm = null;
            try
            {
                if (balserNameList.Contains(cameraName))
                {
                    bool result = BaslerCamera.Instance.IsOpen;
                    if (!result)
                    {
                        result = BaslerCamera.Instance.Open(cameraName);
                        LogUtil.info("打开相机:" + cameraName + "(" + result + ")");
                    }
                    if (result)
                    {
                        BaslerCamera.Instance.GrabOne();
                        bitm = BaslerCamera.Instance.Image;
                        //BaslerCamera.Instance.Close();
                    }
                    else
                    {
                        LogUtil.error("相机【" + cameraName + "】打开失败:" + BaslerCamera.Instance.ErrInfo);
                    }
                }
                else if (hikNameList.Contains(cameraName))
                {
                    bool result = HIKCamera.Instance.IsOpen;
                    if (!result)
                    {
                        result = HIKCamera.Instance.Open(cameraName);
                        LogUtil.info("打开相机:" + cameraName + "(" + result + ")");
                    }
                    if (result)
                    {
                        HIKCamera.Instance.GrabOne();
                        bitm = HIKCamera.Instance.Image;
                        //HIKCamera.Instance.Close();
                    }
                    else
                    {
                        LogUtil.error("相机【" + cameraName + "】打开失败:" + HIKCamera.Instance.ErrInfo);
                    }
                }
                else
                {
                    LogUtil.info("未找到相机【" + cameraName + "】无法获取图片");
                    //若未加载到相机,需要重新加载相机
                    if (balserNameList.Count <= 0 && hikNameList.Count <= 0)
                    {
                        LogUtil.error("获取图片时发现未加载到相机,重新加载相机");
                        LoadCamera(true);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("从相机【" + cameraName + "】获取图片出错:" + ex.ToString());
            }
            return bitm;
        }

        [HandleProcessCorruptedStateExceptions]
        public static List<string> CameraScan()
        {
            List<string> codeList = new List<string>();
            //List<CodeInfo> allCodeList = new List<CodeInfo>();

            try
            {
                foreach (string cameraName in cameraNameList)
                {
                    using (Bitmap bitmap = GetCamerImage(cameraName))
                    {
                        if (bitmap == null)
                        {
                            LogUtil.error(" 摄像机【" + cameraName + "】获取图片失败,关闭相机");
                            CloseCamera();
                            continue;
                        }

                        LogUtil.debug(" 摄像机【" + cameraName + "】获取图片完成");
                        System.Threading.Thread.Sleep(1);
                        if (DeCodeType.Equals(1))
                        {
                            List<string> result = CodeLibrary.ZXingCodeHelper.DecodeQRCodes(bitmap);
                            return result;
                        }
                        else
                        {
                            HalconDotNet.HObject ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
                            LogUtil.debug(" 摄像机【" + 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, 1, GetCodeParamFilePath(codeType), codeType);
                                }
                                if (cc.Count > 0)
                                {
                                    string r = "";
                                    foreach (CodeInfo c in cc)
                                    {
                                        codeList.Add(c.CodeStr);
                                        r = r + "##" + c.CodeStr;
                                    }
                                    LogUtil.info(" 【" + cameraName + "】【" + codeType + "】扫码完成:" + r);
                                }
                            }

                        }
                    }
                }
            }
            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;
        }

        public static string ProcessXiaoGanCode(List<string> codeList)
        {
            string codeStr = "";
            foreach (string key in codeList)
            {
                if (key.Trim().Equals(""))
                {
                    continue;
                }
                codeStr = key.Trim() + ";";
            }
            if (codeStr.EndsWith(";"))
            {
                codeStr = codeStr.Substring(0, codeStr.Length - 1);
            }
            if (codeStr != null && codeStr.Contains(";"))
            {
                string[] codeArr = codeStr.Split(';');
                string pn = "";
                string qty = "";

                foreach (string temp in codeList)
                {
                    if (temp.StartsWith("DM"))
                    {
                        pn = temp;
                    }
                    if (temp.StartsWith("SL"))
                    {
                        qty = temp;
                    }
                }
                if (pn.Length != 0 && qty.Length != 0)
                {
                    return pn + "@" + codeStr + "@" + qty;
                }
            }
            return codeStr;
        }
        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)
            {
                //成都料仓打印的条码,PN是以:PN;开头的
                string code = pCode[0];
                if (code.Contains("PN;"))
                {
                    code=  code.Replace("PN;", "PN:");
                }
                int subCount = start.Length;
                if (code.Substring(start.Length, 1).Equals(":"))
                {
                    subCount = start.Length + 1; 
                } 
                return code.Substring(subCount, code.Length - subCount); 
            }
            return "";
        }
    }
}