AdjustExposureControl.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using PaintDotNet.Base.SettingModel;
  2. using PaintDotNet.Camera;
  3. using System;
  4. using System.Threading;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet.ImageCollect
  7. {
  8. public partial class AdjustExposureControl : UserControl
  9. {
  10. private ICamera m_camera;
  11. private CameraParamModel.ParameterSets _settings;
  12. private void InitializeLanguageText()
  13. {
  14. this.cmpLight.Caption = PdnResources.GetString("Menu.Exposurepercentage.text");
  15. this.lblExp.Text = PdnResources.GetString("Menu.timeofexposure.text");
  16. this.groupBox1.Text = PdnResources.GetString("Menu.timeofexposure.text");
  17. this.ckbAutoExposure.Text = PdnResources.GetString("Menu.auto-exposure.text");
  18. this.btnAutoExposureOnce.Text = PdnResources.GetString("Menu.Oneautomaticexposure.Text");
  19. this.groupBox2.Text = PdnResources.GetString("Menu.Gainvalueadjustment.text");
  20. this.cmpGain.Caption = PdnResources.GetString("Menu.imagecapture.adjust.Gain.text");
  21. }
  22. public AdjustExposureControl(int mode = 3)
  23. {
  24. InitializeComponent();
  25. var height = 8;
  26. if ((mode & 1) > 0)
  27. {
  28. height += groupBox1.Height;
  29. }
  30. else
  31. {
  32. groupBox1.Hide();
  33. }
  34. if ((mode & 2) > 0)
  35. {
  36. height += groupBox2.Height;
  37. }
  38. else
  39. {
  40. groupBox2.Hide();
  41. }
  42. Height = height;
  43. }
  44. public void Initialize(CameraParamModel model)
  45. {
  46. InitializeLanguageText();
  47. m_camera = CameraManager.CurrentCamera;
  48. _settings = model.parame;
  49. if (m_camera.IsOpen())
  50. {
  51. ckbAutoExposure.Checked = m_camera.AutoExposure == 1;// _settings.ATExposure == 1;
  52. // 曝光时间
  53. var range = m_camera.GetExposureTimeRange();
  54. SetExposureBarRange(range.Min, range.Max);
  55. SetExposureBarValue(m_camera.ExposureTime);
  56. txbExposure.Text = m_camera.ExposureTime.ToString("f3");
  57. // 亮度
  58. range = m_camera.GetLightRange();
  59. cmpLight.Max = (int)range.Max;
  60. cmpLight.Min = (int)range.Min;
  61. cmpLight.OnValueChange += CmpLight_OnValueChange;
  62. cmpLight.Tag = range;
  63. cmpLight.Value = m_camera.Light;
  64. //增益
  65. range = m_camera.GetGlobalGainRange();
  66. cmpGain.Max = (int)range.Max;
  67. cmpGain.Min = (int)range.Min;
  68. cmpGain.OnValueChange += CmpGain_OnValueChange;
  69. _settings = model.parame;
  70. if (ckbAutoExposure.Checked)
  71. {
  72. SetAutoExposure();
  73. }
  74. else
  75. {
  76. SetFixedExposure();
  77. }
  78. }
  79. }
  80. private void CmpGain_OnValueChange(double obj)
  81. {
  82. m_camera.Gain = (int)obj;
  83. _settings.GlobalGain = (int)obj;
  84. }
  85. private void CmpLight_OnValueChange(double obj)
  86. {
  87. var value = (int)obj;
  88. _settings.GlobalGain = value;
  89. m_camera.Light = value;
  90. }
  91. public void OnTimerAutoExposure()
  92. {
  93. cmpGain.Value = m_camera.Gain;
  94. txbExposure.Text = m_camera.ExposureTime.ToString("f3");
  95. SetExposureBarValue(m_camera.ExposureTime);
  96. }
  97. private void ckbAutoExposure_Click(object sender, EventArgs e)
  98. {
  99. var isAuto = ckbAutoExposure.Checked;
  100. if (isAuto)
  101. SetAutoExposure();
  102. else
  103. SetFixedExposure();
  104. }
  105. private void btnAutoExposureOnce_Click(object sender, EventArgs e)
  106. {
  107. SetAutoExposure();
  108. ckbAutoExposure.Checked = false;
  109. new Thread(() =>
  110. {
  111. Thread.Sleep(1000);
  112. SetFixedExposure();
  113. this.Invoke(new Action(OnTimerAutoExposure));
  114. }).Start();
  115. }
  116. void SetAutoExposure()
  117. {
  118. _settings.ATExposure = 1;
  119. m_camera.AutoExposure = 1;
  120. }
  121. void SetFixedExposure()
  122. {
  123. _settings.ATExposure = 0;
  124. m_camera.AutoExposure = 0;
  125. }
  126. /// <summary>
  127. /// 刷新控件状态
  128. /// </summary>
  129. public void UpdateDisplay()
  130. {
  131. var isAuto = ckbAutoExposure.Checked;
  132. panel1.Enabled = !isAuto;
  133. cmpGain.Enabled = !isAuto;
  134. if (isAuto)//固定曝光
  135. {
  136. OnTimerAutoExposure();
  137. }
  138. }
  139. double _log = 1.1;
  140. int _min;
  141. int _max;
  142. private void SetExposureBarRange(double min, double max)
  143. {
  144. _min = (int)min;
  145. tkbExposure.Minimum = (int)Math.Log(min, _log);
  146. _max = (int)max;
  147. tkbExposure.Maximum = (int)Math.Log(max, _log); ;
  148. }
  149. private void tkbExposure_Scroll(object sender, EventArgs e)
  150. {
  151. double value = tkbExposure.Value;
  152. if (tkbExposure.Value == tkbExposure.Minimum) value = _min;
  153. if (tkbExposure.Value == tkbExposure.Maximum) value = _max;
  154. value = Math.Pow(_log, value);
  155. value = Math.Round(value, 3);
  156. txbExposure.Text = value.ToString();
  157. _settings.LNExposure = value;
  158. m_camera.ExposureTime = value;
  159. }
  160. private void SetExposureBarValue(double value)
  161. {
  162. if (value == 0) return;
  163. var ival = (int)Math.Log(Math.Max(_min, value), _log);
  164. tkbExposure.Value = Math.Max(tkbExposure.Minimum, Math.Min(ival, tkbExposure.Maximum));
  165. }
  166. /// <summary>
  167. /// 曝光时间内容输入后校验
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. private void baoGuangTBox_KeyPress(object sender, KeyPressEventArgs e)
  172. {
  173. if (e.KeyChar == (char)Keys.Delete)
  174. {
  175. if (txbExposure.Text.Contains("."))
  176. e.Handled = true;
  177. }
  178. else if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Back)) // 非数字键, 放弃该输入
  179. {
  180. e.Handled = true;
  181. return;
  182. }
  183. if (e.KeyChar == (char)Keys.Enter)
  184. {
  185. try
  186. {
  187. double value = Convert.ToDouble(txbExposure.Text);
  188. SetExposureBarValue(value);
  189. _settings.LNExposure = value;
  190. m_camera.ExposureTime = value;
  191. }
  192. catch (Exception ex)
  193. {
  194. MessageBox.Show(ex.Message);
  195. }
  196. }
  197. }
  198. }
  199. }