AdjustExposureControl.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 TUCamera;
  12. namespace PaintDotNet.ImageCollect.CameraComponent
  13. {
  14. public partial class AdjustExposureControl : UserControl
  15. {
  16. private TUCamera.TUCamera m_camera;
  17. private CameraParamModel m_cameraParamModel;
  18. private System.Timers.Timer m_aeTimer;
  19. private bool m_use;
  20. private bool m_autoExposureOnce;
  21. private bool m_autoExposureComplete;
  22. private void InitializeLanguageText()
  23. {
  24. this.groupBox1.Text = PdnResources.GetString("Menu.timeofexposure.text");
  25. this.checkBoxAutoExposure.Text = PdnResources.GetString("Menu.auto-exposure.text");
  26. this.label6.Text = PdnResources.GetString("Menu.Exposurepercentage.text") + ":";
  27. this.label5.Text = PdnResources.GetString("Menu.timeofexposure.text") + ":";
  28. this.btnAutoExposureOnce.Text = PdnResources.GetString("Menu.Oneautomaticexposure.Text");
  29. }
  30. public AdjustExposureControl(CameraParamModel model, bool use)
  31. {
  32. m_use = use;
  33. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  34. m_cameraParamModel = model;
  35. m_aeTimer = new System.Timers.Timer(1000);
  36. m_aeTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerAutoExposure);
  37. m_aeTimer.AutoReset = true;
  38. m_aeTimer.SynchronizingObject = this;
  39. m_aeTimer.Start();
  40. InitializeComponent();
  41. InitializeLanguageText();
  42. InitializeControlData();
  43. }
  44. public void ReLoad(CameraParamModel model, bool use)
  45. {
  46. m_cameraParamModel = model;
  47. m_use = use;
  48. UpdateConfigMode();
  49. }
  50. private void InitializeControlData()
  51. {
  52. if (m_camera.IsOpen())
  53. {
  54. // 曝光时间
  55. double aeMinVal = 0;
  56. double aeMaxVal = 0;
  57. double aeDftVal = 0;
  58. m_camera.GetExposureTimeRange(ref aeMinVal, ref aeMaxVal, ref aeDftVal);
  59. SetExposureBarRange(aeMinVal, aeMaxVal);
  60. lblBaoGuangMinVal.Text = ((int)aeMinVal).ToString() + "μs";
  61. lblBaoGuangMaxVal.Text = UpdateExposureTime((UInt64)aeMaxVal);
  62. }
  63. }
  64. private void UpdateConfigMode()
  65. {
  66. // 曝光时间
  67. int value = (int)(m_cameraParamModel.parame.LNExposure);
  68. SetExposureBarValue(value);
  69. DisplayExposureText(value);
  70. // 曝光百分比
  71. int preExposure = m_cameraParamModel.parame.Light;
  72. preExposure = preExposure < 0 ? 0 : preExposure > 255 ? 255 : preExposure;
  73. lightTBar.Value = preExposure;
  74. LightBox.Text = preExposure.ToString();
  75. UpdateExposureUI(m_cameraParamModel.parame.ATExposure);
  76. }
  77. /// <summary>
  78. /// 1:自动 ,0:手动
  79. /// </summary>
  80. private void UpdateExposureUI(int autoExposure)
  81. {
  82. ExposureTBar.Enabled = autoExposure != 1;
  83. ExposureBox.Enabled = autoExposure != 1;
  84. checkBoxAutoExposure.Checked = autoExposure == 1;
  85. m_camera.SetExposureMode((ExposureMode)autoExposure);
  86. m_cameraParamModel.parame.ATExposure = autoExposure;
  87. }
  88. double _log = 1.1;
  89. int _min;
  90. int _max;
  91. private int GetExposureBarValue()
  92. {
  93. var value = ExposureTBar.Value;
  94. if (ExposureTBar.Value == ExposureTBar.Minimum) return _min;
  95. if (ExposureTBar.Value == ExposureTBar.Maximum) return _max;
  96. return (int)Math.Pow(_log, value);
  97. }
  98. private void SetExposureBarValue(double value)
  99. {
  100. var ival = (int)Math.Log(Math.Max(_min, value), _log);
  101. ExposureTBar.Value = Math.Min(ival, ExposureTBar.Maximum);
  102. }
  103. private void SetExposureBarRange(double min, double max)
  104. {
  105. _min = (int)min;
  106. ExposureTBar.Minimum = (int)Math.Log(min, _log);
  107. _max = (int)max;
  108. ExposureTBar.Maximum = (int)Math.Log(max, _log); ;
  109. }
  110. public void OnTimerAutoExposure(object source, System.Timers.ElapsedEventArgs e)
  111. {
  112. if (m_autoExposureOnce)
  113. {
  114. checkBoxAutoExposure.Checked = false;
  115. m_autoExposureOnce = false;
  116. UpdateExposureUI(0);
  117. // checkBoxAutoExposure_CheckedChanged(null, null);
  118. }
  119. else if (!checkBoxAutoExposure.Checked)
  120. {
  121. return;
  122. }
  123. double us = m_camera.GetExposureTime() * 1000;
  124. SetExposureBarValue(us);
  125. DisplayExposureText((int)us);
  126. }
  127. /// <summary>
  128. /// 曝光百分比滑块滑动事件
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. private void Light_ValueChanged(object sender, System.EventArgs e)
  133. {
  134. m_camera.Light = lightTBar.Value;
  135. m_cameraParamModel.parame.Light = lightTBar.Value;
  136. LightBox.Text = lightTBar.Value + "";
  137. }
  138. /// <summary>
  139. /// 曝光时间滑块滑动事件
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void Exposure_ValueChanged(object sender, System.EventArgs e)
  144. {
  145. int exposureTimeUSec = GetExposureBarValue();
  146. this.m_cameraParamModel.parame.LNExposure = exposureTimeUSec;
  147. DisplayExposureText(exposureTimeUSec);
  148. m_camera.SetExposureTime((ulong)(exposureTimeUSec));
  149. }
  150. private void checkBoxAutoExposure_CheckedChanged(object sender, EventArgs e)
  151. {
  152. int autoExposure = checkBoxAutoExposure.Checked ? 1 : 0;
  153. UpdateExposureUI(autoExposure);
  154. if (autoExposure == 0)
  155. {
  156. Light_ValueChanged(null, null);
  157. }
  158. else
  159. {
  160. m_autoExposureOnce = false;
  161. }
  162. }
  163. /// <summary>
  164. /// 一次自动曝光
  165. /// </summary>
  166. /// <param name="sender"></param>
  167. /// <param name="e"></param>
  168. private void btnAutoExposureOnce_Click(object sender, EventArgs e)
  169. {
  170. if (m_autoExposureComplete && checkBoxAutoExposure.Checked)
  171. {
  172. checkBoxAutoExposure.Checked = false;
  173. }
  174. else
  175. {
  176. if (checkBoxAutoExposure.Checked)
  177. {
  178. checkBoxAutoExposure.Checked = false;
  179. }
  180. m_autoExposureOnce = true;
  181. UpdateExposureUI(1);
  182. }
  183. }
  184. /// <summary>
  185. /// 曝光时间内容输入后校验
  186. /// </summary>
  187. /// <param name="sender"></param>
  188. /// <param name="e"></param>
  189. private void baoGuangTBox_KeyPress(object sender, KeyPressEventArgs e)
  190. {
  191. if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Back)) // 非数字键, 放弃该输入
  192. {
  193. e.Handled = true;
  194. return;
  195. }
  196. if (e.KeyChar == (char)Keys.Enter)
  197. {
  198. try
  199. {
  200. double value = Convert.ToDouble(ExposureBox.Text);
  201. switch (unitLabel.Text)
  202. {
  203. case "s":
  204. value = value * 1000 * 1000;
  205. break;
  206. case "ms":
  207. value = value * 1000;
  208. break;
  209. }
  210. SetExposureBarValue(value);
  211. Exposure_ValueChanged(null, null);
  212. }
  213. catch (Exception ex)
  214. {
  215. MessageBox.Show(ex.Message);
  216. }
  217. }
  218. }
  219. private void DisplayExposureText(int exposureTimeUSec)
  220. {
  221. decimal txtVale = 0;
  222. int sec = 0;
  223. int msec = 0;
  224. int usec = 0;
  225. m_camera.UpdateExposureTime(ref sec, ref msec, ref usec, (UInt64)exposureTimeUSec);
  226. if (sec > 0)
  227. {
  228. this.unitLabel.Text = "s";
  229. txtVale += sec;
  230. }
  231. if (msec > 0)
  232. {
  233. if (txtVale > 0)
  234. {
  235. txtVale += ((decimal)msec / 1000);
  236. this.unitLabel.Text = "s";
  237. usec = 0; // 微秒舍掉不显示了
  238. }
  239. else
  240. {
  241. this.unitLabel.Text = "ms";
  242. txtVale += msec;
  243. }
  244. }
  245. if (usec > 0)
  246. {
  247. if (txtVale > 0)
  248. {
  249. txtVale += ((decimal)usec / 1000);
  250. }
  251. else
  252. {
  253. this.unitLabel.Text = "μs";
  254. txtVale = usec;
  255. }
  256. }
  257. this.ExposureBox.Text = txtVale.ToString();
  258. }
  259. private string UpdateExposureTime(UInt64 exposureTime)
  260. {
  261. string str = "";
  262. int sec = 0;
  263. int msec = 0;
  264. int usec = 0;
  265. m_camera.UpdateExposureTime(ref sec, ref msec, ref usec, exposureTime);
  266. if (sec > 0)
  267. {
  268. str += sec + "s";
  269. }
  270. else if (msec > 0)
  271. {
  272. str += msec + "ms";
  273. }
  274. else if (usec > 0)
  275. {
  276. str += usec + "μs";
  277. }
  278. return str;
  279. }
  280. }
  281. }