VisionPro二开之工作流程模块-WorkFlow
![]()
using AOI外观检测.Algorithm;using AOI外观检测.Models;using HalconDotNet;using LiveCharts;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.Forms;namespace AOI外观检测.WorkFlow{public class WorkFlowService{// 定义图像队列public ConcurrentQueue<ImageInfo>ImageList1=new ConcurrentQueue<ImageInfo>();// 添加图像到图像队列publicvoidAddImageInfo1(ImageInfo info){ImageList1.Enqueue(info);//($"相机1新增检测任务,任务ID = {info.TaskID}");}privatestaticWorkFlowService instance=newWorkFlowService();privateWorkFlowService(){}publicstaticWorkFlowService Instance{get{returninstance;}}//3. 确定供外界调用的代码资源// 工作标志位private bool IsWorking=false;//关闭工作线程publicvoidStopWorkFlow(){IsWorking=false;}//启动工作线程publicvoidStartWorkFlow(){Task.Run(Working);}//工作线程publicvoidWorking(){// 工作位为trueIsWorking=true;while(IsWorking){// 一 判别是否处于在线状态if(!GlobalParamters.IsOnline){// 停止以后循环IsWorking=false;// 跳出当前循环break;}// 生产流程// 二 取图(如果取不出图,则跳过)if(!ImageList1.TryDequeue(out ImageInfo info)){Thread.Sleep(5);continue;}try{DateTime dateTime_Start=DateTime.Now;HObject defectRegion=AlgorithmService.Instance.DetectZW(info.Image,GlobalParamters.CurrAlgoParam);// 获取主窗体var mainForm=Application.OpenForms.OfType<MainForm>().FirstOrDefault();// 四 将检测结果发送上位机if(defectRegion!=null){CommunicateService.Instance.Send("NG");GlobalParamters.NGNum++;TimeSpan CT_Elapse=DateTime.Now-dateTime_Start;Log.Info($"本次算法处理-CT:{CT_Elapse.TotalMilliseconds}ms");// 五 显示NG图像// 使用扩展方法更新UImainForm.Invoke(newAction(()=>{// 显示图像mainForm.hWindow_Final2.HobjectToHimage(info.Image);// 显示缺陷mainForm.hWindow_Final2.DispObj(defectRegion,"red");Log.Info($"图像ID:{info.ImageID} 已发送:NG\n");}));//六.多线程保存NG图Task.Run(newAction(()=>{if(!Directory.Exists(Path.Combine("D:\\AOI存图","NG",info.ImageDate))){Directory.CreateDirectory(Path.Combine("D:\\AOI存图","NG",info.ImageDate));}// NG原图HOperatorSet.WriteImage(info.Image,"bmp",0,Path.Combine("D:\\AOI存图","NG",info.ImageDate,info.ImageID));//计算通道HOperatorSet.CountChannels(info.Image,out HTuple channels);HObject showImg=newHObject();if(channels.D!=1){//转为灰度图HOperatorSet.Rgb1ToGray(info.Image,out showImg);}else{showImg=info.Image;}// 对缺陷矩形进行绘制HOperatorSet.PaintRegion(defectRegion,showImg,out HObject ResImg,255,"margin");// NG效果图保存HOperatorSet.WriteImage(ResImg,"jpeg",0,Path.Combine("D:\\AOI存图","NG",info.ImageDate,info.ImageID));}));}else{CommunicateService.Instance.Send("OK");GlobalParamters.OKNum++;TimeSpan CT_Elapse=DateTime.Now-dateTime_Start;Log.Info($"本次算法处理-CT:{CT_Elapse.TotalMilliseconds}ms");// 使用扩展方法更新UImainForm.Invoke(newAction(()=>{Log.Info($"图像ID:{info.ImageID}已发送:OK\n");}));// 多线程保存OK图Task.Run(newAction(()=>{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.OKNum+GlobalParamters.NGNum;// 更新UI统计数据mainForm.Invoke(newAction(()=>{mainForm.label1.Text=GlobalParamters.AllNum.ToString();mainForm.label3.Text=GlobalParamters.NGNum.ToString();mainForm.label5.Text=GlobalParamters.OKNum.ToString();mainForm.pieChart1.Series[0].Values=new ChartValues<double>{GlobalParamters.OKNum};mainForm.pieChart1.Series[1].Values=new ChartValues<double>{GlobalParamters.NGNum};}));// 防止cpu过载Thread.Sleep(2);}catch(Exception ex){}}}}}