using CCDCount.DLL; using CCDCount.MODEL.ShuLiClass; using MvCameraControl; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Threading; using System.Windows.Forms; using CCDCount.MODEL.ConfigModel; namespace CCDCount.Forms { public partial class ShuLiForm : Form { #region 变量 List ImageData = new List(); LoadSplieImageClass loadSplieImageClass = new LoadSplieImageClass(); //MainThreadClass mainThread = new MainThreadClass(new ShuLiConfigClass()); MainThreadClass mainThread = new MainThreadClass(new ShuLiConfigClass(),new MODEL.ConfigModel.CameraConfig()); List RowsShowList = new List(); Stopwatch ShowImageStopwatch = new Stopwatch(); int Frame = 24; // 帧率 #endregion #region 测试 ShuLiClass shuLiClass = null; #endregion #region 构造函数 public ShuLiForm() { InitializeComponent(); SDKSystem.Initialize(); this.FormBorderStyle = FormBorderStyle.None; // 全屏显示 this.WindowState = FormWindowState.Maximized; // 窗口置顶 //this.TopMost = true; button4.Enabled = false; button5.Enabled = false; button8.Enabled = false; } #endregion #region 窗口事件 private void button1_Click(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog { Title = "请选择文件", // 对话框标题 Filter = "图片文件|*.bmp;*.jpg;*.png", // 文件类型过滤器 Multiselect = false // 是否允许多选 }; // 显示对话框并判断结果 if (openFileDialog.ShowDialog() == DialogResult.OK) { string selectedFilePath = openFileDialog.FileName; loadSplieImageClass.LoadImage(selectedFilePath); ImageData = loadSplieImageClass.SplieImage(); } } private void button2_Click(object sender, EventArgs e) { RowsShowList.Clear(); shuLiClass = new ShuLiClass(); shuLiClass.UpdateIdentifyImageWidth(ImageData[0].Count()); shuLiClass.InitChannel(); shuLiClass.WorkCompleted += Worker_MianThreadToFrom; Stopwatch sw = new Stopwatch(); List ActivesList = new List(); for (int i = 0; i < ImageData.Count; i++) { sw.Restart(); shuLiClass.ProcessImageSequence(ImageData[i], ImageData[i].Count()); sw.Stop(); Console.WriteLine(sw.Elapsed.ToString()); } if (shuLiClass.GetHistoryActiveNum() > 0) { button8.Enabled = true; } } private void button3_Click(object sender, EventArgs e) { if (shuLiClass!=null) { shuLiClass = null; RowsShowList.Clear(); } if (mainThread.StartMianThread()) { ShowImageStopwatch.Start(); mainThread.WorkerToFrom += Worker_MianThreadToFrom; button3.Enabled = false; button4.Enabled = true; button8.Enabled = false; } else { MessageBox.Show("请检查相机是否连接!"); } } private void button4_Click(object sender, EventArgs e) { mainThread.StopMianThread(); mainThread.WorkerToFrom -= Worker_MianThreadToFrom; button3.Enabled = true; button4.Enabled = false; if(mainThread.shuLiClass.GetHistoryActiveNum() > 0) button8.Enabled = true; } private void button5_Click(object sender, EventArgs e) { mainThread.SaveAllConfig(); } private void button8_Click(object sender, EventArgs e) { HistoryImageDataForm historyDataForm = new HistoryImageDataForm(); //图像测试模式下获取历史数据 if (shuLiClass != null) historyDataForm.historyActive = shuLiClass.GetHistoryActive(); //相机模式下获取历史数据 else if (mainThread.shuLiClass != null) historyDataForm.historyActive = mainThread.shuLiClass.GetHistoryActive(); historyDataForm.ShowDialog(); } private void ImageMoniShuLiForm_FormClosing(object sender, FormClosingEventArgs e) { SDKSystem.Finalize(); mainThread.StopMianThread(); Thread.Sleep(1000); //System.Windows.Forms.Application.Exit(); this.Dispose(); } #endregion #region 主界面回调事件 /// /// 通知主界面回调的事件 /// /// /// private void Worker_MianThreadToFrom(object sender, ActiveObjectEventArgsClass e) { // 事件处理逻辑 Console.WriteLine("结果已通知到主界面!"); e.Actives.ForEach(o => o.RowsData.ForEach(p => RowsShowList.Add(p))); e.Actives.ForEach(o => PrintMianPicrture(o)); } #endregion #region 私有方法 /// /// 渲染画面方法 /// /// private void PrintMianPicrture(ActiveObjectClass activeObject) { var timeChaZhi = (DateTime.Now - activeObject.EndCheckTime).TotalMilliseconds; UpdateLable2("总数量:" + activeObject.Num.ToString()); UpdateRichTextBox1(string.Format("第{0}识别结束到展示总用时:\n{1}毫秒", activeObject.Num, timeChaZhi)); UpdateRichTextBox1(string.Format("第{0}识别结束面积:{1}像素", activeObject.Num, activeObject.Area)); UpdateRichTextBox1(string.Format("第{0}识别结果所属通达数为{1}", activeObject.Num, activeObject.ChannelNO+1)); Bitmap bitmap = new Bitmap(activeObject.ImageWidth, 2048); Graphics g = Graphics.FromImage(bitmap); Pen redPen = new Pen(Color.Red, 1); List ShowList = RowsShowList.Where(o => o.RowsCol > activeObject.LastSeenLine - bitmap.Height).ToList(); RowsShowList.Where(o => o.RowsCol < activeObject.LastSeenLine - bitmap.Height).ToList().ForEach(o => RowsShowList.Remove(o)); if (ShowImageStopwatch.ElapsedMilliseconds < (1000 / Frame)) { return; } else { ShowImageStopwatch.Restart(); } ShowList.ForEach(o => g.DrawLine(redPen, new Point(o.StartCol, (int)(activeObject.LastSeenLine - o.RowsCol)), new Point(o.EndCol, (int)(activeObject.LastSeenLine - o.RowsCol)))); UpdatepictureBox1(bitmap.Clone() as Bitmap); } private void UpdateLable2(string text) { if (label2.InvokeRequired) { label2.Invoke(new Action(UpdateLable2), text); } else { label2.Text = text; } } private void UpdatepictureBox1(Bitmap image) { if (pictureBox1.InvokeRequired) { pictureBox1.Invoke(new Action(UpdatepictureBox1), image); } else { pictureBox1.Image = image; } } private void UpdateRichTextBox1(string text) { if (richTextBox1.InvokeRequired) { richTextBox1.Invoke(new Action(UpdateRichTextBox1), text); } else { richTextBox1.SelectionStart = richTextBox1.TextLength; richTextBox1.SelectionLength = 0; richTextBox1.AppendText(text + Environment.NewLine); richTextBox1.ScrollToCaret(); } } #endregion private void button1_Click_1(object sender, EventArgs e) { this.Close(); } } }