ScanWork.cs 11.2 KB
using Asa.FaceControl;
using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartScan
{
    public class ScanWork
    {
        private bool isRun = false;
        private bool isTouch = false;
        private readonly FacePictureBox picShow;
        private readonly FaceButton btnMatchedName;

        private List<CameraVisionLib.Model.BarcodeInfo> workCodeInfo;
        public Dictionary<string, string> workCodeKeyword;
        private string[] originalCodeText;
        private bool[] originalCodeIsUsed;

        public ScanWork()
        {
            picShow = (FacePictureBox)Common.frmMain.Controls["PicShow"];
            btnMatchedName = (FaceButton)Common.frmMain.Controls["BtnMatchedName"];
        }

        public void Open()
        {
            isRun = true;
            isTouch = false;
            Common.frmMain.Controls["BtnSet"].Visible = false;
            Common.frmMain.Controls["BtnRetrospect"].Visible = false;
            Common.frmMain.Controls["BtnAbout"].Visible = false;
            Common.frmMain.Controls["BtnTriggerIO"].Visible = true;
            LogNet.log.Info("Work Start");

            if (Common.config.EnabledCamera)
                Common.cameraVision?.Open();

            if (Common.config.TriggerOpenLight)  //触发时才打开光源
                Common.lightSource?.TurnOff();
            else
                Common.lightSource?.TurnOn();
        }

        public void Close()
        {
            isRun = false;
            Common.frmMain.Controls["BtnSet"].Visible = true;
            Common.frmMain.Controls["BtnRetrospect"].Visible = true;
            Common.frmMain.Controls["BtnAbout"].Visible = true;
            Common.frmMain.Controls["BtnTriggerIO"].Visible = false;
            LogNet.log.Info("Work Stop");

            if (Common.config.EnabledCamera)
                Common.cameraVision?.Close();

            Common.lightSource?.TurnOff();
        }

        public void TouchOff()
        {
            isTouch = false;
        }

        public void Scan()
        {
            if (!isRun) return;
            if (isTouch) return;
            isTouch = true;
            btnMatchedName.Invoke(delegate() {
                if (!BLL.Config.Backgrounder)
                    btnMatchedName.Visible = false;
                Common.extension.Clear();
            });            
            
            var t = Task.Run(() =>
            {
                try
                {
                    workCodeInfo = new();
                    workCodeKeyword = new(StringComparer.OrdinalIgnoreCase);
                    originalCodeText = null;
                    originalCodeIsUsed = null;
                    
                    Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialScanning"));//拍照识别...
                    if (!GetCodeInfo())
                    {
                        isTouch = false;
                        Common.frmMain.CloseWaittingDialog();
                        return;
                    }
                    Common.frmMain.Invoke(delegate ()
                    {
                        AddCodeCenter();
                    });
                    Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialTemplateMatching"));//模版匹配...
                    bool hasMatch = MatchingTemplate();
                    Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialProcessing"));//计算结果...
                    Common.frmMain.Invoke(delegate ()
                    {
                        SetKey(hasMatch);
                    });

                    isTouch = false;
                    
                    LogNet.log.Info("Work scan is done");
                }
                catch (Exception ex)
                {
                    LogNet.log.Error("Scan", ex);
                }
                finally {
                    isTouch = false;
                    Common.frmMain.CloseWaittingDialog();
                }
            });
            Common.frmMain.Invoke(delegate ()
            {
                Common.frmMain.ShowWaittingDialog();
            });
            
        }

            
        
        public void Scan(string[] code)
        {
            try
            {
                if (!isRun) return;
                if (isTouch) return;

                if (!BLL.Config.Backgrounder)
                    btnMatchedName.Visible = false;
                Common.extension.Clear();
                workCodeInfo = new();
                workCodeKeyword = new(StringComparer.OrdinalIgnoreCase);
                originalCodeText = code;
                originalCodeIsUsed = null;

                var hasMatch = MatchingTemplate();
                SetKey(hasMatch);

                isTouch = true;
                LogNet.log.Info("Work scan code is done");
            }
            catch (Exception ex)
            {
                LogNet.log.Error("Scan", ex);
            }
        }

        public object[] SaveCodeInfo()
        {
            object[] obj = null;
            if (workCodeInfo == null)
                return obj;

            obj = new object[workCodeInfo.Count];
            for (int i = 0; i < workCodeInfo.Count; i++)
            {
                Dictionary<string, string> dicCode = new()
                {
                    { "X", workCodeInfo[i].Center.X.ToString() },
                    { "Y", workCodeInfo[i].Center.Y.ToString() },
                    { "Text", workCodeInfo[i].Text }
                };
                obj[i] = dicCode;
            }
            return obj;
        }

        public WebCodeAll[] GetWebCodeAll()
        {
            WebCodeAll[] str = new WebCodeAll[workCodeInfo.Count];
            for (int i = 0; i < str.Length; i++)
            {
                str[i] = new()
                {
                    Text = workCodeInfo[i].Text,
                    CodeType = workCodeInfo[i].CodeType,
                    CenterX = workCodeInfo[i].Center.X,
                    CenterY = workCodeInfo[i].Center.Y,
                    Angle = workCodeInfo[i].Angle,
                    Width = workCodeInfo[i].Size.Width,
                    Height = workCodeInfo[i].Size.Height,
                    IsUsed = originalCodeIsUsed[i]
                };
            }
            return str;
        }

        public WebCodeText[] GetWebCodeText()
        {
            WebCodeText[] str = new WebCodeText[originalCodeText.Length];
            for (int i = 0; i < str.Length; i++)
            {
                str[i] = new()
                {
                    Text = originalCodeText[i],
                    IsUsed = originalCodeIsUsed[i]
                };
            }
            return str;
        }



        private bool GetCodeInfo()
        {
            LogNet.log.Info("Work GetCodeInfo");

            if (Common.config.EnabledCamera)
            {
                if (Common.config.TriggerOpenLight)
                {
                    Common.lightSource.TurnOn();
                    System.Threading.Thread.Sleep(2000);  //光源打开有一个由暗变亮的过程
                }
                workCodeInfo = Common.cameraVision.GetBarCode(out List<Bitmap> image);
                if (image.Count > 0 && !BLL.Config.Backgrounder) picShow.Image = image[0];
                if (image.Count > 0)
                    Common.mateEdit.CurrntBitmap = image[0];

                if (Common.config.TriggerOpenLight)
                    Common.lightSource.TurnOff();
                return true;
            }
            else
            {
                string filename = "";
                Common.frmMain.Invoke(delegate()
                {
                    OpenFileDialog dlg = new() { Filter = "图片文件|*.jpg;*.png;*.bmp;*.jpeg" };
                    DialogResult dr = dlg.ShowDialog();
                    filename = dlg.FileName;
                });
                if (string.IsNullOrEmpty(filename))
                    return false;

                Bitmap bmp = null;
                bmp = new(filename);
                workCodeInfo = Common.cameraVision.GetBarCode(bmp);
                Common.frmMain.Invoke(delegate ()
                {
                    if (!BLL.Config.Backgrounder)
                        picShow.Image = bmp;
                    Common.mateEdit.CurrntBitmap = bmp;
                });
                return true;
            }

            if (workCodeInfo.Count == 0 && !BLL.Config.Backgrounder)
            {
                string text = Language.Dialog(LanguageDialogKey.CODE_COUNT);
                new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog(Common.frmMain);
            }

            Application.DoEvents();
            LogNet.log.Info("条码个数:" + workCodeInfo.Count);
        }

        private void AddCodeCenter()
        {
            LogNet.log.Info("Work AddCodeCenter");
            if (!BLL.Config.Backgrounder)
                picShow.CodeCenterClear();
            if (workCodeInfo.Count == 0) return;
            PointF[] center = new PointF[workCodeInfo.Count];
            for (int i = 0; i < workCodeInfo.Count; i++)
                center[i] = workCodeInfo[i].Center;
            if (!BLL.Config.Backgrounder)
                picShow.AddCodeCenter(center);
            Application.DoEvents();
        }

        private bool MatchingTemplate()
        {
            LogNet.log.Info("Work MatchingTemplate");
            if (workCodeInfo.Count == 0) return false;
            originalCodeText = Common.cameraVision.GetBarCodeText(workCodeInfo);
            
            bool rtn = Common.mateEdit.MatchingTemplate(workCodeInfo, Common.config.DefaultMaterialName,false, out string mateName, out workCodeKeyword, out originalCodeIsUsed);
            BLL.MatchAnalysis.ShowResult();
            Common.frmMain.Invoke(delegate ()
            {
                if (rtn)
                {
                    LogNet.log.Info("模板匹配 " + mateName + ",关键字个数 " + workCodeKeyword.Count);
                    if (!BLL.Config.Backgrounder)
                    {
                        btnMatchedName.Visible = true;
                        btnMatchedName.Text = mateName;
                    }
                }
                else
                {
                    LogNet.log.Info("没有匹配到模板");
                    if (!BLL.Config.Backgrounder)
                    {
                        string text = Language.Dialog("MaterialTemplateNoMatch");
                        var fm = new FaceMessageBox("", text, MessageBoxButtons.OK);
                        fm.TopMost = true;
                        fm.ShowDialog(Common.frmMain);
                    }
                }
            });
            return rtn;
        }

        private void SetKey(bool hasMatch)
        {
            LogNet.log.Info("Work SetKey hasMatch:"+ hasMatch);
            Common.extension.SetKey(originalCodeText, workCodeKeyword, hasMatch,out _);
            if (workCodeKeyword.Count == 0) return;

            if (Common.config.PromptAfterPrinting && !BLL.Config.Backgrounder)
            {
                string text = Language.Dialog("SelectPrintContent");
                new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
            }
        }



    }
}