123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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<byte[]> ImageData = new List<byte[]>();
- LoadSplieImageClass loadSplieImageClass = new LoadSplieImageClass();
- //MainThreadClass mainThread = new MainThreadClass(new ShuLiConfigClass());
- MainThreadClass mainThread = new MainThreadClass(new ShuLiConfigClass(),new MODEL.ConfigModel.CameraConfig());
- List<RowStartEndCol> RowsShowList = new List<RowStartEndCol>();
- 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<ActiveObjectClass> ActivesList = new List<ActiveObjectClass>();
- 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 主界面回调事件
- /// <summary>
- /// 通知主界面回调的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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 私有方法
- /// <summary>
- /// 渲染画面方法
- /// </summary>
- /// <param name="activeObjects"></param>
- 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<RowStartEndCol> 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<string>(UpdateLable2), text);
- }
- else
- {
- label2.Text = text;
- }
- }
- private void UpdatepictureBox1(Bitmap image)
- {
- if (pictureBox1.InvokeRequired)
- {
- pictureBox1.Invoke(new Action<Bitmap>(UpdatepictureBox1), image);
- }
- else
- {
- pictureBox1.Image = image;
- }
- }
- private void UpdateRichTextBox1(string text)
- {
- if (richTextBox1.InvokeRequired)
- {
- richTextBox1.Invoke(new Action<string>(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();
- }
- }
- }
|