CodeManager.cs 7.6 KB
using CodeLibrary;
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.Runtime.ExceptionServices;
using System.Text;
using System.Windows.Forms;

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>
        /// <param name="nameStr">摄像机名称,多个用#分割</param>
        /// <param name="codeStr">二维码类型,多个用#分割</param>
        public static void LoadConfig(string nameStr, string codeStr)
        {
            cameraNameList = new List<string>();
            codeTypeList = new List<string>();
            HDLogUtil.LogName = "RollingLogFileAppender";
            try
            {
                string[] nameArray = nameStr.Split(spiltChar);
                foreach (string str in nameArray)
                {
                    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);

            }
            catch (Exception ex)
            {
                LogUtil.error("解析摄像机配置出错:" + ex.ToString());
            }
        }
        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()
        {
            //BaslerCamera.Instance.Close();
            //HIKCamera.Instance.Close();
            Camera._cam.CloseAll();
        }

        //public static Bitmap GetCamerImage(string cameraName)
        //{
        //    Bitmap bitm = null;
        //    try
        //    {
        //        bitm = Camera._cam.GrabOneImage(cameraName);
        //    }
        //    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)
                {
                    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;
                        }
                        //Bitmap bit = new Bitmap(bitmap);
                        //LogUtil.debug(" 摄像机【" + cameraName + "】获取图片完成");
                        //System.Threading.Thread.Sleep(1);
                        //HObject ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(bit);
                        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, 1, GetCodeParamFilePath(codeType), codeType);
                            }
                            LogUtil.info("  【" + cameraName + "】【"+ codeType + "】扫码完成");
                        }
                        allCodeList.AddRange(cc);
                    }
                    catch (AccessViolationException e)
                    {
                        LogUtil.error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
                        Camera._cam.Close(cameraName);
                        //  GC.Collect();
                    }
                    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());
            }
            foreach (CodeInfo info in allCodeList)
            {
                codeList.Add(info.CodeStr);
            }
            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 "";
            }
        }
       
    }
}