AdjustWhiteBalanceControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. using System.Threading;
  13. namespace PaintDotNet.ImageCollect.CameraComponent
  14. {
  15. public partial class AdjustWhiteBalanceControl : UserControl
  16. {
  17. private TUCamera.TUCamera m_camera;
  18. private CameraParamModel m_cameraParamModel;
  19. private bool m_use;
  20. private void InitializeLanguageText()
  21. {
  22. this.groupBox3.Text = PdnResources.GetString("Menu.Image.WhiteBalance.Text");
  23. this.button1.Text = PdnResources.GetString("Menu.Primarywhitebalance.Text");
  24. this.label12.Text = PdnResources.GetString("Menu.Blue.text");
  25. this.label11.Text = PdnResources.GetString("Menu.green.text");
  26. this.label10.Text = PdnResources.GetString("Menu.red.text");
  27. this.label9.Text = PdnResources.GetString("Menu.yellow.text");
  28. this.label8.Text = PdnResources.GetString("Menu.Magenta.text");
  29. this.label7.Text = PdnResources.GetString("Menu.aqua.text");
  30. this.ColourCheckBox.Text = PdnResources.GetString("Menu.Displaycolorvaluechannel.text");
  31. this.button3.Text = PdnResources.GetString("Menu.auto.text");
  32. this.button2.Text = PdnResources.GetString("Menu.Manual.text");
  33. }
  34. public AdjustWhiteBalanceControl(CameraParamModel model, bool use)
  35. {
  36. InitializeComponent();
  37. InitializeLanguageText();
  38. m_use = use;
  39. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  40. m_cameraParamModel = model;
  41. InitializeControlData();
  42. }
  43. public void ReLoad(CameraParamModel model, bool use)
  44. {
  45. m_cameraParamModel = model;
  46. m_use = use;
  47. InitializeControlData();
  48. }
  49. private void InitializeControlData()
  50. {
  51. if (m_camera.IsOpen())
  52. {
  53. // Get red channel range
  54. int minVal = 0;
  55. int maxVal = 0;
  56. m_camera.GetColorChannelRang(ColorChannel.RED, ref minVal, ref maxVal);
  57. redChannelTB.SetRange(minVal, maxVal);
  58. // Get green channel range
  59. m_camera.GetColorChannelRang(ColorChannel.GREEN, ref minVal, ref maxVal);
  60. greenChannelTB.SetRange(minVal, maxVal);
  61. // Get blue channel range
  62. m_camera.GetColorChannelRang(ColorChannel.BLUE, ref minVal, ref maxVal);
  63. blueChannelTB.SetRange(minVal, maxVal);
  64. }
  65. // 颜色值
  66. int redChannelValue = (int)m_cameraParamModel.parame.RedChannel;
  67. if (redChannelValue >= this.redChannelTB.Minimum && redChannelValue <= this.redChannelTB.Maximum)
  68. {
  69. this.redChannelTB.Value = redChannelValue;
  70. }
  71. int greenChannelValue = (int)m_cameraParamModel.parame.GreenChannel;
  72. if (greenChannelValue >= this.greenChannelTB.Minimum && greenChannelValue <= this.greenChannelTB.Maximum)
  73. {
  74. this.greenChannelTB.Value = greenChannelValue;
  75. }
  76. int blueChannelValue = (int)m_cameraParamModel.parame.BlueChannel;
  77. if (blueChannelValue >= this.blueChannelTB.Minimum && blueChannelValue <= this.blueChannelTB.Maximum)
  78. {
  79. this.blueChannelTB.Value = blueChannelValue;
  80. }
  81. // 白平衡
  82. if (m_cameraParamModel.parame.WhiteBalance == 1)
  83. {
  84. AutoWhiteBalance(true);
  85. }
  86. else
  87. {
  88. if (m_cameraParamModel.parame.FMExposure == 1)
  89. {
  90. m_camera.SetColorTemperatureByString("3200K");
  91. }
  92. else if (m_cameraParamModel.parame.FMExposure == 2)
  93. {
  94. m_camera.SetColorTemperatureByString("5500K");
  95. }
  96. AutoWhiteBalance(false);
  97. }
  98. // 显示颜色值通道
  99. if (m_cameraParamModel.parame.ShowColorPBoxFlag == 1)
  100. {
  101. this.ColourCheckBox.Checked = true;
  102. }
  103. else
  104. {
  105. this.ColourCheckBox.Checked = false;
  106. }
  107. }
  108. /// <summary>
  109. /// 是否自动白平衡 修改显示样式
  110. /// </summary>
  111. /// <param name="s"></param>
  112. private void AutoWhiteBalance(Boolean isWhiteBalance)
  113. {
  114. if (isWhiteBalance)
  115. {
  116. this.redChannelTB.Enabled = false;
  117. this.greenChannelTB.Enabled = false;
  118. this.blueChannelTB.Enabled = false;
  119. m_cameraParamModel.parame.WhiteBalance = 1;
  120. if (m_use)
  121. {
  122. // 自动白平衡
  123. m_camera.SetWhiteBalanceMode(WhiteBalanceMode.AUTO);
  124. }
  125. }
  126. else
  127. {
  128. m_cameraParamModel.parame.WhiteBalance = 0;
  129. if (m_use)
  130. {
  131. // 手动白平衡
  132. m_camera.SetWhiteBalanceMode(WhiteBalanceMode.MANUAL);
  133. }
  134. }
  135. try
  136. {
  137. this.Invoke(new Action(UpdateColorTemperature));
  138. }
  139. catch
  140. { }
  141. }
  142. private void UpdateColorTemperature()
  143. {
  144. button2.Enabled = true;
  145. button3.Enabled = true;
  146. button4.Enabled = true;
  147. button5.Enabled = true;
  148. uint cct = 0;
  149. double redChannel = 0;
  150. double greenChannel = 0;
  151. double blueChannel = 0;
  152. int rst = m_camera.GetColorTemperature(ref redChannel, ref greenChannel, ref blueChannel, ref cct);
  153. m_cameraParamModel.parame.RedChannel = redChannel;
  154. m_cameraParamModel.parame.GreenChannel = greenChannel;
  155. m_cameraParamModel.parame.BlueChannel = blueChannel;
  156. // 颜色值
  157. this.redChannelTB.Value = (int)m_cameraParamModel.parame.RedChannel;
  158. this.greenChannelTB.Value = (int)m_cameraParamModel.parame.GreenChannel;
  159. this.blueChannelTB.Value = (int)m_cameraParamModel.parame.BlueChannel;
  160. ////string colorTemperatureString = m_camera.GetColorTemperatureString(cct);
  161. this.redChannelTB.Enabled = false;
  162. this.greenChannelTB.Enabled = false;
  163. this.blueChannelTB.Enabled = false;
  164. if (m_cameraParamModel.parame.WhiteBalance == 1)
  165. {
  166. button3.Enabled = false;
  167. }
  168. else
  169. {
  170. switch (m_cameraParamModel.parame.FMExposure)
  171. {
  172. case 0:
  173. button2.Enabled = false;
  174. this.redChannelTB.Enabled = true;
  175. this.greenChannelTB.Enabled = true;
  176. this.blueChannelTB.Enabled = true;
  177. break;
  178. case 1:
  179. button4.Enabled = false;
  180. break;
  181. case 2:
  182. button5.Enabled = false;
  183. break;
  184. }
  185. }
  186. ColourCheckBox.Focus();
  187. }
  188. /// <summary>
  189. /// 手动白平衡
  190. /// </summary>
  191. /// <param name="sender"></param>
  192. /// <param name="e"></param>
  193. private void button2_Click(object sender, EventArgs e)
  194. {
  195. m_cameraParamModel.parame.FMExposure = 0;
  196. AutoWhiteBalance(false);
  197. }
  198. /// <summary>
  199. /// 自动白平衡按钮点击
  200. /// </summary>
  201. /// <param name="sender"></param>
  202. /// <param name="e"></param>
  203. private void button3_Click(object sender, EventArgs e)
  204. {
  205. AutoWhiteBalance(true);
  206. Thread.Sleep(1500);
  207. UpdateColorTemperature();
  208. }
  209. /// <summary>
  210. /// 自动一次按钮点击
  211. /// </summary>
  212. /// <param name="sender"></param>
  213. /// <param name="e"></param>
  214. private void button1_Click(object sender, EventArgs e)
  215. {
  216. AutoWhiteBalance(true);
  217. new Task(() =>
  218. {
  219. Thread.Sleep(500);
  220. m_cameraParamModel.parame.FMExposure = 0;
  221. AutoWhiteBalance(false);
  222. }).Start();
  223. }
  224. /// <summary>
  225. /// 色温3200K
  226. /// </summary>
  227. /// <param name="sender"></param>
  228. /// <param name="e"></param>
  229. private void button4_Click(object sender, EventArgs e)
  230. {
  231. m_cameraParamModel.parame.FMExposure = 1;
  232. m_camera.SetColorTemperatureByString("3200K");
  233. AutoWhiteBalance(false);
  234. }
  235. /// <summary>
  236. /// 色温5500K
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. private void button5_Click(object sender, EventArgs e)
  241. {
  242. m_cameraParamModel.parame.FMExposure = 2;
  243. m_camera.SetColorTemperatureByString("5500K");
  244. AutoWhiteBalance(false);
  245. }
  246. private void redChannelTB_ValueChanged(object sender, EventArgs e)
  247. {
  248. m_cameraParamModel.parame.RedChannel = ((TrackBar)sender).Value;
  249. // 设置到相机
  250. if (m_use)
  251. {
  252. m_camera.SetRedGain(m_cameraParamModel.parame.RedChannel); //usec
  253. }
  254. }
  255. private void greenChannelTB_ValueChanged(object sender, EventArgs e)
  256. {
  257. m_cameraParamModel.parame.GreenChannel = ((TrackBar)sender).Value;
  258. // 设置到相机
  259. if (m_use)
  260. {
  261. m_camera.SetGreeGain(m_cameraParamModel.parame.GreenChannel); //usec
  262. }
  263. }
  264. private void blueChannelTB_ValueChanged(object sender, EventArgs e)
  265. {
  266. m_cameraParamModel.parame.BlueChannel = ((TrackBar)sender).Value;
  267. // 设置到相机
  268. if (m_use)
  269. {
  270. m_camera.SetBlueGain(m_cameraParamModel.parame.BlueChannel); //usec
  271. }
  272. }
  273. private void xianshiColourCheckBox_CheckedChanged(object sender, EventArgs e)
  274. {
  275. CheckBox cb = (CheckBox)sender;
  276. this.yanghongsepictureBox.Visible = cb.Checked;
  277. this.yanghongsepictureBox.Visible = cb.Checked; ;
  278. this.lansepictureBox.Visible = cb.Checked;
  279. this.hongsepictureBox.Visible = cb.Checked;
  280. this.lansepictureBox.Visible = cb.Checked;
  281. this.huangsepictureBox.Visible = cb.Checked;
  282. this.lanlvpictureBox.Visible = cb.Checked;
  283. this.lvsepictureBox.Visible = cb.Checked;
  284. this.m_cameraParamModel.parame.ShowColorPBoxFlag = cb.Checked ? 1 : 0;
  285. }
  286. }
  287. }