WPF之工作流程模块-WorkFlow

WPF之工作流程模块-WorkFlow

在这里插入图片描述

ImageInfo

using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF之AOI缺陷检测软件.Models
{
    /// <summary>
    /// 图像信息类
    /// </summary>
    public class ImageInfo
    {

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="imageDate"></param>
        /// <param name="imageId"></param>
        /// <param name="image"></param>
        public ImageInfo(string imageDate, string imageId, HObject image)
        {

            ImageDate = imageDate;
            ImageId = imageId;
            Image = image;
        }

        /// <summary>
        /// 图像产生的时间
        /// </summary>
        public string ImageDate { get; set; }

        /// <summary>
        /// 图像
        /// </summary>

        public HObject Image { get; set; }

        /// <summary>
        /// 图像ID
        /// </summary>

        public string ImageId { get; set; }
    }
}

WorkFlowService

using HalconControl;
using HalconDotNet;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using WPF之AOI缺陷检测软件.Algorithm;
using WPF之AOI缺陷检测软件.Communicate;
using WPF之AOI缺陷检测软件.Models;

namespace WPF之AOI缺陷检测软件.WorkFlow
{
    public class WorkFlowService
    {


        // (1)存放图像的队列
        // 自动实现了锁等机制,安全,避免死锁
        private ConcurrentQueue<ImageInfo> ImageList1 = new ConcurrentQueue<ImageInfo>();

        // (2)添加图像到图像队列的函数
        public void AddImageInfo1(ImageInfo imageInfo)
        {
            ImageList1.Enqueue(imageInfo);
        }


        //1. 私有静态变量(在第四步供外界使用),创建类的实例
        //2. 私有构造函数,确保外部无法直接实例化(确保是单个实例)
        //3. 确定供外界调用的代码资源
        //4. 公开静态属性,供外界使用(把第一步类的实例,开放出去)
        //5. 外界使用


        //1. 私有静态变量(在第四步供外界使用),创建类的实例

        private static WorkFlowService instance = new WorkFlowService();

        //2. 私有构造函数,确保外部无法直接实例化(确保是单个实例)
        private WorkFlowService()
        {

        }
        //4. 公开静态属性,供外界使用(把第一步类的实例,开放出去)
        public static WorkFlowService Instance
        { get { return instance; } }



        //3. 确定供外界调用的代码资源

        #region 启动工作线程
        public void StartWorkFlow()
        {
            Task.Run(Working);
        }
        #endregion

        /// <summary>
        /// 关闭线程
        /// </summary>
        public void StopWorkFlow()
        {
            IsWorking = false;
        }

        // 是否工作的标志位
        private bool IsWorking = false;


        /// <summary>
        /// 工作线程函数
        /// </summary>
        public void Working()
        {
            IsWorking = true;

            while (IsWorking)
            {

                // 1.判别是否在在线状态
                if (!GlobalParamters.IsOnline)
                {
                    // 停止循环
                    IsWorking = false;
                    // 跳出循环
                    break;
                }

                // 2.取图
                // 如果这个图像队列里,有图像,就取处理。如果有图像取出来,则继续往下走
                //(4) 利用TryDequeue取出图像
                if (!ImageList1.TryDequeue(out ImageInfo info))
                {
                    // 防止cpu过载
                    Thread.Sleep(5);
                    // 如果imagelist没有图像,跳过。
                    continue;
                }

                try
                {
                    // 开始时间
                    DateTime startTime = DateTime.Now;

                    // 3.调用算法模块

                    HObject defectRegion = AlgorithmService.Instance.DetectZW(info.Image, GlobalParamters.CurrAlgoParamter);


                    // 4.把检测结果发送上位机或PLC

                    if (defectRegion != null)
                    {
                        CommunicateService.Instance.Send("NG");

                        GlobalParamters.NGNum++;

                        // 5.NG图像显示
                        Application.Current.Dispatcher.BeginInvoke((Action)(() => {
                            // 1.获取到_mainWindow窗口
                            var _mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
                            // 2.获取窗口上的Halcon的显示控件,显示图像
                            _mainWindow.hWindow_Final_NGImg.HobjectToHimage(info.Image);
                            // 3.显示缺陷
                            _mainWindow.hWindow_Final_NGImg.DispObj(defectRegion, "red");

                        }));




                        // 6.图像存储,存NG图像

                        // 多线程
                        Task.Run(new Action(
                            () =>
                            {
                                if (!Directory.Exists(Path.Combine("D:\\AOI存图", "NG", info.ImageDate)))
                                {
                                    Directory.CreateDirectory(Path.Combine("D:\\AOI存图", "NG", info.ImageDate));
                                }

                                HOperatorSet.WriteImage(info.Image, "bmp", 0, Path.Combine("D:\\AOI存图", "NG", info.ImageDate, info.ImageId));

                                HOperatorSet.CountChannels(info.Image, out HTuple channels);

                                HObject showImg = new HObject();
                                if (channels.D != 1)
                                {
                                    HOperatorSet.Rgb1ToGray(info.Image, out showImg);
                                }

                                HOperatorSet.PaintRegion(defectRegion, showImg, out HObject ResImg, 125, "margin");

                                HOperatorSet.WriteImage(ResImg, "jpeg", 0, Path.Combine("D:\\AOI存图", "NG", info.ImageDate, info.ImageId));

                            }

                            ));



                    }
                    else
                    {
                        GlobalParamters.OKNum++;
                        CommunicateService.Instance.Send("OK");

                        // 多线程
                        Task.Run(new Action(
                            () =>
                            {
                                if (!Directory.Exists(Path.Combine("D:\\AOI存图", "OK", info.ImageDate)))
                                {
                                    Directory.CreateDirectory(Path.Combine("D:\\AOI存图", "OK", info.ImageDate));
                                }

                                HOperatorSet.WriteImage(info.Image, "bmp", 0, Path.Combine("D:\\AOI存图", "OK", info.ImageDate, info.ImageId));


                            }

                            ));

                    }

                    GlobalParamters.AllNum = GlobalParamters.NGNum + GlobalParamters.OKNum;

                    // 刷新界面更新

                    Application.Current.Dispatcher.BeginInvoke((Action)(() => {
                        // 1.获取到_mainWindow窗口
                        var _mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
                     

                    }));

                    TimeSpan DetectCT = DateTime.Now - startTime;

                }
                catch
                {

                }

                // 避免CPU过载:睡眠2ms
                Thread.Sleep(2);
            }


        }




    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值