CameraSettingDialog.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Annotation.relationModel;
  3. using PaintDotNet.Base.SettingModel;
  4. using PaintDotNet.Base.CommTool;
  5. using PaintDotNet.ImageCollect;
  6. using PaintDotNet.Setting.LabelComponent;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Windows.Forms;
  12. using PaintDotNet.ImageCollect;
  13. using PaintDotNet.Camera;
  14. namespace PaintDotNet.Setting
  15. {
  16. /// <summary>
  17. /// 设置->相机设置
  18. /// </summary>
  19. internal class CameraSettingDialog : FloatingToolForm
  20. {
  21. /// <summary>
  22. /// 标注一二级关系
  23. /// </summary>
  24. private static List<CameraRelationModel> list = new List<CameraRelationModel>();
  25. /// <summary>
  26. /// 当前选中的参数
  27. /// </summary>
  28. private CameraParamType cameraParamType;
  29. /// <summary>
  30. /// 相机参数
  31. /// </summary>
  32. public CameraParamModel m_cameraParamModel = Startup.instance.cameraParamModel;
  33. /// <summary>
  34. /// 主配置文件的model
  35. /// </summary>
  36. private ConfigModel configModel = Startup.instance.configModel;
  37. /// <summary>
  38. /// 操作样式下拉数据
  39. /// </summary>
  40. private List<string> files = new List<string>();
  41. /// <summary>
  42. /// 另存为弹出框显示
  43. /// </summary>
  44. private CreateNameDialog dialog;
  45. /// <summary>
  46. /// 另存为文件名
  47. /// </summary>
  48. private string newName;
  49. // private Bitmap bitmap;
  50. // private OpenCvSharp.Mat mat;
  51. // private OpenCvSharp.Mat oldMat;
  52. //各页面组件
  53. /*private LabelTextControl textControl;
  54. private LabelMarkDateControl dateMarkControl;
  55. private LabelMarkTimeControl timeMarkControl;*/
  56. private AdjustContainer adjustCameraControl;
  57. private FrameCameraControl frameCameraControl;
  58. private CommonCameraControl commonCameraControl;
  59. //private int width, height;
  60. /// <summary>
  61. /// 相机
  62. /// </summary>
  63. //private MIChrome5Pro m5pro;
  64. /// <summary>
  65. /// 一个矩阵数组,用来接收直方图,记得全部初始化
  66. /// </summary>
  67. OpenCvSharp.Mat[] oldHists = new OpenCvSharp.Mat[] { new OpenCvSharp.Mat(), new OpenCvSharp.Mat(), new OpenCvSharp.Mat() };
  68. /// <summary>
  69. /// BGR线条颜色
  70. /// </summary>
  71. private OpenCvSharp.Scalar[] color = new OpenCvSharp.Scalar[] { new OpenCvSharp.Scalar(255, 0, 0, 255), new OpenCvSharp.Scalar(0, 255, 0, 255), new OpenCvSharp.Scalar(0, 0, 255, 255) };
  72. #region 控件
  73. private System.Windows.Forms.GroupBox groupBox1;
  74. private System.Windows.Forms.GroupBox groupBox3;
  75. private System.Windows.Forms.Panel controlPanel;
  76. private System.Windows.Forms.ListBox listBox1;
  77. private Button button3;
  78. private Button button2;
  79. private Label label1;
  80. private Button btnClose;
  81. private Label label2;
  82. private PictureBox pictureBox1;
  83. private Label lblParamName;
  84. private Label label4;
  85. private Timer timer1;
  86. private System.ComponentModel.IContainer components;
  87. private Button btnRest;
  88. #endregion
  89. public CameraSettingDialog()
  90. {
  91. if (list == null || list.Count == 0)
  92. {
  93. CameraRelationModel crm1 = new CameraRelationModel(1, PdnResources.GetString("Menu.Image.Adjust.Text"), CameraParamType.Adjust);
  94. list.Add(crm1);
  95. CameraRelationModel crm2 = new CameraRelationModel(2, PdnResources.GetString("Menu.ImageCollection.CameraFrame.Text"), CameraParamType.Frame);
  96. list.Add(crm2);
  97. CameraRelationModel crm3 = new CameraRelationModel(3, PdnResources.GetString("Menu.ImageCollection.CameraConventional.Text"), CameraParamType.Common);
  98. list.Add(crm3);
  99. }
  100. adjustCameraControl = new AdjustContainer();
  101. adjustCameraControl.Initialize(m_cameraParamModel, true);
  102. adjustCameraControl.Location = new Point(10, 15);
  103. frameCameraControl = new FrameCameraControl(m_cameraParamModel, true);
  104. frameCameraControl.Location = new Point(10, 15);
  105. commonCameraControl = new CommonCameraControl();
  106. commonCameraControl.Initialize(m_cameraParamModel, true);
  107. commonCameraControl.Location = new Point(10, 15);
  108. InitializeComponent();
  109. InitializeLanguageText();
  110. InitializeData();
  111. timer1.Tick += (s, e) =>
  112. {
  113. controlPanel.Enabled = CameraManager.IsLive;
  114. if (CameraManager.IsLive)
  115. adjustCameraControl.UpdateDisplay();
  116. };
  117. this.Load += (s, e) => { timer1.Enabled = true; };
  118. this.FormClosing += (s, e) =>
  119. {
  120. frameCameraControl.Dispose();
  121. adjustCameraControl.Dispose();
  122. timer1.Enabled = false;
  123. };
  124. }
  125. private void InitializeLanguageText()
  126. {
  127. this.label1.Text = PdnResources.GetString("Menu.Systemusecamera.text") + ":";
  128. this.button3.Text = PdnResources.GetString("Menu.File.SaveAs.Text");
  129. this.button2.Text = PdnResources.GetString("Menu.File.Save.Text");
  130. this.groupBox3.Text = PdnResources.GetString("Menu.Type.text");
  131. this.btnClose.Text = PdnResources.GetString("Menu.File.Close.Text");
  132. this.btnRest.Text = PdnResources.GetString("Menu.reinformation.text");
  133. this.Text = PdnResources.GetString("Menu.ImageCollection.CameraSetting.Text");
  134. }
  135. private void InitializeComponent()
  136. {
  137. this.components = new System.ComponentModel.Container();
  138. this.groupBox1 = new System.Windows.Forms.GroupBox();
  139. this.lblParamName = new System.Windows.Forms.Label();
  140. this.label4 = new System.Windows.Forms.Label();
  141. this.label2 = new System.Windows.Forms.Label();
  142. this.label1 = new System.Windows.Forms.Label();
  143. this.button3 = new System.Windows.Forms.Button();
  144. this.button2 = new System.Windows.Forms.Button();
  145. this.groupBox3 = new System.Windows.Forms.GroupBox();
  146. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  147. this.btnClose = new System.Windows.Forms.Button();
  148. this.btnRest = new System.Windows.Forms.Button();
  149. this.listBox1 = new System.Windows.Forms.ListBox();
  150. this.controlPanel = new System.Windows.Forms.Panel();
  151. this.timer1 = new System.Windows.Forms.Timer(this.components);
  152. this.groupBox1.SuspendLayout();
  153. this.groupBox3.SuspendLayout();
  154. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  155. this.SuspendLayout();
  156. //
  157. // groupBox1
  158. //
  159. this.groupBox1.Controls.Add(this.lblParamName);
  160. this.groupBox1.Controls.Add(this.label4);
  161. this.groupBox1.Controls.Add(this.label2);
  162. this.groupBox1.Controls.Add(this.label1);
  163. this.groupBox1.Controls.Add(this.button3);
  164. this.groupBox1.Controls.Add(this.button2);
  165. this.groupBox1.Location = new System.Drawing.Point(134, 13);
  166. this.groupBox1.Name = "groupBox1";
  167. this.groupBox1.Size = new System.Drawing.Size(556, 78);
  168. this.groupBox1.TabIndex = 1;
  169. this.groupBox1.TabStop = false;
  170. //
  171. // lblParamName
  172. //
  173. this.lblParamName.AutoSize = true;
  174. this.lblParamName.Location = new System.Drawing.Point(109, 47);
  175. this.lblParamName.Name = "lblParamName";
  176. this.lblParamName.Size = new System.Drawing.Size(53, 12);
  177. this.lblParamName.TabIndex = 8;
  178. this.lblParamName.Text = " ";
  179. //
  180. // label4
  181. //
  182. this.label4.AutoSize = true;
  183. this.label4.Location = new System.Drawing.Point(14, 47);
  184. this.label4.Name = "label4";
  185. this.label4.Size = new System.Drawing.Size(89, 12);
  186. this.label4.TabIndex = 7;
  187. this.label4.Text = "相机参数名称:";
  188. //
  189. // label2
  190. //
  191. this.label2.AutoSize = true;
  192. this.label2.Location = new System.Drawing.Point(109, 25);
  193. this.label2.Name = "label2";
  194. this.label2.Size = new System.Drawing.Size(53, 12);
  195. this.label2.TabIndex = 6;
  196. this.label2.Text = " ";
  197. //
  198. // label1
  199. //
  200. this.label1.AutoSize = true;
  201. this.label1.Location = new System.Drawing.Point(14, 25);
  202. this.label1.Name = "label1";
  203. this.label1.Size = new System.Drawing.Size(89, 12);
  204. this.label1.TabIndex = 5;
  205. this.label1.Text = "系统使用相机:";
  206. //
  207. // button3
  208. //
  209. this.button3.Location = new System.Drawing.Point(457, 20);
  210. this.button3.Name = "button3";
  211. this.button3.Size = new System.Drawing.Size(75, 23);
  212. this.button3.TabIndex = 2;
  213. this.button3.Text = "另存为";
  214. this.button3.UseVisualStyleBackColor = true;
  215. this.button3.Click += new System.EventHandler(this.button3_Click);
  216. //
  217. // button2
  218. //
  219. this.button2.Location = new System.Drawing.Point(376, 19);
  220. this.button2.Name = "button2";
  221. this.button2.Size = new System.Drawing.Size(75, 23);
  222. this.button2.TabIndex = 1;
  223. this.button2.Text = "保存";
  224. this.button2.UseVisualStyleBackColor = true;
  225. this.button2.Click += new System.EventHandler(this.button2_Click);
  226. //
  227. // groupBox3
  228. //
  229. this.groupBox3.Controls.Add(this.pictureBox1);
  230. this.groupBox3.Controls.Add(this.btnClose);
  231. this.groupBox3.Controls.Add(this.btnRest);
  232. this.groupBox3.Controls.Add(this.listBox1);
  233. this.groupBox3.Location = new System.Drawing.Point(13, 13);
  234. this.groupBox3.Name = "groupBox3";
  235. this.groupBox3.Size = new System.Drawing.Size(115, 471);
  236. this.groupBox3.TabIndex = 3;
  237. this.groupBox3.TabStop = false;
  238. //
  239. // pictureBox1
  240. //
  241. this.pictureBox1.Location = new System.Drawing.Point(6, 301);
  242. this.pictureBox1.Name = "pictureBox1";
  243. this.pictureBox1.Size = new System.Drawing.Size(100, 50);
  244. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  245. this.pictureBox1.TabIndex = 4;
  246. this.pictureBox1.TabStop = false;
  247. //
  248. // btnClose
  249. //
  250. this.btnClose.Location = new System.Drawing.Point(16, 431);
  251. this.btnClose.Name = "btnClose";
  252. this.btnClose.Size = new System.Drawing.Size(75, 23);
  253. this.btnClose.TabIndex = 3;
  254. this.btnClose.Text = "关闭";
  255. this.btnClose.UseVisualStyleBackColor = true;
  256. this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
  257. //
  258. // btnRest
  259. //
  260. this.btnRest.Location = new System.Drawing.Point(16, 393);
  261. this.btnRest.Name = "btnRest";
  262. this.btnRest.Size = new System.Drawing.Size(75, 23);
  263. this.btnRest.TabIndex = 2;
  264. this.btnRest.Text = "重置";
  265. this.btnRest.UseVisualStyleBackColor = true;
  266. this.btnRest.Click += new System.EventHandler(this.btnRest_Click);
  267. //
  268. // listBox1
  269. //
  270. this.listBox1.FormattingEnabled = true;
  271. this.listBox1.ItemHeight = 12;
  272. this.listBox1.Location = new System.Drawing.Point(6, 20);
  273. this.listBox1.Name = "listBox1";
  274. this.listBox1.Size = new System.Drawing.Size(103, 208);
  275. this.listBox1.TabIndex = 0;
  276. this.listBox1.SelectedValueChanged += new System.EventHandler(this.listBox1_SelectedValueChanged);
  277. //
  278. // controlPanel
  279. //
  280. this.controlPanel.AutoScroll = true;
  281. this.controlPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  282. this.controlPanel.Location = new System.Drawing.Point(134, 98);
  283. this.controlPanel.Name = "controlPanel";
  284. this.controlPanel.Size = new System.Drawing.Size(556, 386);
  285. this.controlPanel.TabIndex = 5;
  286. //
  287. // CameraSettingDialog
  288. //
  289. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  290. this.ClientSize = new System.Drawing.Size(702, 496);
  291. this.Controls.Add(this.controlPanel);
  292. this.Controls.Add(this.groupBox3);
  293. this.Controls.Add(this.groupBox1);
  294. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  295. this.Name = "CameraSettingDialog";
  296. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  297. this.Text = "相机设置";
  298. this.Activated += new System.EventHandler(this.CameraSettingDialog_Activated);
  299. this.Controls.SetChildIndex(this.groupBox1, 0);
  300. this.Controls.SetChildIndex(this.groupBox3, 0);
  301. this.Controls.SetChildIndex(this.controlPanel, 0);
  302. this.groupBox1.ResumeLayout(false);
  303. this.groupBox1.PerformLayout();
  304. this.groupBox3.ResumeLayout(false);
  305. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  306. this.ResumeLayout(false);
  307. }
  308. /// <summary>
  309. /// 初始化数据
  310. /// </summary>
  311. public void InitializeData()
  312. {
  313. //绑定左侧listbox数据
  314. this.listBox1.Items.Clear();
  315. foreach (CameraRelationModel model in list)
  316. {
  317. this.listBox1.Items.Add(model.name);
  318. }
  319. this.listBox1.SelectedIndex = 0;
  320. try
  321. {
  322. // 获取系统使用的相机名称
  323. ICamera camera = CameraManager.CurrentCamera;
  324. // 获取系统使用的相机SN
  325. string sn;
  326. camera.ReadRegisterData(out sn);
  327. //label2.Text = camera.GetName();
  328. if (!string.IsNullOrEmpty(sn))
  329. {
  330. label2.Text = sn;
  331. }
  332. m_cameraParamModel = Startup.instance.cameraParamModel;
  333. lblParamName.Text = CameraConfigs.GetInstance().CurrentConfigFileName;
  334. }
  335. catch (Exception ex)
  336. {
  337. MessageBox.Show(ex.Message);
  338. }
  339. }
  340. /// <summary>
  341. /// 当前操作样式 下拉选切换
  342. /// </summary>
  343. /// <param name="sender"></param>
  344. /// <param name="e"></param>
  345. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  346. {
  347. InitializeData();
  348. }
  349. /// <summary>
  350. /// 左侧listbox选择事件
  351. /// </summary>
  352. /// <param name="sender"></param>
  353. /// <param name="e"></param>
  354. private void listBox1_SelectedValueChanged(object sender, EventArgs e)
  355. {
  356. //首先更新右侧分类部分
  357. UpdateRightCatalog();
  358. //更新右侧设置界面
  359. UpdateRightSettingUI();
  360. }
  361. /// <summary>
  362. /// 更新右侧分类
  363. /// </summary>
  364. private void UpdateRightCatalog()
  365. {
  366. this.controlPanel.Controls.Clear();
  367. CameraRelationModel model = list[this.listBox1.SelectedIndex];
  368. this.cameraParamType = model.cameraParamType;
  369. }
  370. /// <summary>
  371. /// 更新右侧设置
  372. /// </summary>
  373. private void UpdateRightSettingUI()
  374. {
  375. this.controlPanel.Controls.Clear();
  376. switch (this.cameraParamType)
  377. {
  378. case CameraParamType.Adjust: //调节
  379. adjustCameraControl.Location = new Point(0, 0);
  380. this.controlPanel.Controls.Add(adjustCameraControl);
  381. break;
  382. case CameraParamType.Frame: //边框
  383. frameCameraControl.Location = new Point(0, 0);
  384. this.controlPanel.Controls.Add(frameCameraControl);
  385. break;
  386. case CameraParamType.Common: //常规
  387. commonCameraControl.Location = new Point(0, 0);
  388. this.controlPanel.Controls.Add(commonCameraControl);
  389. break;
  390. }
  391. }
  392. /// <summary>
  393. /// 另存为 出弹窗
  394. /// </summary>
  395. /// <param name="sender"></param>
  396. /// <param name="e"></param>
  397. private void button3_Click(object sender, EventArgs e)
  398. {
  399. dialog = new CreateNameDialog(this);
  400. dialog.Text = PdnResources.GetString("Menu.Savecamerasettings.text");
  401. dialog.StartPosition = FormStartPosition.CenterParent;
  402. dialog.ShowDialog();
  403. }
  404. /// <summary>
  405. /// 另存为弹窗 确定保存
  406. /// </summary>
  407. /// <param name="name"></param>
  408. public override void GetCreateName(string name)
  409. {
  410. // string currentcomboBox = this.comboBox1.Text;
  411. this.newName = name;
  412. if (CameraConfigs.GetInstance().FileExist(this.newName))
  413. {
  414. MessageBox.Show(PdnResources.GetString("Menu.Namecannotbepeated.Text"), PdnResources.GetString("Menu.ensure.text"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
  415. return;
  416. }
  417. CameraConfigs.GetInstance().Save(this.m_cameraParamModel, this.newName);
  418. dialog.Close();
  419. MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text"));
  420. }
  421. /// <summary>
  422. /// 保存
  423. /// </summary>
  424. /// <param name="sender"></param>
  425. /// <param name="e"></param>
  426. private void button2_Click(object sender, EventArgs e)
  427. {
  428. CameraConfigs.GetInstance().Save();
  429. MessageBox.Show(PdnResources.GetString("Menu.Successfullysaved.text"));
  430. }
  431. private void btnClose_Click(object sender, EventArgs e)
  432. {
  433. this.Close();
  434. this.Dispose();
  435. }
  436. private void btnRest_Click(object sender, EventArgs e)
  437. {
  438. CameraConfigs cameraConfig = CameraConfigs.GetInstance();
  439. Startup.instance.cameraParamModel = cameraConfig.GetDefaultCameraParamModel();
  440. m_cameraParamModel = Startup.instance.cameraParamModel;
  441. if (adjustCameraControl != null)
  442. {
  443. adjustCameraControl.Initialize(Startup.instance.cameraParamModel);
  444. }
  445. if (frameCameraControl != null)
  446. {
  447. frameCameraControl.ResetCameraParamModel(Startup.instance.cameraParamModel);
  448. }
  449. if (commonCameraControl != null)
  450. {
  451. commonCameraControl.Initialize(Startup.instance.cameraParamModel);
  452. }
  453. }
  454. private void CameraSettingDialog_Activated(object sender, EventArgs e)
  455. {
  456. m_cameraParamModel = Startup.instance.cameraParamModel;
  457. lblParamName.Text = CameraConfigs.GetInstance().CurrentConfigFileName;
  458. adjustCameraControl.Initialize(m_cameraParamModel, true);
  459. frameCameraControl.ResetCameraParamModel(m_cameraParamModel);
  460. commonCameraControl.ResetCameraParamModel(m_cameraParamModel);
  461. }
  462. private void timer1_Tick(object sender, EventArgs e)
  463. {
  464. controlPanel.Enabled = Camera.CameraManager.IsLive;
  465. }
  466. }
  467. }