版博士V2.0程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ImageShowFrm.cs 7.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using HalconDotNet;
  2. using ImageToolKits;
  3. using MaiMuAOI.SysCtrl;
  4. using MaiMuControl.Device;
  5. using MaiMuControl.Device.AxisDev;
  6. using MaiMuControl.Device.CamDev;
  7. using OpenCvSharp.Flann;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Drawing.Imaging;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Web;
  19. using System.Windows.Forms;
  20. namespace MaiMuAOI.SysUI.DefectPicShow
  21. {
  22. public partial class ImageShowFrm : Form
  23. {
  24. private Control.ControlCollection picBoxList;
  25. private int picIndex;
  26. private int tagIndex;
  27. private double Xmm, Ymm;
  28. private Bitmap currBmp;
  29. private bool IsMove;
  30. public ImageShowFrm(Control.ControlCollection picBoxColl, int index, bool isDev = false)
  31. {
  32. InitializeComponent();
  33. UIStyle.SetUIStyle(this);
  34. this.uiTitel1.FatherForm = this;
  35. this.picBoxList = picBoxColl;
  36. this.picIndex = index;
  37. IsMove = isDev;
  38. }
  39. private void ImageShowFrm_Shown(object sender, EventArgs e)
  40. {
  41. showPic(this.picIndex);
  42. }
  43. private void tsbtnPre_Click(object sender, EventArgs e)
  44. {
  45. showPic(--this.picIndex);
  46. }
  47. private void tsbtnNext_Click(object sender, EventArgs e)
  48. {
  49. showPic(++this.picIndex);
  50. }
  51. private void ImageShowFrm_Load(object sender, EventArgs e)
  52. {
  53. if (!IsMove)
  54. {
  55. imageBox2.Visible = false;
  56. imageBox1.Top = 0;
  57. imageBox1.Left = this.panel1.Left;
  58. imageBox1.Width = this.panel1.Width;
  59. imageBox1.Height = this.panel1.Height;
  60. }
  61. else
  62. {
  63. imageBox2.Visible = true;
  64. imageBox1.Top = 0;
  65. imageBox1.Left = this.panel1.Left;
  66. imageBox1.Width = this.panel1.Width / 2 - 5;
  67. imageBox1.Height = this.panel1.Height;
  68. imageBox2.Top = 0;
  69. imageBox2.Left = this.imageBox1.Width + 10;
  70. imageBox2.Width = this.panel1.Width / 2 - 5;
  71. imageBox2.Height = this.panel1.Height;
  72. }
  73. PictureBox picBox = picBoxList[this.picIndex] as PictureBox;
  74. this.ActiveControl = this.imageBox1; // 设置焦点
  75. this.logsts.Text = "";
  76. this.Refresh();
  77. }
  78. private void FrmPhoto_KeyDown(object sender, KeyEventArgs e)
  79. {
  80. switch (e.KeyCode)
  81. {
  82. case Keys.Up:
  83. case Keys.Left:
  84. if (!this.tsbtnPre.Enabled) return;
  85. tsbtnPre_Click(null, null);
  86. break;
  87. case Keys.Down:
  88. case Keys.Right:
  89. if (!this.tsbtnNext.Enabled) return;
  90. tsbtnNext_Click(null, null);
  91. break;
  92. }
  93. }
  94. private void tsbtnExit_Click(object sender, EventArgs e)
  95. {
  96. this.Close();
  97. }
  98. private void showPic(int index)
  99. {
  100. try
  101. {
  102. PictureBox picBox = picBoxList[index] as PictureBox;
  103. string[] tags = picBox.Tag.ToString().Split(',');
  104. this.tagIndex = Convert.ToInt32(tags[0]);
  105. this.Xmm = Convert.ToDouble(tags[1]);
  106. this.Ymm = Convert.ToDouble(tags[2]);
  107. //tsStateMsg.Text = $"索引:{tagIndex}, X:{Xmm}mm, Y:{Ymm}mm";
  108. tsStateStep.Text = $"第{index + 1}/{picBoxList.Count}张";
  109. currBmp = (Bitmap)picBox.Image;
  110. OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(currBmp);//用bitmap转换为mat
  111. this.imageBox1.RefreshWindow(mat);
  112. //
  113. this.tsbtnPre.Enabled = (index > 0);
  114. this.tsbtnNext.Enabled = (index < picBoxList.Count - 1);
  115. if (IsMove)
  116. moveAxis();
  117. }
  118. catch (Exception ex)
  119. {
  120. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  121. }
  122. }
  123. private void ImageShowFrm_Resize(object sender, EventArgs e)
  124. {
  125. if (!IsMove)
  126. {
  127. imageBox2.Visible = false;
  128. imageBox1.Top = 0;
  129. imageBox1.Left = this.panel1.Left;
  130. imageBox1.Width = this.panel1.Width;
  131. imageBox1.Height = this.panel1.Height;
  132. }
  133. else
  134. {
  135. imageBox2.Visible = true;
  136. imageBox1.Top = 0;
  137. imageBox1.Left = this.panel1.Left;
  138. imageBox1.Width = this.panel1.Width / 2 - 5;
  139. imageBox1.Height = this.panel1.Height;
  140. imageBox2.Top = 0;
  141. imageBox2.Left = this.imageBox1.Width + 10;
  142. imageBox2.Width = this.panel1.Width / 2 - 5;
  143. imageBox2.Height = this.panel1.Height;
  144. }
  145. this.Refresh();
  146. }
  147. //移动电机拍照
  148. private void moveAxis()
  149. {
  150. this.tsbtnPre.Enabled = this.tsbtnNext.Enabled = false;
  151. this.logsts.Text = " 正在移动相机...";
  152. // 开启光源
  153. SysMgr.Instance.LightDev.OpenLight((int)LightChannelEnum.CH1);
  154. // 移动电机
  155. if((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60)<0 )||
  156. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis1, 60) < 0) ||
  157. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0))
  158. {
  159. MessageBox.Show("轴状态为不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  160. return;
  161. }
  162. var list = SysMgr.Instance.DefectLib.viewTag(tagIndex, Xmm, Ymm, currBmp.Width, currBmp.Height);
  163. double val = SysMgr.Instance.DefaultSpeed;
  164. int st = SysMgr.Instance.MotionST;
  165. VelocityCurveParams Velocity1 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis0));
  166. VelocityCurveParams Velocity2 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis1));
  167. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis0, Velocity1, list[0]);
  168. SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis1, Velocity2, list[1]);
  169. this.logsts.Text = "";
  170. this.tsbtnPre.Enabled = (picIndex > 0);
  171. this.tsbtnNext.Enabled = (picIndex < picBoxList.Count - 1);
  172. if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60) < 0) ||
  173. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis1, 60) < 0) ||
  174. (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0))
  175. {
  176. MessageBox.Show("到位失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  177. return;
  178. }
  179. Thread.Sleep(300);
  180. //拍照
  181. this.logsts.Text = " 拍照...";
  182. Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 6000);
  183. if (acq.GrabStatus != "GrabPass")
  184. {
  185. MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
  186. return;
  187. }
  188. imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
  189. }
  190. }
  191. }