UsbCameraHDevelop.cs 9.2 KB
using URSoldering.Common;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace URSoldering.DeviceLibrary
{
    public class UsbCameraHDevelop
    {
        //private static HTuple hv_AcqHandle = null;
        public static Dictionary<string, HTuple> cameraHtuple = new Dictionary<string, HTuple>();
        private static List<string> codeTypeList = null;
        private static char spiltChar = '#';
        public static List<string> GetCodeTypeList()
        {
            if (codeTypeList == null)
            {
                codeTypeList = new List<string>();
                try
                {
                    string codeStr = ConfigAppSettings.GetValue(Setting_Init.CodeType);
                    string[] codeArray = codeStr.Split(spiltChar);
                    foreach (string str in codeArray)
                    {
                        LogUtil.info("加载到条码类型:" + str.Trim());
                        codeTypeList.Add(str.Trim());
                    }

                }
                catch (Exception ex)
                {
                    LogUtil.error("解析摄像机配置出错:" + ex.ToString());
                }
            }
            return codeTypeList;
        }
        public static bool OpenCamera(string cameraName)
        {
            try
            {
                //[1]SMI 

                //             HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                //-1, "false", "default", "[0] Integrated Camera", 0, -1, out hv_AcqHandle);
                HTuple hv_AcqHandle = null;
                HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                   -1, "false", "default", cameraName, 0, -1, out hv_AcqHandle);

                HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
                cameraHtuple.Add(cameraName, hv_AcqHandle);
                return true; 
            }
            catch (Exception ex)
            {
                LogUtil.error("打开摄像机【" + cameraName + "】失败:" + ex.ToString() + ",直接关闭摄像机");
                CloseCamera(cameraName);
                return false;
            }
        }

        public static void CloseAllCamera()
        {
            List<string> keys = new List<string>(cameraHtuple.Keys);
            foreach (string key in keys)
            {
                CloseCamera(key);
            }
        }

        public static HObject GrabImage(string cameraName)
        {
            HObject ho_Image = null;
            try
            {
                if (cameraHtuple.ContainsKey(cameraName))
                {
                    HTuple hv_AcqHandle = cameraHtuple[cameraName];
                      HOperatorSet.GenEmptyObj(out ho_Image);
                    ho_Image.Dispose();
                    if (hv_AcqHandle != null)
                    {
                        HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("获取摄像机【"+ cameraName + "】的图片出错:" + ex.ToString());
            }
            return ho_Image;
        }
        public static bool IsOpen(string cameraStr)
        {
            return cameraHtuple.ContainsKey(cameraStr);
        }
        public static void CloseCamera(string cameraName)
        {
            try
            {
                if (cameraHtuple.ContainsKey(cameraName))
                {
                    HTuple hv_AcqHandle = cameraHtuple[cameraName];
                    if (hv_AcqHandle != null)
                    {
                        HOperatorSet.CloseFramegrabber(hv_AcqHandle);
                        hv_AcqHandle = null; 
                    }
                    cameraHtuple.Remove(cameraName);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("关闭摄像机【" + cameraName + "】失败:" + ex.ToString());
            }
        }
        public static List<string> GetCode(HObject ho_Image)
        {
            List<string> list = new List<string>();

            foreach (string codeType in GetCodeTypeList())
            {
                string[] array = GetQrCode(ho_Image, codeType);
                if (array != null)
                {
                    list.AddRange(array);
                }
            }
            return list;
        }
        public static string GetCodeStr(HObject ho_Image)
        {
            List<string> codeList = GetCode(ho_Image);
            if (codeList.Count > 0)
            {
                return codeList[0];
            }
            return "";
        }
        private static string[] GetQrCode(HObject ho_Image, string symbolType)
        {
            try
            {
                DateTime date = DateTime.Now;
                // Local iconic variables  
                HObject ho_SymbolXLDs;

                // Local control variables  
                HTuple hv_ResultHandles = null;
                //HTuple hv_DataCodeHandle = null, hv_ResultHandles = null;
                HTuple hv_DecodedDataStrings = null;
                HTuple hv_Area = null;
                HTuple hv_Row1 = null;
                HTuple hv_Column = null;
                HTuple hv_PointOrder = null;
                HTuple hv_DataCodeHandle = null;
                // Initialize local and output iconic variables 
                HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
                //LogUtil.info("GetQrCode:结束HOperatorSet.GenEmptyObj,已耗时:" + (DateTime.Now - date).ToString());
                HOperatorSet.CreateDataCode2dModel(symbolType, "default_parameters", "maximum_recognition",
                    out hv_DataCodeHandle);

                string hv_model_path = GetCodeParamFilePath(symbolType);
                if (!hv_model_path.Equals(""))
                {
                    HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
                }
                else
                { 
                        HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "timeout", 300);
                        HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "mirrored", "no");
                        HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "model_type", 2);
                        HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "module_gap_max", "no"); 
                }
                //LogUtil.info("GetQrCode:结束HOperatorSet.CreateDataCode2dModel,已耗时:" + (DateTime.Now - date).ToString());
                ho_SymbolXLDs.Dispose();
                LogUtil.debug("GetQrCode:结束ho_SymbolXLDs.Dispose,已耗时:" + (DateTime.Now - date).ToString());
                HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, "stop_after_result_num",
                    3, out hv_ResultHandles, out hv_DecodedDataStrings);

                LogUtil.debug("GetQrCode:结束HOperatorSet.FindDataCode2d,已耗时:" + (DateTime.Now - date).ToString());
                HOperatorSet.AreaCenterXld(ho_SymbolXLDs, out hv_Area, out hv_Row1, out hv_Column,
                    out hv_PointOrder);

                //LogUtil.info("GetQrCode:结束HOperatorSet.AreaCenterXld,已耗时:" + (DateTime.Now - date).ToString());
                HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
                //LogUtil.info("GetQrCode:结束HOperatorSet.ClearDataCode2dModel,已耗时:" + (DateTime.Now - date).ToString());
                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 = "=" + x + "x" + y + "=" + hv_DecodedDataStrings.SArr[i];
                //            hv_DecodedDataStrings[i] = str;
                //        }
                //        catch (Exception ex)
                //        {
                //            LogUtil.error("处理条码出错:索引=" + i + "," + resultList.ToString());
                //        }
                //    }
                //}
                LogUtil.debug("GetQrCode:结束处理条码,已耗时:" + (DateTime.Now - date).ToString());
                return resultList;
            }
            catch (Exception ex)
            {
                return new string[] { };
            }
        }
        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 "";
            }
        }
    }
}