Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

267 řádky
10 KiB

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