FrameCameraControl1.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using PaintDotNet.Base.SettingModel;
  11. using System.Collections;
  12. using PaintDotNet.ImageCollect;
  13. using TUCamera;
  14. using System.IO;
  15. using System.Runtime.Serialization.Formatters.Binary;
  16. using System.Threading;
  17. namespace PaintDotNet.Setting.LabelComponent
  18. {
  19. /// <summary>
  20. /// 相机设置,边框界面
  21. /// </summary>
  22. public class FrameCameraControl1 : UserControl
  23. {
  24. #region 控件
  25. #endregion
  26. /// <summary>
  27. /// 相机参数的Model
  28. /// </summary>
  29. private CameraParamModel m_cameraParamModel;
  30. /// <summary>
  31. /// 相机设置的dialog
  32. /// </summary>
  33. private Point m_startP; //画框的起始点
  34. private bool m_blnDraw = false;
  35. private Rectangle m_rect ;//初始设置一个拖拽的显示框
  36. SolidBrush m_brush = new SolidBrush(Color.Black);
  37. int m_handle = 0;
  38. int m_targetX = 0; // 起始点X坐标 对应分辨率
  39. int m_targetY = 0; // 起始点Y坐标 对应分辨率
  40. int m_targetWidth = 320; //显示框的宽度 对应分辨率
  41. int m_targetHeight = 200; //显示框的高度 对应分辨率
  42. int m_tempX = 0;
  43. int m_tempY = 0;
  44. int m_tempW = 0;
  45. int m_tempH = 0;
  46. private TUCameraIFrameProcess m_process = null;
  47. private Size m_resolution; // 分辨率
  48. private TUCamera.TUCamera m_camera;
  49. private bool m_use = false; // 设置是否立刻生效,设置到相机
  50. private OpenCvSharp.Mat m_mat;
  51. string[] m_binArray = new string[] { "1x1 Bin", "2x2 Bin", "3x3 Bin", "4x4 Bin" };
  52. private GroupBox groupBox2;
  53. private ComboBox cmbResolution;
  54. private bool m_isWaiting;
  55. private int m_selectedBin;
  56. public FrameCameraControl1(CameraParamModel model , bool use)
  57. {
  58. this.m_use = use;
  59. this.m_cameraParamModel = model;
  60. this.m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  61. InitializeComponent();
  62. InitializeLanguageText();
  63. InitializeControlData();
  64. }
  65. public void ResetCameraParamModel(CameraParamModel model)
  66. {
  67. m_cameraParamModel = model;
  68. InitializeControlData();
  69. //InitializeData();
  70. }
  71. /// <summary>
  72. /// 设置下拉等数据源
  73. /// </summary>
  74. private void InitializeControlData()
  75. {
  76. int index = m_cameraParamModel.parame.BinningSumation -1;
  77. if (index > m_binArray.Length || index < 0)
  78. {
  79. index = 0;
  80. }
  81. cmbResolution.Items.Clear();
  82. List<string> resolutions = this.m_camera.GetResolutionRange();
  83. for(int i=0; i< resolutions.Count; i++)
  84. {
  85. cmbResolution.Items.Add(resolutions[i]);
  86. }
  87. if (resolutions.Count > 0)
  88. {
  89. cmbResolution.SelectedIndex = m_cameraParamModel.parame.Resolution;
  90. }
  91. }
  92. /// <summary>
  93. /// 绑定样式数据
  94. /// </summary>
  95. private void InitializeData()
  96. {
  97. m_targetX = this.m_cameraParamModel.parame.previewRectX;
  98. m_targetY = this.m_cameraParamModel.parame.previewRectY;
  99. m_targetWidth = this.m_cameraParamModel.parame.previewRectW;
  100. m_targetHeight = this.m_cameraParamModel.parame.previewRectH;
  101. SetROI();
  102. m_rect = new Rectangle();
  103. //drawingImg();
  104. //if (m_resolution.Width > 0 && m_resolution.Height > 0)
  105. //{
  106. // this.pictureBox1.Height = (this.pictureBox1.Width * m_resolution.Height) / m_resolution.Width;
  107. // m_rect.X = this.m_cameraParamModel.parame.previewRectX * this.pictureBox1.Width / m_resolution.Width;
  108. // m_rect.Y = this.m_cameraParamModel.parame.previewRectY * this.pictureBox1.Height / m_resolution.Height;
  109. // m_rect.Width = this.m_cameraParamModel.parame.previewRectW * this.pictureBox1.Width / m_resolution.Width;
  110. // m_rect.Height = this.m_cameraParamModel.parame.previewRectH * this.pictureBox1.Height / m_resolution.Height;
  111. // Console.WriteLine(m_rect);
  112. //}
  113. //else
  114. //{
  115. // m_rect.X = 0;
  116. // m_rect.Y = 0;
  117. // m_rect.Width = this.pictureBox1.Width;
  118. // m_rect.Height = this.pictureBox1.Height;
  119. //}
  120. }
  121. private void InitializeLanguageText()
  122. {
  123. this.groupBox2.Text = PdnResources.GetString("Menu.Resolution.text");
  124. }
  125. private void InitializeComponent()
  126. {
  127. this.groupBox2 = new System.Windows.Forms.GroupBox();
  128. this.cmbResolution = new System.Windows.Forms.ComboBox();
  129. this.groupBox2.SuspendLayout();
  130. this.SuspendLayout();
  131. //
  132. // groupBox2
  133. //
  134. this.groupBox2.Controls.Add(this.cmbResolution);
  135. this.groupBox2.Location = new System.Drawing.Point(12, 12);
  136. this.groupBox2.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  137. this.groupBox2.Name = "groupBox2";
  138. this.groupBox2.Padding = new System.Windows.Forms.Padding(2, 2, 2, 2);
  139. this.groupBox2.Size = new System.Drawing.Size(464, 66);
  140. this.groupBox2.TabIndex = 3;
  141. this.groupBox2.TabStop = false;
  142. this.groupBox2.Text = "分辨率";
  143. //
  144. // cmbResolution
  145. //
  146. this.cmbResolution.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  147. this.cmbResolution.FormattingEnabled = true;
  148. this.cmbResolution.Location = new System.Drawing.Point(19, 26);
  149. this.cmbResolution.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
  150. this.cmbResolution.Name = "cmbResolution";
  151. this.cmbResolution.Size = new System.Drawing.Size(416, 20);
  152. this.cmbResolution.TabIndex = 0;
  153. this.cmbResolution.SelectedIndexChanged += new System.EventHandler(this.cmbResolution_SelectedIndexChanged);
  154. //
  155. // FrameCameraControl1
  156. //
  157. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  158. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  159. this.Controls.Add(this.groupBox2);
  160. this.Name = "FrameCameraControl1";
  161. this.Size = new System.Drawing.Size(488, 93);
  162. this.groupBox2.ResumeLayout(false);
  163. this.ResumeLayout(false);
  164. }
  165. /// <summary>
  166. /// 绘制
  167. /// </summary>
  168. /// <param name="pBuf"></param>
  169. /// <param name="obj"></param>
  170. public void CallbackDraw(byte[] pBuf, object obj, int channel)
  171. {
  172. lock (obj)
  173. {
  174. m_isWaiting = false;
  175. Console.WriteLine("接收全图buf");
  176. m_camera.m_drawAllHandler -= new DrawAllHandler(CallbackDraw);
  177. OpenCvSharp.Mat mat;
  178. if (channel == 1)
  179. {
  180. mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC1, pBuf);
  181. }
  182. else
  183. {
  184. mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC3, pBuf);
  185. }
  186. OpenCvSharp.Cv2.Flip(mat, mat, 0);
  187. stopDrawing();
  188. if (mat != null)
  189. {
  190. mat.Dispose();
  191. }
  192. if (m_mat != null)
  193. {
  194. m_mat.Dispose();
  195. }
  196. }
  197. }
  198. private void stopDrawing()
  199. {
  200. //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
  201. Action invokeAction = new Action(stopDrawing);
  202. //判断操作控件的线程是否创建控件的线程
  203. //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
  204. if (this.InvokeRequired)
  205. {
  206. //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
  207. this.Invoke(invokeAction);
  208. }
  209. else
  210. {
  211. m_camera.StopDrawing(m_process);
  212. m_camera.StopWaitForFrame();
  213. ////窗体线程,即主线程
  214. //shuaxinButton.Enabled = true;
  215. }
  216. }
  217. private void SetROI()
  218. {
  219. if (m_use)
  220. {
  221. m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
  222. this.m_cameraParamModel.parame.previewRectX = m_targetX;
  223. this.m_cameraParamModel.parame.previewRectY = m_targetY;
  224. this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  225. this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  226. }
  227. }
  228. private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e)
  229. {
  230. m_cameraParamModel.parame.Resolution = cmbResolution.SelectedIndex;
  231. int resolutionId = m_camera.GetResolutionId();
  232. if (resolutionId != m_cameraParamModel.parame.Resolution)
  233. {
  234. // 分辨率
  235. m_camera.SetResolution(m_cameraParamModel.parame.Resolution);
  236. }
  237. m_camera.GetROI(ref m_cameraParamModel.parame.previewRectX, ref m_cameraParamModel.parame.previewRectY, ref m_cameraParamModel.parame.previewRectW, ref m_cameraParamModel.parame.previewRectH);
  238. InitializeData();
  239. //this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString();
  240. //this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString();
  241. //this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString();
  242. //this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString();
  243. }
  244. }
  245. }