CameraPreviewDialog.cs 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using OpenCvSharp.ImgHash;
  4. using PaintDotNet.Adjust;
  5. using PaintDotNet.Base.CommTool;
  6. using PaintDotNet.DbOpreate.DbBll;
  7. using PaintDotNet.DbOpreate.DbModel;
  8. using PaintDotNet.ImageCollect.CameraPreviewComponent;
  9. using PaintDotNet.ImageCollect.CameraPreviewComponent.Properties;
  10. using PaintDotNet.Instrument;
  11. using PaintDotNet.Measuring;
  12. using PaintDotNet.Menus;
  13. using PaintDotNet.Base.SettingModel;
  14. using PaintDotNet.SystemLayer;
  15. using StageController;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Drawing;
  19. using System.IO;
  20. using System.Runtime.InteropServices;
  21. using System.Threading;
  22. using System.Windows.Forms;
  23. using PaintDotNet.Adjust.BaseImage;
  24. using PaintDotNet.Annotation.Measure;
  25. using PaintDotNet.Annotation.Enum;
  26. using PaintDotNet.Annotation;
  27. using Point = System.Drawing.Point;
  28. using StageController.M3H;
  29. using System.Threading.Tasks;
  30. using System.Linq;
  31. using Metis.AutoAnalysis;
  32. using PaintDotNet.Camera;
  33. using PaintDotNet.Preview2;
  34. namespace PaintDotNet.ImageCollect
  35. {
  36. internal class CameraPreviewDialog : FloatingToolForm, IStageEvent
  37. {
  38. public bool o_use = false; // [原图取色]设置是否立刻生效
  39. //private Bitmap m_bitmat;
  40. ////private Mat m_mat; // [区域选择]白平衡结果存储
  41. public bool m_use = false; // [区域选择]设置是否立刻生效(,设置到相机)
  42. public Point m_startP; //画框的起始点
  43. private bool m_blnDraw = false;
  44. private Rectangle m_rect;//初始设置一个拖拽的显示框
  45. SolidBrush m_brush = new SolidBrush(Color.Black);
  46. int m_handle = 0;
  47. public bool isLeave = false;//鼠标是否离开窗体
  48. public static CameraPreviewDialog cameraPreviewDialog;
  49. private AppWorkspace appWorkspace;
  50. public DocumentWorkspaceWindow documentWorkspace;
  51. private GroupBox groupBox2;
  52. private GroupBox groupBox3;
  53. private GroupBox groupBox4;
  54. private ProgressBar progressBar1;
  55. private Button btnRestStep;
  56. public ComboBox comboBox1;
  57. private Panel panel1;
  58. private Panel panel2;
  59. private GroupBox groupBox1;
  60. private Panel panel3;
  61. private BaseActionsStrip baseActionsStrip;
  62. private ShootActionsStrip shootActionsStrip;
  63. private TheoreticalScaleStrip theoreticalScaleStrip;
  64. //private Mat m_mat;
  65. public static Mat m_matClone;
  66. public static Mat m_matOrig;
  67. private Document document;
  68. private ComboBox cmbConfigs;
  69. private Button btnConfigDel;
  70. private TextBox gainNumbertxt;
  71. private string picturePath = Application.StartupPath + "\\Picture\\";
  72. /// <summary>
  73. /// 显示图像
  74. /// </summary>
  75. private ICamera m_camera => CameraManager.CurrentCamera;
  76. /// <summary>
  77. /// 窗口标题
  78. /// </summary>
  79. public string DailogTitle { get; }
  80. private CameraParamModel m_cameraParamModel;
  81. private CameraConfigs m_cameraConfig;
  82. private AxisController m_Stage;
  83. PropertiesSettingDialog fmSetting;
  84. private bool maxAndMinButtonEnable = false;//最大最小按钮点击状态
  85. private bool bestButtonEnable = false;//最佳按钮点击状态
  86. private bool gamma45ButtonEnable = false;//伽马45按钮点击状态
  87. private int trackBar1Value = -50;//亮度
  88. private int trackBar2Value = 100;//对比度
  89. private int trackBar3Value = 100;//gamma值
  90. private bool underexposedButtonEnable = false;//曝光不足按钮点击状态
  91. private bool overexposedButton = false;//过曝光按钮点击状态
  92. private double micronRatio;//每像素多少微米
  93. private CheckBox checkBox1;
  94. private bool isFitToWindow = false;//辅助判断当前尺寸是否为适应大小
  95. private TheoreticalScaleDialog theoreticalScaleDialog;//理论比例尺窗口
  96. private Dictionary<string, mic_rulers> micRulersDictonary;//标尺下拉菜单绑定数据
  97. private BindingSource bs;//绑定combobox的数据源
  98. private System.Windows.Forms.Timer tmrUpdate;
  99. private System.ComponentModel.IContainer components;
  100. private Panel panelZ;
  101. private Label lblZPos;
  102. private Button btnZClear;
  103. private Button btuZDown;
  104. private Button btnZUp;
  105. private NumericUpDown numZLen;
  106. /// <summary>
  107. /// 预览属性
  108. /// </summary>
  109. // ScriptEditDialog histogramDialog;
  110. //public bool comboBox_Change = false;
  111. public string comboBox_Value = string.Empty;
  112. public CameraPreviewDialog(AppWorkspace appWorkspace)
  113. {
  114. cameraPreviewDialog = this;
  115. this.DoubleBuffered = true;//设置本窗体
  116. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  117. SetStyle(ControlStyles.UserPaint, true);
  118. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  119. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  120. this.appWorkspace = appWorkspace;
  121. this.appWorkspace.cameraPreviewDialog = this;
  122. DailogTitle = PdnResources.GetString("Menu.ImageCollection.PreviewExtraction.Text");
  123. this.Text = DailogTitle;
  124. //this.FormBorderStyle = FormBorderStyle.FixedSingle;
  125. this.MinimizeBox = false;
  126. this.MaximizeBox = true;
  127. m_cameraParamModel = Startup.instance.cameraParamModel;
  128. m_cameraConfig = CameraConfigs.GetInstance();
  129. CameraManager.FrameCallback += CallbackDraw;
  130. // BIN
  131. //m_camera.BinningSum = m_cameraParamModel.parame.BinningSumation;
  132. InitStage();
  133. InitializeComponent();
  134. InitializeLanguageText();
  135. InitializeBaseTool();
  136. InitializeShootTool();
  137. InitializeTheoreticalScaleStripTool();
  138. // 加载配置文件列表
  139. initCmbConfigs();
  140. InitializeImage();
  141. // 分辨率 非2448 * 2048的时, ROI在StartWaitForFrame后设置
  142. if (m_camera.ResolutionId != 0)
  143. {
  144. }
  145. this.Resize += new EventHandler(Form_Resize);
  146. BindingRule();
  147. getRulerMicronRatio();//计算每像素多少微米
  148. this.MinimumSize = new System.Drawing.Size(748, 656);//设置窗口最小尺寸
  149. comboBox_Value = comboBox1.Text;//获取下拉值
  150. CurrentImage = () => { return BitmapConverter.ToBitmap(m_matClone); };
  151. }
  152. private void InitStage()
  153. {
  154. try
  155. {
  156. m_Stage = AxisController.GetInstance();
  157. m_Stage.AddApp(this);
  158. }
  159. catch { }
  160. }
  161. /// <summary>
  162. /// 绑定标尺
  163. /// </summary>
  164. public void BindingRule()
  165. {
  166. this.comboBox1.Items.Clear();
  167. //绑定标尺下拉菜单
  168. List<mic_rulers> list = Startup.instance.mic_rulersAll;
  169. string rulerName = "";
  170. if (list != null)
  171. {
  172. for (int ruleI = 0; ruleI < list.Count; ruleI++)
  173. {
  174. mic_rulers rule = list[ruleI];
  175. int rulid = rule.id;
  176. if (rulid == Startup.instance.configModel.RulerId)
  177. {
  178. rulerName = rule.ruler_name;
  179. }
  180. this.comboBox1.Items.Add(rule.ruler_name);
  181. }
  182. }
  183. for (int i = 0; i < this.comboBox1.Items.Count; i++)
  184. {
  185. if (rulerName.Equals(this.comboBox1.Items[i].ToString()))
  186. {
  187. this.comboBox1.SelectedIndex = i;
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 加载数据
  193. /// </summary>
  194. public void InitializeData()
  195. {
  196. m_cameraParamModel = Startup.instance.cameraParamModel;
  197. m_cameraConfig.CameraParamInit();
  198. }
  199. /// <summary>
  200. /// 加载配置文件列表
  201. /// </summary>
  202. public void initCmbConfigs()
  203. {
  204. string selected = "";
  205. this.cmbConfigs.Items.Clear();
  206. List<string> fileNames = CameraConfigs.GetInstance().GetAllConfigFiles();
  207. foreach (string fileName in fileNames)
  208. {
  209. this.cmbConfigs.Items.Add(fileName);
  210. if (fileName.Equals(CameraConfigs.GetInstance().CurrentConfigFileName))
  211. {
  212. selected = fileName;
  213. }
  214. }
  215. if (selected.Equals("") && fileNames.Count > 0)
  216. {
  217. selected = fileNames[0];
  218. }
  219. this.cmbConfigs.SelectedItem = selected;
  220. }
  221. /// <summary>
  222. /// 修改配置文件列表
  223. /// </summary>
  224. public void UpdateCmbConfigs()
  225. {
  226. string selected = "";
  227. List<string> fileNames = CameraConfigs.GetInstance().GetAllConfigFiles();
  228. if (fileNames.Count == cmbConfigs.Items.Count)
  229. return;
  230. this.cmbConfigs.Items.Clear();
  231. foreach (string fileName in fileNames)
  232. {
  233. this.cmbConfigs.Items.Add(fileName);
  234. if (fileName.Equals(CameraConfigs.GetInstance().CurrentConfigFileName))
  235. {
  236. selected = fileName;
  237. }
  238. }
  239. if (selected.Equals("") && fileNames.Count > 0)
  240. {
  241. selected = fileNames[0];
  242. }
  243. this.cmbConfigs.SelectedItem = selected;
  244. }
  245. private void InitializeLanguageText()
  246. {
  247. this.groupBox1.Text = PdnResources.GetString("Menu.Preview.text");
  248. this.groupBox2.Text = PdnResources.GetString("Menu.operation.text");
  249. this.groupBox3.Text = PdnResources.GetString("Menu.Ruler.text");
  250. this.checkBox1.Text = PdnResources.GetString("Menu.imagecapture.Previewwindow.Shootingruler.text");
  251. this.groupBox4.Text = PdnResources.GetString("Menu.ImageCollection.CameraSetting.Text");
  252. this.btnRestStep.Text = PdnResources.GetString("Menu.reset.Text");
  253. }
  254. /// <summary>
  255. /// 实例化预览窗口同期组件
  256. /// </summary>
  257. public void InitializeComponent()
  258. {
  259. this.components = new System.ComponentModel.Container();
  260. this.groupBox1 = new System.Windows.Forms.GroupBox();
  261. this.panel2 = new System.Windows.Forms.Panel();
  262. this.panel1 = new System.Windows.Forms.Panel();
  263. this.groupBox2 = new System.Windows.Forms.GroupBox();
  264. this.panelZ = new System.Windows.Forms.Panel();
  265. this.lblZPos = new System.Windows.Forms.Label();
  266. this.btnZClear = new System.Windows.Forms.Button();
  267. this.btuZDown = new System.Windows.Forms.Button();
  268. this.btnZUp = new System.Windows.Forms.Button();
  269. this.numZLen = new System.Windows.Forms.NumericUpDown();
  270. this.groupBox3 = new System.Windows.Forms.GroupBox();
  271. this.panel3 = new System.Windows.Forms.Panel();
  272. this.checkBox1 = new System.Windows.Forms.CheckBox();
  273. this.comboBox1 = new System.Windows.Forms.ComboBox();
  274. this.groupBox4 = new System.Windows.Forms.GroupBox();
  275. this.btnConfigDel = new System.Windows.Forms.Button();
  276. this.cmbConfigs = new System.Windows.Forms.ComboBox();
  277. this.progressBar1 = new System.Windows.Forms.ProgressBar();
  278. this.btnRestStep = new System.Windows.Forms.Button();
  279. this.tmrUpdate = new System.Windows.Forms.Timer(this.components);
  280. this.groupBox1.SuspendLayout();
  281. this.groupBox2.SuspendLayout();
  282. this.panelZ.SuspendLayout();
  283. ((System.ComponentModel.ISupportInitialize)(this.numZLen)).BeginInit();
  284. this.groupBox3.SuspendLayout();
  285. this.groupBox4.SuspendLayout();
  286. this.SuspendLayout();
  287. //
  288. // groupBox1
  289. //
  290. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  291. | System.Windows.Forms.AnchorStyles.Left)
  292. | System.Windows.Forms.AnchorStyles.Right)));
  293. this.groupBox1.Controls.Add(this.panel2);
  294. this.groupBox1.Controls.Add(this.panel1);
  295. this.groupBox1.Location = new System.Drawing.Point(13, 13);
  296. this.groupBox1.Name = "groupBox1";
  297. this.groupBox1.Size = new System.Drawing.Size(707, 441);
  298. this.groupBox1.TabIndex = 1;
  299. this.groupBox1.TabStop = false;
  300. this.groupBox1.Text = "预览";
  301. //
  302. // panel2
  303. //
  304. this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  305. | System.Windows.Forms.AnchorStyles.Left)
  306. | System.Windows.Forms.AnchorStyles.Right)));
  307. this.panel2.Location = new System.Drawing.Point(7, 21);
  308. this.panel2.Name = "panel2";
  309. this.panel2.Size = new System.Drawing.Size(684, 368);
  310. this.panel2.TabIndex = 2;
  311. //
  312. // panel1
  313. //
  314. this.panel1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  315. this.panel1.Location = new System.Drawing.Point(7, 395);
  316. this.panel1.Name = "panel1";
  317. this.panel1.Size = new System.Drawing.Size(684, 40);
  318. this.panel1.TabIndex = 1;
  319. //
  320. // groupBox2
  321. //
  322. this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  323. | System.Windows.Forms.AnchorStyles.Right)));
  324. this.groupBox2.Controls.Add(this.panelZ);
  325. this.groupBox2.Location = new System.Drawing.Point(13, 460);
  326. this.groupBox2.Name = "groupBox2";
  327. this.groupBox2.Size = new System.Drawing.Size(707, 55);
  328. this.groupBox2.TabIndex = 2;
  329. this.groupBox2.TabStop = false;
  330. this.groupBox2.Text = "操作";
  331. //
  332. // panelZ
  333. //
  334. this.panelZ.Controls.Add(this.lblZPos);
  335. this.panelZ.Controls.Add(this.btnZClear);
  336. this.panelZ.Controls.Add(this.btuZDown);
  337. this.panelZ.Controls.Add(this.btnZUp);
  338. this.panelZ.Controls.Add(this.numZLen);
  339. this.panelZ.Dock = System.Windows.Forms.DockStyle.Right;
  340. this.panelZ.Location = new System.Drawing.Point(510, 17);
  341. this.panelZ.Name = "panelZ";
  342. this.panelZ.Size = new System.Drawing.Size(194, 35);
  343. this.panelZ.TabIndex = 0;
  344. this.panelZ.Visible = false;
  345. //
  346. // lblZPos
  347. //
  348. this.lblZPos.BackColor = System.Drawing.SystemColors.ButtonHighlight;
  349. this.lblZPos.Dock = System.Windows.Forms.DockStyle.Fill;
  350. this.lblZPos.Location = new System.Drawing.Point(132, 0);
  351. this.lblZPos.Name = "lblZPos";
  352. this.lblZPos.Size = new System.Drawing.Size(62, 35);
  353. this.lblZPos.TabIndex = 3;
  354. this.lblZPos.Text = "0000";
  355. this.lblZPos.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  356. //
  357. // btnZClear
  358. //
  359. this.btnZClear.Dock = System.Windows.Forms.DockStyle.Left;
  360. this.btnZClear.Location = new System.Drawing.Point(100, 0);
  361. this.btnZClear.Name = "btnZClear";
  362. this.btnZClear.Size = new System.Drawing.Size(32, 35);
  363. this.btnZClear.TabIndex = 2;
  364. this.btnZClear.Text = "C";
  365. this.btnZClear.UseVisualStyleBackColor = true;
  366. this.btnZClear.Click += new System.EventHandler(this.btnZClear_Click);
  367. //
  368. // btuZDown
  369. //
  370. this.btuZDown.Dock = System.Windows.Forms.DockStyle.Left;
  371. this.btuZDown.Location = new System.Drawing.Point(68, 0);
  372. this.btuZDown.Name = "btuZDown";
  373. this.btuZDown.Size = new System.Drawing.Size(32, 35);
  374. this.btuZDown.TabIndex = 1;
  375. this.btuZDown.Text = "↓";
  376. this.btuZDown.UseVisualStyleBackColor = true;
  377. this.btuZDown.Click += new System.EventHandler(this.btuZDown_Click);
  378. //
  379. // btnZUp
  380. //
  381. this.btnZUp.Dock = System.Windows.Forms.DockStyle.Left;
  382. this.btnZUp.Location = new System.Drawing.Point(38, 0);
  383. this.btnZUp.Name = "btnZUp";
  384. this.btnZUp.Size = new System.Drawing.Size(30, 35);
  385. this.btnZUp.TabIndex = 0;
  386. this.btnZUp.Text = "↑";
  387. this.btnZUp.UseVisualStyleBackColor = true;
  388. this.btnZUp.Click += new System.EventHandler(this.btnZUp_Click);
  389. //
  390. // numZLen
  391. //
  392. this.numZLen.Dock = System.Windows.Forms.DockStyle.Left;
  393. this.numZLen.Font = new System.Drawing.Font("Calibri", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  394. this.numZLen.Location = new System.Drawing.Point(0, 0);
  395. this.numZLen.Minimum = new decimal(new int[] {
  396. 1,
  397. 0,
  398. 0,
  399. 0});
  400. this.numZLen.Name = "numZLen";
  401. this.numZLen.Size = new System.Drawing.Size(38, 25);
  402. this.numZLen.TabIndex = 4;
  403. this.numZLen.Value = new decimal(new int[] {
  404. 1,
  405. 0,
  406. 0,
  407. 0});
  408. //
  409. // groupBox3
  410. //
  411. this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  412. | System.Windows.Forms.AnchorStyles.Right)));
  413. this.groupBox3.Controls.Add(this.panel3);
  414. this.groupBox3.Controls.Add(this.checkBox1);
  415. this.groupBox3.Controls.Add(this.comboBox1);
  416. this.groupBox3.Location = new System.Drawing.Point(13, 522);
  417. this.groupBox3.Name = "groupBox3";
  418. this.groupBox3.Size = new System.Drawing.Size(441, 55);
  419. this.groupBox3.TabIndex = 3;
  420. this.groupBox3.TabStop = false;
  421. this.groupBox3.Text = "标尺";
  422. //
  423. // panel3
  424. //
  425. this.panel3.BackColor = System.Drawing.Color.Transparent;
  426. this.panel3.Location = new System.Drawing.Point(290, 19);
  427. this.panel3.Name = "panel3";
  428. this.panel3.Size = new System.Drawing.Size(111, 24);
  429. this.panel3.TabIndex = 2;
  430. //
  431. // checkBox1
  432. //
  433. this.checkBox1.AutoSize = true;
  434. this.checkBox1.Location = new System.Drawing.Point(192, 24);
  435. this.checkBox1.Name = "checkBox1";
  436. this.checkBox1.Size = new System.Drawing.Size(15, 14);
  437. this.checkBox1.TabIndex = 1;
  438. this.checkBox1.UseVisualStyleBackColor = true;
  439. this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
  440. //
  441. // comboBox1
  442. //
  443. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  444. this.comboBox1.FormattingEnabled = true;
  445. this.comboBox1.Location = new System.Drawing.Point(7, 21);
  446. this.comboBox1.Name = "comboBox1";
  447. this.comboBox1.Size = new System.Drawing.Size(152, 20);
  448. this.comboBox1.TabIndex = 0;
  449. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  450. this.comboBox1.SelectionChangeCommitted += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
  451. //
  452. // groupBox4
  453. //
  454. this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  455. this.groupBox4.Controls.Add(this.btnConfigDel);
  456. this.groupBox4.Controls.Add(this.cmbConfigs);
  457. this.groupBox4.Location = new System.Drawing.Point(460, 522);
  458. this.groupBox4.Name = "groupBox4";
  459. this.groupBox4.Size = new System.Drawing.Size(260, 55);
  460. this.groupBox4.TabIndex = 4;
  461. this.groupBox4.TabStop = false;
  462. this.groupBox4.Text = "相机设置";
  463. //
  464. // btnConfigDel
  465. //
  466. this.btnConfigDel.Location = new System.Drawing.Point(213, 20);
  467. this.btnConfigDel.Margin = new System.Windows.Forms.Padding(2);
  468. this.btnConfigDel.Name = "btnConfigDel";
  469. this.btnConfigDel.Size = new System.Drawing.Size(31, 18);
  470. this.btnConfigDel.TabIndex = 1;
  471. this.btnConfigDel.Text = "Del";
  472. this.btnConfigDel.UseVisualStyleBackColor = true;
  473. this.btnConfigDel.Click += new System.EventHandler(this.btnConfigDel_Click);
  474. //
  475. // cmbConfigs
  476. //
  477. this.cmbConfigs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  478. this.cmbConfigs.FormattingEnabled = true;
  479. this.cmbConfigs.Location = new System.Drawing.Point(10, 21);
  480. this.cmbConfigs.Margin = new System.Windows.Forms.Padding(2);
  481. this.cmbConfigs.Name = "cmbConfigs";
  482. this.cmbConfigs.Size = new System.Drawing.Size(185, 20);
  483. this.cmbConfigs.TabIndex = 0;
  484. this.cmbConfigs.SelectedIndexChanged += new System.EventHandler(this.cmbConfigs_SelectedIndexChanged);
  485. //
  486. // progressBar1
  487. //
  488. this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  489. | System.Windows.Forms.AnchorStyles.Right)));
  490. this.progressBar1.Location = new System.Drawing.Point(13, 584);
  491. this.progressBar1.Name = "progressBar1";
  492. this.progressBar1.Size = new System.Drawing.Size(641, 23);
  493. this.progressBar1.TabIndex = 5;
  494. //
  495. // btnRestStep
  496. //
  497. this.btnRestStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  498. this.btnRestStep.Location = new System.Drawing.Point(661, 584);
  499. this.btnRestStep.Name = "btnRestStep";
  500. this.btnRestStep.Size = new System.Drawing.Size(59, 23);
  501. this.btnRestStep.TabIndex = 6;
  502. this.btnRestStep.Text = "复位";
  503. this.btnRestStep.UseVisualStyleBackColor = true;
  504. this.btnRestStep.Click += new System.EventHandler(this.btnRestStep_Click);
  505. //
  506. // tmrUpdate
  507. //
  508. this.tmrUpdate.Interval = 1000;
  509. this.tmrUpdate.Tick += new System.EventHandler(this.tmrUpdate_Tick);
  510. //
  511. // CameraPreviewDialog
  512. //
  513. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  514. this.ClientSize = new System.Drawing.Size(732, 618);
  515. this.Controls.Add(this.btnRestStep);
  516. this.Controls.Add(this.progressBar1);
  517. this.Controls.Add(this.groupBox4);
  518. this.Controls.Add(this.groupBox3);
  519. this.Controls.Add(this.groupBox2);
  520. this.Controls.Add(this.groupBox1);
  521. this.Margin = new System.Windows.Forms.Padding(4);
  522. this.Name = "CameraPreviewDialog";
  523. this.Activated += new System.EventHandler(this.CameraPreviewDialog_Activated);
  524. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraPreviewDialog_FormClosing);
  525. this.Load += new System.EventHandler(this.CameraPreviewDialog_Load);
  526. this.Controls.SetChildIndex(this.groupBox1, 0);
  527. this.Controls.SetChildIndex(this.groupBox2, 0);
  528. this.Controls.SetChildIndex(this.groupBox3, 0);
  529. this.Controls.SetChildIndex(this.groupBox4, 0);
  530. this.Controls.SetChildIndex(this.progressBar1, 0);
  531. this.Controls.SetChildIndex(this.btnRestStep, 0);
  532. this.groupBox1.ResumeLayout(false);
  533. this.groupBox2.ResumeLayout(false);
  534. this.panelZ.ResumeLayout(false);
  535. ((System.ComponentModel.ISupportInitialize)(this.numZLen)).EndInit();
  536. this.groupBox3.ResumeLayout(false);
  537. this.groupBox3.PerformLayout();
  538. this.groupBox4.ResumeLayout(false);
  539. this.ResumeLayout(false);
  540. }
  541. public void InitializeImage()
  542. {
  543. m_rect = new Rectangle();
  544. this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
  545. this.documentWorkspace.panel.MouseDown += DocumentWorkspace_MouseDown;
  546. this.documentWorkspace.panel.MouseMove += PictureBox1_MouseMove;
  547. this.documentWorkspace.panel.MouseUp += PictureBox1_MouseUp;
  548. this.documentWorkspace.panel.Paint += Panel_Paint;
  549. this.documentWorkspace.refueshZoomTrackValue = false;
  550. Bitmap bitmap = new Bitmap(m_cameraParamModel.parame.previewRectW, m_cameraParamModel.parame.previewRectH);
  551. document = Document.FromImage(bitmap);
  552. //if (bitmap.Width > 3 && bitmap.Height > 3)
  553. //{
  554. // m_rect.X = Math.Min(10, bitmap.Width / 2 - 1);
  555. // m_rect.Y = Math.Min(10, bitmap.Height / 2 - 1);
  556. // m_rect.Width = bitmap.Width - 2 * m_rect.X;
  557. // m_rect.Height = bitmap.Height - 2 * m_rect.Y;
  558. // Console.WriteLine(m_rect);
  559. //}
  560. //else
  561. {
  562. m_rect.X = 0;
  563. m_rect.Y = 0;
  564. m_rect.Width = 0;
  565. m_rect.Height = 0;
  566. }
  567. //this.documentWorkspace.AppWorkspaceTop = this.appWorkspace;
  568. //this.documentWorkspace.Visible = true;
  569. //this.documentWorkspace.PanelBottom.Visible = false;
  570. this.documentWorkspace.Dock = DockStyle.Fill;
  571. //this.documentWorkspace.RulersEnabled = true;
  572. this.panel2.Controls.Add(this.documentWorkspace);
  573. this.documentWorkspace.Document = document;
  574. this.documentWorkspace.ScaleFactorChanged += new EventHandler(DocumentWorkspace_ScaleFactorChanged);
  575. if (this.documentWorkspace.oldDrawTreeView == null && MeasureListDialog.drawNodes != null)
  576. {
  577. this.documentWorkspace.oldDrawTreeView = MeasureListDialog.drawNodes;
  578. //this.documentWorkspace.previewMeasure = true;
  579. this.documentWorkspace.rules = Startup.instance.rules;
  580. }
  581. }
  582. private void Panel_Paint(object sender, PaintEventArgs e)
  583. {
  584. if (m_use && documentWorkspace.CompositionSurface.Width != 0 && documentWorkspace.CompositionSurface.Height != 0)
  585. {
  586. if (m_rect.Width > 0 && m_rect.Height > 0)
  587. {
  588. //
  589. // 以下是计算绘制图片的位置和大小并绘制图片
  590. //
  591. Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
  592. int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
  593. int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
  594. int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
  595. int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
  596. //if (m_mat != null)
  597. //{
  598. // oldMPatch.Dispose();
  599. // oldMPatch = m_mat;
  600. //}
  601. //m_mat = new Data.Action.Action5012().PerformProcess(PaintDotNet.Camera.Tools.ToMat(m_bitmap).Clone(new Rect(m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height)));
  602. ////
  603. //// 以下是绘制网格、标注、测量、视场等开始
  604. ////
  605. e.Graphics.TranslateTransform(x, y);
  606. e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
  607. //if (m_bitmat != null)
  608. //{
  609. // e.Graphics.DrawImage(m_bitmat, m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height);
  610. //}
  611. if (!m_blnDraw)
  612. {
  613. e.Graphics.DrawRectangle(new Pen(Color.Black/*Red*/, 2), m_rect);//重新绘制颜色为红色
  614. for (int i = 1; i <= 8; i++)
  615. {
  616. e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i));
  617. }
  618. }
  619. e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
  620. e.Graphics.TranslateTransform(-x, -y);
  621. }
  622. }
  623. else
  624. {
  625. m_rect.Width = 0;
  626. m_rect.Height = 0;
  627. }
  628. }
  629. private void DocumentWorkspace_MouseDown(object sender, MouseEventArgs e)
  630. {
  631. if (!m_use && !o_use)
  632. return;
  633. // 换算后的点
  634. Point point1 = documentWorkspace.GetScalePoint(e.Location);//PointF
  635. m_handle = HitTest(point1);
  636. if (m_handle < 0)
  637. {
  638. if (m_rect.Contains(point1))//判断鼠标按下的坐标是否在红框中,确定是否拖动的红框
  639. {
  640. m_handle = 9;
  641. }
  642. }
  643. this.Cursor = GetHandleCursor(m_handle);
  644. m_startP = point1;
  645. Invalidate();
  646. m_blnDraw = true;
  647. }
  648. private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
  649. {
  650. if (!m_use)
  651. return;
  652. if (m_handle > 0 ||/*&&*/ m_blnDraw)
  653. {
  654. if (e.Button != MouseButtons.Left)//判断是否按下左键
  655. return;
  656. MoveHandleTo(documentWorkspace.GetScalePoint(e.Location), m_handle);
  657. }
  658. }
  659. private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
  660. {
  661. if (!m_use)
  662. return;
  663. m_blnDraw = false; //结束绘制
  664. this.Cursor = Cursors.Default;
  665. SetROI();
  666. this.documentWorkspace/*pictureBox1*/.Refresh();
  667. }
  668. private void SetROI()
  669. {
  670. if (m_use)
  671. {
  672. //m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
  673. //this.m_cameraParamModel.parame.previewRectX = m_targetX;
  674. //this.m_cameraParamModel.parame.previewRectY = m_targetY;
  675. //this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  676. //this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  677. }
  678. }
  679. public Rectangle GetHandleRectangle(int handleNumber)
  680. {
  681. Point point = GetHandle(handleNumber);
  682. return new Rectangle(point.X - 4, point.Y - 4, 8, 8);
  683. }
  684. public int HitTest(Point point)
  685. {
  686. for (int i = 1; i <= 8; i++)
  687. {
  688. if (GetHandleRectangle(i).Contains(point))
  689. return i;
  690. }
  691. return -1;
  692. }
  693. public Point GetHandle(int handleNumber)
  694. {
  695. int x, y, xCenter, yCenter;
  696. xCenter = m_rect.X + m_rect.Width / 2;
  697. yCenter = m_rect.Y + m_rect.Height / 2;
  698. x = m_rect.X;
  699. y = m_rect.Y;
  700. switch (handleNumber)
  701. {
  702. case 1:
  703. x = m_rect.X;
  704. y = m_rect.Y;
  705. break;
  706. case 2:
  707. x = xCenter;
  708. y = m_rect.Y;
  709. break;
  710. case 3:
  711. x = m_rect.Right;
  712. y = m_rect.Y;
  713. break;
  714. case 4:
  715. x = m_rect.Right;
  716. y = yCenter;
  717. break;
  718. case 5:
  719. x = m_rect.Right;
  720. y = m_rect.Bottom;
  721. break;
  722. case 6:
  723. x = xCenter;
  724. y = m_rect.Bottom;
  725. break;
  726. case 7:
  727. x = m_rect.X;
  728. y = m_rect.Bottom;
  729. break;
  730. case 8:
  731. x = m_rect.X;
  732. y = yCenter;
  733. break;
  734. }
  735. return new Point(x, y);
  736. }
  737. public Cursor GetHandleCursor(int handleNumber)
  738. {
  739. switch (handleNumber)
  740. {
  741. case 1:
  742. return Cursors.SizeNWSE;
  743. case 2:
  744. return Cursors.SizeNS;
  745. case 3:
  746. return Cursors.SizeNESW;
  747. case 4:
  748. return Cursors.SizeWE;
  749. case 5:
  750. return Cursors.SizeNWSE;
  751. case 6:
  752. return Cursors.SizeNS;
  753. case 7:
  754. return Cursors.SizeNESW;
  755. case 8:
  756. return Cursors.SizeWE;
  757. default:
  758. return Cursors.Default;
  759. }
  760. }
  761. public void MoveHandleTo(Point point, int handleNumber)
  762. {
  763. if (point.X > this.documentWorkspace.CompositionSurface/*pictureBox1*/.Width)
  764. {
  765. point.X = documentWorkspace.CompositionSurface/*pictureBox1*/.Width;
  766. }
  767. if (point.X < 0)
  768. {
  769. point.X = 0;
  770. }
  771. if (point.Y < 0)
  772. {
  773. point.Y = 0;
  774. }
  775. if (point.Y > documentWorkspace.CompositionSurface/*pictureBox1*/.Height)
  776. {
  777. point.Y = documentWorkspace.CompositionSurface/*pictureBox1*/.Height;
  778. }
  779. Point point1 = new Point(m_startP.X, m_startP.Y);
  780. if (point1.X > this.documentWorkspace.CompositionSurface/*pictureBox1*/.Width)
  781. {
  782. point1.X = documentWorkspace.CompositionSurface/*pictureBox1*/.Width;
  783. }
  784. if (point1.X < 0)
  785. {
  786. point1.X = 0;
  787. }
  788. if (point1.Y < 0)
  789. {
  790. point1.Y = 0;
  791. }
  792. if (point1.Y > documentWorkspace.CompositionSurface/*pictureBox1*/.Height)
  793. {
  794. point1.Y = documentWorkspace.CompositionSurface/*pictureBox1*/.Height;
  795. }
  796. m_rect.X = Math.Min(point.X, point1.X);
  797. m_rect.Y = Math.Min(point.Y, point1.Y);
  798. m_rect.Width = Math.Abs(point.X - point1.X);
  799. m_rect.Height = Math.Abs(point.Y - point1.Y);
  800. return;
  801. }
  802. /// <summary>
  803. /// 接收相机frame
  804. /// </summary>
  805. /// <param name="frame"></param>
  806. /// <param name="obj"></param>
  807. public void CallbackDraw(Bitmap bitmap)
  808. {
  809. //add begin by songxk
  810. if (bitmap == null)
  811. return;
  812. //add end by songxk
  813. try
  814. {
  815. var mat = PaintDotNet.Camera.Tools.ToMat(bitmap);
  816. m_matClone = mat.Clone();
  817. if (!this.IsShown)
  818. {
  819. return;
  820. }
  821. if (fmSetting != null)
  822. {
  823. m_matOrig = mat.Clone();
  824. try
  825. {
  826. trackBar1Value = fmSetting.displayCameraControl.trackBar1Value;
  827. trackBar2Value = fmSetting.displayCameraControl.trackBar2Value;
  828. trackBar3Value = fmSetting.displayCameraControl.trackBar3Value;
  829. }
  830. catch (Exception e)
  831. {
  832. System.Console.WriteLine("nullException can be catched here:" + e.Message);
  833. }
  834. }
  835. if (trackBar1Value != -50 || trackBar2Value != 100 || trackBar3Value != 100)
  836. {
  837. mat = PaintDotNet.Setting.LabelComponent.DisplayCameraControl.scrollMethod(mat, (trackBar1Value / 100.0).ToString("f2"), (trackBar2Value / 100.0).ToString("f2"), (trackBar3Value / 100.0).ToString("f2"));
  838. }
  839. else if (bestButtonEnable || maxAndMinButtonEnable || gamma45ButtonEnable)
  840. {
  841. if (bestButtonEnable)//最佳
  842. mat = AdjustIntent.CalHistMethod(mat, 1, gamma45ButtonEnable);
  843. if (maxAndMinButtonEnable)//最大最小
  844. mat = AdjustIntent.CalHistMethod(mat, 2, gamma45ButtonEnable);
  845. if (!bestButtonEnable && !maxAndMinButtonEnable)//仅伽马0.45
  846. mat = AdjustIntent.CalHistMethod(mat, 0, gamma45ButtonEnable);
  847. }
  848. if (underexposedButtonEnable)//曝光不足
  849. mat = ExposedIntent.UnderExposed(mat, 30, 0.5f);
  850. if (overexposedButton)//过曝光
  851. mat = ExposedIntent.OverExposed(mat, 225, 0.5f);
  852. //17461
  853. if (m_cameraParamModel.parame.ShadingCorrection == 1)//阴影矫正
  854. mat = AdjustIntent.ShadingCorrection(mat);
  855. ShowFrameThreadEntry(mat);
  856. }
  857. catch (Exception ex)
  858. {
  859. Console.WriteLine(ex.Message);
  860. }
  861. }
  862. private void ShowFrameThreadEntry(Mat matClone)
  863. {
  864. try
  865. {
  866. Bitmap m_bitmap = BitmapConverter.ToBitmap(matClone);
  867. if (o_use && m_startP.X >= 0 && m_startP.Y >= 0)
  868. {
  869. int x = m_startP.X;
  870. int y = m_startP.Y;
  871. if (x >= 0 && y >= 0 && x < m_bitmap.Width && y < m_bitmap.Height)
  872. {
  873. OpenCvSharp.Mat srt = PaintDotNet.Camera.Tools.ToMat(m_bitmap);
  874. OpenCvSharp.Vec3b bgr = srt.At<OpenCvSharp.Vec3b>(y, x);
  875. Color color = Color.FromArgb(bgr[2], bgr[1], bgr[0]);
  876. /// <summary>
  877. /// 当前处理的程序
  878. /// </summary>
  879. Data.Param.ParamObject action = new Data.Action.Action5012();
  880. //重新初始化选项列表
  881. Base.Args args = action.Lists[0];
  882. args.value = 3;
  883. for (int j = 0; j < args.choiseList.Count; j++)
  884. {
  885. Data.Param.BooleanObject argsChoise = args.choiseList[j] as Data.Param.BooleanObject;
  886. if (argsChoise.Lists.Count > 0)
  887. {
  888. Data.Param.ColorNumber phaseColor = argsChoise.Lists[0] as Data.Param.ColorNumber;
  889. if (phaseColor.Key.Equals("imageColor"))//原图取色
  890. {
  891. phaseColor.Value = color.ToArgb();// this.panel3.BackColor.ToArgb();
  892. }
  893. }
  894. }
  895. OpenCvSharp.Mat mat = action.PerformProcess(srt);
  896. //string filename2 = "E:\\show1" + (misshow++) + ".jpg";
  897. //Cv2.ImWrite(filename2, mat);
  898. this.documentWorkspace.Document = Document.FromMat(srt); ;
  899. }
  900. }
  901. else if (m_use && m_rect.Width > 0 && m_rect.Height > 0)
  902. {
  903. Mat m_mat = new Data.Action.Action5012().PerformProcess(matClone.Clone(new Rect(m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height)));
  904. //复制用户选中的区域到原图像
  905. //Mat mask0 = m_matClone.CvtColor(ColorConversionCodes.RGBA2GRAY);
  906. Mat pos0 = new Mat(matClone, new Rect(m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height));
  907. m_mat.CopyTo(pos0, m_mat);
  908. this.documentWorkspace.Document = Document.FromImage(BitmapConverter.ToBitmap(matClone));
  909. m_mat.Dispose();
  910. }
  911. else
  912. this.documentWorkspace.Document = Document.FromMat(matClone/*BitmapConverter.ToBitmap(m_matClone)*/);
  913. if (fmSetting != null && m_matOrig != null)
  914. {
  915. this.BeginInvoke(new Action(() => { fmSetting?.resetMat(m_matOrig); }));
  916. }
  917. }
  918. catch (Exception)
  919. {
  920. }
  921. }
  922. /// <summary>
  923. /// 初始化基础工具
  924. /// </summary>
  925. public void InitializeBaseTool()
  926. {
  927. this.baseActionsStrip = new BaseActionsStrip();
  928. //十字线按钮点击
  929. this.baseActionsStrip.crossButton.Click += new System.EventHandler(this.crossButton_Click);
  930. //网格按钮点击
  931. this.baseActionsStrip.gridButton.Click += new System.EventHandler(this.gridButton_Click);
  932. //方形按钮点击
  933. this.baseActionsStrip.squareButton.Click += new System.EventHandler(this.squareButton_Click);
  934. //圆形按钮点击
  935. this.baseActionsStrip.roundButton.Click += new System.EventHandler(this.roundButton_Click);
  936. //实际大小按钮点击
  937. this.baseActionsStrip.actualSizeButton.Click += new System.EventHandler(this.actualSizeButton_Click);
  938. //合适大小按钮点击
  939. this.baseActionsStrip.zoomToWindowButton.Click += new System.EventHandler(this.zoomToWindowButton_Click);
  940. //全屏按钮点击
  941. this.baseActionsStrip.fullScreenButton.Click += new System.EventHandler(this.fullScreenButton_Click);
  942. //缩小按钮点击
  943. this.baseActionsStrip.zoomOutButton.Click += new System.EventHandler(this.zoomOutButton_Click);
  944. //放大按钮点击
  945. this.baseActionsStrip.zoomInButton.Click += new System.EventHandler(this.zoomInButton_Click);
  946. this.baseActionsStrip.GripStyle = ToolStripGripStyle.Hidden;
  947. this.baseActionsStrip.Dock = DockStyle.None;
  948. this.baseActionsStrip.Location = new System.Drawing.Point(136, 0);
  949. this.panel1.Controls.Add(baseActionsStrip);
  950. this.gainNumbertxt = new TextBox();
  951. this.gainNumbertxt.Location = new System.Drawing.Point(554, 8);
  952. this.gainNumbertxt.Size = new System.Drawing.Size(62, 22);
  953. this.gainNumbertxt.Anchor = AnchorStyles.Left;
  954. this.gainNumbertxt.Text = PdnResources.GetString("Menu.nothing.Text");
  955. this.gainNumbertxt.ReadOnly = true;
  956. this.panel1.Controls.Add(this.gainNumbertxt);
  957. }
  958. /// <summary>
  959. /// 初始化快捷工具
  960. /// </summary>
  961. public void InitializeShootTool()
  962. {
  963. this.shootActionsStrip = new ShootActionsStrip();
  964. //拍摄按钮点击
  965. this.shootActionsStrip.shootButton.Click += new System.EventHandler(this.camera_Click);
  966. //单次拍摄按钮点击
  967. this.shootActionsStrip.aloneShootButton.Click += new System.EventHandler(this.aloneShootButton_Click);
  968. //速度按钮点击
  969. this.shootActionsStrip.speedButton.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(CopyDetail_DropDownItemClicked);
  970. //曝光时间点击事件
  971. this.shootActionsStrip.exposureTimeButton.Click += new System.EventHandler(this.exposureTimeButton_Click);
  972. //白平衡点击事件
  973. this.shootActionsStrip.whiteBalanceButton.Click += new System.EventHandler(this.whiteBalance_Click);
  974. //最佳点击事件
  975. this.shootActionsStrip.bestButton.Click += new System.EventHandler(this.bestButton_Click);
  976. //最小最大点击事件
  977. this.shootActionsStrip.maxAndMinButton.Click += new System.EventHandler(this.maxAndMinButton_Click);
  978. //原始状态事件
  979. this.shootActionsStrip.originalStateButton.Click += new System.EventHandler(this.originalStateButton_Click);
  980. //伽马0.45事件
  981. this.shootActionsStrip.gamma45Button.Click += new System.EventHandler(this.gamma45Button_Click);
  982. //曝光不足事件
  983. this.shootActionsStrip.underexposedButton.Click += new System.EventHandler(this.underexposedButton_Click);
  984. //过曝光事件
  985. this.shootActionsStrip.overexposedButton.Click += new System.EventHandler(this.overexposedButton_Click);
  986. //预览属性事件
  987. this.shootActionsStrip.propertiesButton.Click += new System.EventHandler(this.propertiesButton_Click);
  988. //自动聚焦事件
  989. this.shootActionsStrip.autoFocusButton.Click += new System.EventHandler(this.autoFocusButton_Click);
  990. this.shootActionsStrip.GripStyle = ToolStripGripStyle.Hidden;
  991. shootActionsStrip.Dock = DockStyle.Fill;
  992. this.groupBox2.Controls.Add(this.shootActionsStrip);
  993. }
  994. /// <summary>
  995. /// 理论比例尺初始化
  996. /// </summary>
  997. private void InitializeTheoreticalScaleStripTool()
  998. {
  999. this.theoreticalScaleStrip = new TheoreticalScaleStrip();
  1000. this.theoreticalScaleStrip.theoreticalScaleButton.Click += new System.EventHandler(this.theoreticalScaleButton_Click);
  1001. this.theoreticalScaleStrip.GripStyle = ToolStripGripStyle.Hidden;
  1002. this.theoreticalScaleStrip.Dock = DockStyle.None;
  1003. this.theoreticalScaleStrip.BackColor = Color.Transparent;
  1004. this.panel3.Controls.Add(theoreticalScaleStrip);
  1005. }
  1006. /// <summary>
  1007. /// 拍照按钮被点击
  1008. /// </summary>
  1009. /// <param name="sender"></param>
  1010. /// <param name="e"></param>
  1011. public void camera_Click(object sender, EventArgs e)
  1012. {
  1013. Capture1();
  1014. }
  1015. /// <summary>
  1016. /// 单次拍摄被点击
  1017. /// </summary>
  1018. /// <param name="sender"></param>
  1019. /// <param name="e"></param>
  1020. public void aloneShootButton_Click(object sender, EventArgs e)
  1021. {
  1022. Capture1();
  1023. this.Close();
  1024. }
  1025. public void Capture1()
  1026. {
  1027. var bitmap = documentWorkspace.CompositionSurface.CreateAliasedBitmap();
  1028. var mode = Startup.instance.configModel.CaptureSaveMode;
  1029. var r = appWorkspace.DisplayShoot(bitmap, this.documentWorkspace.GraphicsList, checkBox1.Checked, mode);
  1030. if (!r) return;
  1031. }
  1032. /// <summary>
  1033. /// 白平衡点击
  1034. /// </summary>
  1035. /// <param name="sender"></param>
  1036. /// <param name="e"></param>
  1037. public void whiteBalance_Click(object sender, EventArgs e)
  1038. {
  1039. m_camera.WhiteBalanceMode = 2;
  1040. }
  1041. /// <summary>
  1042. /// 速度点击
  1043. /// </summary>
  1044. void CopyDetail_DropDownItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
  1045. {
  1046. foreach (ToolStripMenuItem item in shootActionsStrip.speedButton.DropDownItems)
  1047. {
  1048. if (item.Name != e.ClickedItem.Name)
  1049. {
  1050. item.Checked = false;
  1051. }
  1052. else
  1053. {
  1054. item.Checked = true;
  1055. this.shootActionsStrip.speedButton.Text = e.ClickedItem.Text;
  1056. }
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// 曝光时间被点击
  1061. /// </summary>
  1062. /// <param name="sender"></param>
  1063. /// <param name="e"></param>
  1064. public void exposureTimeButton_Click(object sender, EventArgs e)
  1065. {
  1066. m_camera.AutoExposure = 1;
  1067. }
  1068. /// <summary>
  1069. /// 最佳被点击
  1070. /// </summary>
  1071. /// <param name="sender"></param>
  1072. /// <param name="e"></param>
  1073. public void bestButton_Click(object sender, EventArgs e)
  1074. {
  1075. if (this.shootActionsStrip == null || this.shootActionsStrip.bestButton == null)
  1076. return;
  1077. // TUCamAPI.TUCAM_Capa_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_ATEXPOSURE_MODE, 0);
  1078. // TUCamAPI.TUCAM_Prop_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_EXPOSURETM, (double)(20000000 / 1000.0f), 0);
  1079. //MessageBox.Show("临时调整曝光时长为20000000us");
  1080. bestButtonEnable = !bestButtonEnable;
  1081. var button = this.shootActionsStrip.bestButton;
  1082. button.Checked = !button.Checked;
  1083. if (maxAndMinButtonEnable)
  1084. {
  1085. maxAndMinButtonEnable = !maxAndMinButtonEnable;
  1086. this.shootActionsStrip.maxAndMinButton.Checked = false;
  1087. }
  1088. }
  1089. /// <summary>
  1090. /// 最大最小被点击
  1091. /// </summary>
  1092. /// <param name="sender"></param>
  1093. /// <param name="e"></param>
  1094. public void maxAndMinButton_Click(object sender, EventArgs e)
  1095. {
  1096. if (this.shootActionsStrip == null || this.shootActionsStrip.maxAndMinButton == null)
  1097. return;
  1098. // TUCamAPI.TUCAM_Capa_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_ATWBALANCE, 0);
  1099. // TUCamAPI.TUCAM_Prop_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_CHNLGAIN, 1220.0, 2);
  1100. //MessageBox.Show("最大最小被点击临时变绿色");
  1101. maxAndMinButtonEnable = !maxAndMinButtonEnable;
  1102. var button = this.shootActionsStrip.maxAndMinButton;
  1103. button.Checked = !button.Checked;
  1104. if (bestButtonEnable)
  1105. {
  1106. bestButtonEnable = !bestButtonEnable;
  1107. this.shootActionsStrip.bestButton.Checked = false;
  1108. }
  1109. }
  1110. /// <summary>
  1111. /// 原始状态被点击
  1112. /// </summary>
  1113. /// <param name="sender"></param>
  1114. /// <param name="e"></param>
  1115. public void originalStateButton_Click(object sender, EventArgs e)
  1116. {
  1117. if (this.shootActionsStrip == null || this.shootActionsStrip.originalStateButton == null)//1012###19464
  1118. return;
  1119. bestButtonEnable = false;
  1120. maxAndMinButtonEnable = false;
  1121. gamma45ButtonEnable = false;
  1122. this.shootActionsStrip.bestButton.Checked = false;
  1123. this.shootActionsStrip.maxAndMinButton.Checked = false;
  1124. this.shootActionsStrip.gamma45Button.Checked = false;
  1125. }
  1126. /// <summary>
  1127. /// 伽马0.45被点击
  1128. /// </summary>
  1129. /// <param name="sender"></param>
  1130. /// <param name="e"></param>
  1131. public void gamma45Button_Click(object sender, EventArgs e)
  1132. {
  1133. if (this.shootActionsStrip == null || this.shootActionsStrip.gamma45Button == null)
  1134. return;
  1135. gamma45ButtonEnable = !gamma45ButtonEnable;
  1136. var button = this.shootActionsStrip.gamma45Button;
  1137. button.Checked = !button.Checked;
  1138. }
  1139. /// <summary>
  1140. /// 曝光不足被点击
  1141. /// </summary>
  1142. /// <param name="sender"></param>
  1143. /// <param name="e"></param>
  1144. public void underexposedButton_Click(object sender, EventArgs e)
  1145. {
  1146. underexposedButtonEnable = !underexposedButtonEnable;
  1147. //if (overexposedButton)
  1148. //{
  1149. // overexposedButton = !overexposedButton;
  1150. // this.shootActionsStrip.overexposedButton.Checked = false;
  1151. //}
  1152. var button = (ToolStripButton)sender;
  1153. button.Checked = !button.Checked;
  1154. }
  1155. /// <summary>
  1156. /// 预览属性按钮被点击
  1157. /// </summary>
  1158. /// <param name="sender"></param>
  1159. /// <param name="e"></param>
  1160. public void propertiesButton_Click(object sender, EventArgs e)
  1161. {
  1162. Form form = Application.OpenForms["PropertiesSettingDialog"];//尝试获取已经弹出的窗口对象
  1163. if (form == null || form.IsDisposed)
  1164. {
  1165. if (fmSetting != null)
  1166. {
  1167. fmSetting.Dispose();
  1168. fmSetting = null;
  1169. }
  1170. form = null;
  1171. fmSetting = new PropertiesSettingDialog(this);
  1172. //最佳事件
  1173. fmSetting.displayCameraControl.button2.Click += new System.EventHandler(this.bestButton_Click);
  1174. //最小最大点击事件
  1175. fmSetting.displayCameraControl.button3.Click += new System.EventHandler(this.maxAndMinButton_Click);
  1176. //原始状态事件
  1177. fmSetting.displayCameraControl.button5.Click += new System.EventHandler(this.originalStateButton_Click);
  1178. //伽马0.45事件
  1179. fmSetting.displayCameraControl.button4.Click += new System.EventHandler(this.gamma45Button_Click);
  1180. fmSetting.FormClosing += Fm_FormClosing;
  1181. //fm.ShowDialog();
  1182. FloatingFormMethod.ShowFloatForm(form, form == null ? fmSetting/*没有弹出的窗口对象则创建*/ : null, this.appWorkspace);
  1183. }
  1184. else
  1185. {
  1186. form.Activate();
  1187. form.WindowState = FormWindowState.Normal;
  1188. }
  1189. }
  1190. private void Fm_FormClosing(object sender, FormClosingEventArgs e)
  1191. {
  1192. if (fmSetting != null)//0915###避免引起异常问题
  1193. {
  1194. fmSetting.Dispose();
  1195. fmSetting = null;
  1196. }
  1197. }
  1198. /// <summary>
  1199. /// 过曝光被点击
  1200. /// </summary>
  1201. /// <param name="sender"></param>
  1202. /// <param name="e"></param>
  1203. public void overexposedButton_Click(object sender, EventArgs e)
  1204. {
  1205. overexposedButton = !overexposedButton;
  1206. //if (underexposedButtonEnable)
  1207. //{
  1208. // underexposedButtonEnable = !underexposedButtonEnable;
  1209. // this.shootActionsStrip.underexposedButton.Checked = false;
  1210. //}
  1211. var button = (ToolStripButton)sender;
  1212. button.Checked = !button.Checked;
  1213. }
  1214. /// <summary>
  1215. /// 十字线按钮
  1216. /// </summary>
  1217. /// <param name="sender"></param>
  1218. /// <param name="e"></param>
  1219. public void crossButton_Click(object sender, EventArgs e)
  1220. {
  1221. if (this.documentWorkspace != null)
  1222. {
  1223. this.documentWorkspace.GridCrossCurveEnabled = !this.documentWorkspace.GridCrossCurveEnabled;
  1224. this.documentWorkspace.Refresh();
  1225. var button = (ToolStripButton)sender;
  1226. button.Checked = !button.Checked;
  1227. }
  1228. }
  1229. /// <summary>
  1230. /// 网格按钮
  1231. /// </summary>
  1232. /// <param name="sender"></param>
  1233. /// <param name="e"></param>
  1234. public void gridButton_Click(object sender, EventArgs e)
  1235. {
  1236. if (this.documentWorkspace != null)
  1237. {
  1238. this.documentWorkspace.GridLineEnabled = !this.documentWorkspace.GridLineEnabled;
  1239. this.documentWorkspace.Refresh();
  1240. var button = (ToolStripButton)sender;
  1241. button.Checked = !button.Checked;
  1242. }
  1243. }
  1244. /// <summary>
  1245. /// 方形按钮
  1246. /// </summary>
  1247. /// <param name="sender"></param>
  1248. /// <param name="e"></param>
  1249. public void squareButton_Click(object sender, EventArgs e)
  1250. {
  1251. if (this.documentWorkspace != null)
  1252. {
  1253. this.documentWorkspace.GridRectangleEnabled = !this.documentWorkspace.GridRectangleEnabled;
  1254. this.documentWorkspace.Refresh();
  1255. var button = (ToolStripButton)sender;
  1256. button.Checked = !button.Checked;
  1257. }
  1258. }
  1259. /// <summary>
  1260. /// 圆形按钮
  1261. /// </summary>
  1262. /// <param name="sender"></param>
  1263. /// <param name="e"></param>
  1264. public void roundButton_Click(object sender, EventArgs e)
  1265. {
  1266. if (this.documentWorkspace != null)
  1267. {
  1268. this.documentWorkspace.GridRoundEnabled = !this.documentWorkspace.GridRoundEnabled;
  1269. this.documentWorkspace.Refresh();
  1270. var button = (ToolStripButton)sender;
  1271. button.Checked = !button.Checked;
  1272. }
  1273. }
  1274. /// <summary>
  1275. /// 实际大小按钮
  1276. /// </summary>
  1277. /// <param name="sender"></param>
  1278. /// <param name="e"></param>
  1279. public void actualSizeButton_Click(object sender, EventArgs e)
  1280. {
  1281. if (this.documentWorkspace.CompositionSurface != null)
  1282. {
  1283. this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
  1284. this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
  1285. this.isFitToWindow = false;
  1286. }
  1287. }
  1288. /// <summary>
  1289. /// 合适大小按钮
  1290. /// </summary>
  1291. /// <param name="sender"></param>
  1292. /// <param name="e"></param>
  1293. public void zoomToWindowButton_Click(object sender, EventArgs e)
  1294. {
  1295. if (this.documentWorkspace.CompositionSurface != null)
  1296. {
  1297. this.documentWorkspace.ZoomToWindow();
  1298. this.isFitToWindow = true;
  1299. }
  1300. }
  1301. /// <summary>
  1302. /// 全屏显示按钮
  1303. /// </summary>
  1304. /// <param name="sender"></param>
  1305. /// <param name="e"></param>
  1306. public void fullScreenButton_Click(object sender, EventArgs e)
  1307. {
  1308. var button = (ToolStripButton)sender;
  1309. if (this.WindowState == FormWindowState.Normal)
  1310. {
  1311. this.WindowState = FormWindowState.Maximized;
  1312. button.Checked = true;
  1313. }
  1314. else
  1315. {
  1316. this.WindowState = FormWindowState.Normal;
  1317. button.Checked = false;
  1318. }
  1319. if (isFitToWindow && this.documentWorkspace.CompositionSurface != null)
  1320. this.documentWorkspace.ZoomToWindow();
  1321. this.Invalidate();
  1322. }
  1323. /// <summary>
  1324. /// 缩小按钮
  1325. /// </summary>
  1326. /// <param name="sender"></param>
  1327. /// <param name="e"></param>
  1328. public void zoomOutButton_Click(object sender, EventArgs e)
  1329. {
  1330. if (this.documentWorkspace.CompositionSurface != null)
  1331. {
  1332. this.documentWorkspace.ZoomOut();
  1333. }
  1334. }
  1335. /// <summary>
  1336. /// 放大按钮
  1337. /// </summary>
  1338. /// <param name="sender"></param>
  1339. /// <param name="e"></param>
  1340. public void zoomInButton_Click(object sender, EventArgs e)
  1341. {
  1342. if (this.documentWorkspace.CompositionSurface != null)
  1343. {
  1344. this.documentWorkspace.ZoomIn();
  1345. }
  1346. }
  1347. private void CameraPreviewDialog_FormClosing(object sender, FormClosingEventArgs e)
  1348. {
  1349. AutoFocusWorkflow.Stop();
  1350. Thread.Sleep(50);
  1351. Dispose();
  1352. ConfigModel.OnCameraNameExChanged -= OnNameChange;
  1353. tmrUpdate.Enabled = false;
  1354. }
  1355. public new void Dispose()
  1356. {
  1357. //m_Stage.Close();
  1358. m_Stage.RemoveApp(this);
  1359. CameraManager.FrameCallback -= CallbackDraw;
  1360. if (m_matClone != null)
  1361. {
  1362. m_matClone.Dispose();
  1363. }
  1364. if (fmSetting != null)
  1365. fmSetting.Close();
  1366. if (this.documentWorkspace != null && !this.documentWorkspace.IsDisposed)
  1367. this.documentWorkspace.Dispose();
  1368. if (this.document != null)
  1369. this.document.Dispose();
  1370. base.Dispose();
  1371. GC.Collect();
  1372. }
  1373. /// <summary>
  1374. /// 判断颜色灰度值 如果是true 则设置为灰色
  1375. /// </summary>
  1376. /// <param name="isGray"></param>
  1377. public void SetColorModel(bool isGray)
  1378. {
  1379. //m5pro.SetColorModel(isGray);
  1380. }
  1381. /// <summary>
  1382. /// 选择相机设置
  1383. /// </summary>
  1384. /// <param name="sender"></param>
  1385. /// <param name="e"></param>
  1386. private void cmbConfigs_SelectedIndexChanged(object sender, EventArgs e)
  1387. {
  1388. ComboBox cb = (ComboBox)sender;
  1389. Startup.instance.cameraParamModel = m_cameraConfig.GetCameraParamModel(cb.SelectedItem.ToString());
  1390. m_cameraParamModel = Startup.instance.cameraParamModel;
  1391. Startup.instance.configModel.CameraConfig = cb.SelectedItem.ToString();
  1392. string configXml = XmlSerializeHelper.XmlSerialize<ConfigModel>(Startup.instance.configModel);
  1393. string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\Config.xml";
  1394. FileOperationHelper.WriteStringToFile(configXml, filePath, FileMode.Create);
  1395. m_cameraConfig.CameraParamInit();
  1396. }
  1397. /// <summary>
  1398. /// 删除相机设置
  1399. /// </summary>
  1400. /// <param name="sender"></param>
  1401. /// <param name="e"></param>
  1402. private void btnConfigDel_Click(object sender, EventArgs e)
  1403. {
  1404. if (this.cmbConfigs.SelectedItem != null)
  1405. {
  1406. if (this.cmbConfigs.SelectedItem.ToString().Equals("CameraParam"))
  1407. {
  1408. MessageBox.Show(PdnResources.GetString("Menu.hedefaultconfiguratallowdeletion.Text"), PdnResources.GetString("Menu.ensure.text"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1409. return;
  1410. }
  1411. DialogResult result = MessageBox.Show(PdnResources.GetString("Menu.suredeletit.Text") + "?", PdnResources.GetString("Menu.Tips.text"), MessageBoxButtons.OKCancel);
  1412. if (result == DialogResult.OK)
  1413. {
  1414. CameraConfigs.GetInstance().Delete(CameraConfigs.GetInstance().CurrentConfigFileName);
  1415. initCmbConfigs();
  1416. }
  1417. }
  1418. }
  1419. /// <summary>
  1420. /// 用于修改全屏按钮状态
  1421. /// </summary>
  1422. /// <param name="sender"></param>
  1423. /// <param name="e"></param>
  1424. private void Form_Resize(object sender, EventArgs e)
  1425. {
  1426. if (this.WindowState == FormWindowState.Normal)
  1427. {
  1428. baseActionsStrip.fullScreenButton.Checked = false;
  1429. }
  1430. else
  1431. {
  1432. baseActionsStrip.fullScreenButton.Checked = true;
  1433. }
  1434. if (isFitToWindow && this.documentWorkspace.CompositionSurface != null)
  1435. this.documentWorkspace.ZoomToWindow();
  1436. this.Invalidate();
  1437. }
  1438. /// <summary>
  1439. /// 获取当前标尺并换算为微米
  1440. /// </summary>
  1441. public void getRulerMicronRatio()
  1442. {
  1443. Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out this.micronRatio);
  1444. this.documentWorkspace.MicronRatio = this.micronRatio;
  1445. }
  1446. /// <summary>
  1447. /// 标尺下拉菜单选项改变
  1448. /// </summary>
  1449. /// <param name="sender"></param>
  1450. /// <param name="e"></param>
  1451. private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
  1452. {
  1453. //getRulerMicronRatio();
  1454. //this.documentWorkspace.Refresh();
  1455. }
  1456. /// <summary>
  1457. /// 理论比例尺按钮
  1458. /// </summary>
  1459. /// <param name="sender"></param>
  1460. /// <param name="e"></param>
  1461. public void theoreticalScaleButton_Click(object sender, EventArgs e)
  1462. {
  1463. if (theoreticalScaleDialog == null)
  1464. {
  1465. theoreticalScaleDialog = new TheoreticalScaleDialog(this.appWorkspace, MeasureActionMenu.ArrayList1, MeasureActionMenu.ArrayList2,
  1466. MeasureActionMenu.ArrayList3);
  1467. theoreticalScaleDialog.button7.Click += new EventHandler(button7_Click);
  1468. //theoreticalScaleDialog.FormClosing += new FormClosingEventHandler(theoreticalScaleDialog_FormClosing);
  1469. }
  1470. theoreticalScaleDialog.StartPosition = FormStartPosition.CenterParent;
  1471. theoreticalScaleDialog.ShowDialog();
  1472. }
  1473. /// <summary>
  1474. /// 理论比例尺窗口关闭
  1475. /// </summary>
  1476. /// <param name="sender"></param>
  1477. /// <param name="e"></param>
  1478. private void theoreticalScaleDialog_FormClosing(object sender, FormClosingEventArgs e)
  1479. {
  1480. int selectedNum = this.comboBox1.SelectedIndex;
  1481. this.comboBox1.DataSource = null;
  1482. bs = new BindingSource();
  1483. bs.DataSource = micRulersDictonary;
  1484. this.comboBox1.DataSource = bs;
  1485. this.comboBox1.DisplayMember = "Key";
  1486. this.comboBox1.ValueMember = "Value";
  1487. this.comboBox1.SelectedIndex = selectedNum;
  1488. this.comboBox1.Refresh();
  1489. }
  1490. /// <summary>
  1491. /// 理论比例尺保存
  1492. /// </summary>
  1493. /// <param name="sender"></param>
  1494. /// <param name="e"></param>
  1495. private void button7_Click(object sender, EventArgs e)
  1496. {
  1497. }
  1498. private double GrayHistogramArea(Bitmap bitmap)
  1499. {
  1500. Mat image = PaintDotNet.Camera.Tools.ToMat(bitmap);
  1501. if (image.Channels() == 3)
  1502. {
  1503. Cv2.CvtColor(image, image, ColorConversionCodes.BGR2GRAY);
  1504. }
  1505. int[] list = new int[256];
  1506. var date = image.ToBytes();
  1507. foreach (int value in date)
  1508. {
  1509. list[value]++;
  1510. }
  1511. return 0;
  1512. }
  1513. public static Mat ToGrayMat(Bitmap bitmap)
  1514. {
  1515. Mat image = PaintDotNet.Camera.Tools.ToMat(bitmap);
  1516. if (image.Channels() == 3)
  1517. {
  1518. Cv2.CvtColor(image, image, ColorConversionCodes.BGR2GRAY);
  1519. }
  1520. return image;
  1521. }
  1522. /// <summary>
  1523. /// 高斯距离
  1524. /// </summary>
  1525. /// <param name="im"></param>
  1526. public static double GaussianDiff(Mat im)
  1527. {
  1528. if (im.Channels() == 3)
  1529. {
  1530. Cv2.CvtColor(im, im, ColorConversionCodes.BGR2GRAY);
  1531. }
  1532. Mat blurred = new Mat();
  1533. Cv2.GaussianBlur(im, blurred, new OpenCvSharp.Size(7, 7), 1.66);
  1534. Cv2.Absdiff(im, blurred, blurred);
  1535. return Cv2.Mean(blurred)[0];
  1536. }
  1537. public static double Sobel(Bitmap bitmap)
  1538. {
  1539. var graymat = ToGrayMat(bitmap);
  1540. Mat imageSobel = new Mat();
  1541. Cv2.Sobel(graymat, imageSobel, 2, 1, 1);
  1542. return Cv2.Mean(imageSobel)[0];
  1543. }
  1544. /// <summary>
  1545. /// 获取输入图片的清晰度
  1546. /// </summary>
  1547. /// <param name="bitmap"></param>
  1548. /// <returns></returns>
  1549. private static double getMeanValueOfBitmap(Bitmap bitmap)
  1550. {
  1551. Mat converted = PaintDotNet.Camera.Tools.ToMat(bitmap);
  1552. Mat imageGrey = new Mat();
  1553. Mat imageSobel = new Mat();
  1554. try
  1555. {
  1556. Cv2.CvtColor(converted, imageGrey, ColorConversionCodes.RGB2GRAY);
  1557. }
  1558. catch (Exception)
  1559. {
  1560. imageGrey = converted;
  1561. }
  1562. Mat meanValueImage = new Mat();
  1563. Mat meanStdValueImage = new Mat();
  1564. //求灰度图像的标准差 值越大越好
  1565. OpenCvSharp.Cv2.MeanStdDev(imageGrey, meanValueImage, meanStdValueImage);
  1566. return meanStdValueImage.At<double>(0, 0);
  1567. }
  1568. #region AutoFocus
  1569. private static bool _focusing;
  1570. public static Func<Bitmap> CurrentImage;
  1571. private void autoFocusButton_Click(object sender, EventArgs e)
  1572. {
  1573. m_camera.AutoExposure = 0;
  1574. _focusing = true;
  1575. new Thread(new ThreadStart(() =>
  1576. {
  1577. var result = AutoFocusWorkflow.AutoFocus(m_Stage, CurrentImage);
  1578. if (string.IsNullOrEmpty(result))
  1579. {
  1580. MessageBox.Show("聚焦成功");
  1581. }
  1582. else
  1583. {
  1584. MessageBox.Show(result);
  1585. }
  1586. _focusing = false;
  1587. })).Start();
  1588. }
  1589. private void tmrUpdate_Tick(object sender, EventArgs e)
  1590. {
  1591. if (_focusing)
  1592. progressBar1.Value = (int)(Math.Min(AutoFocusWorkflow.CurrentValue, progressBar1.Maximum));
  1593. else progressBar1.Value = 0;
  1594. // setBarValue((int)m_MeanValue);
  1595. }
  1596. private static double m_resetStep1 = 0;
  1597. private void btnRestStep_Click(object sender, EventArgs e)
  1598. {
  1599. if (_focusing) return;
  1600. if (m_resetStep1 != 0)
  1601. {
  1602. m_Stage.UpTo(m_resetStep1);
  1603. m_resetStep1 = 0;
  1604. }
  1605. }
  1606. #endregion
  1607. private void DocumentWorkspace_ScaleFactorChanged(object sender, EventArgs e)
  1608. {
  1609. this.gainNumbertxt.Text = Math.Round((this.documentWorkspace.ScaleRatio * 100), 2) + "%";
  1610. }
  1611. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  1612. {
  1613. if (((ComboBox)sender).SelectedItem != null)
  1614. {
  1615. if (comboBox_Value != ((ComboBox)sender).SelectedItem.ToString())
  1616. {
  1617. comboBox_Value = ((ComboBox)sender).SelectedItem.ToString();
  1618. //comboBox_Change = true;
  1619. List<mic_rulers> list = Startup.instance.mic_rulersAll;
  1620. if (list != null)
  1621. {
  1622. for (int ruleI = 0; ruleI < list.Count; ruleI++)
  1623. {
  1624. mic_rulers rule = list[ruleI];
  1625. if (rule.ruler_name.Equals(((ComboBox)sender).SelectedItem.ToString()))
  1626. {
  1627. int rulid = rule.id;
  1628. string rulerName2 = rule.ruler_name;
  1629. if (rulid != Startup.instance.configModel.RulerId)
  1630. {
  1631. //设置当前选中标尺
  1632. this.appWorkspace.SetActiveRulerIndex(((ComboBox)sender).SelectedIndex);
  1633. //comboBox_Change = false;
  1634. this.documentWorkspace.rules = Startup.instance.rules;
  1635. }
  1636. break;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. getRulerMicronRatio();
  1642. this.documentWorkspace.Refresh();
  1643. }
  1644. }
  1645. public void OnUpdatePosition()
  1646. {
  1647. try
  1648. {
  1649. this.Invoke(new Action(() =>
  1650. {
  1651. lblZPos.Text = m_Stage.Z.ToString("f2");
  1652. }));
  1653. }
  1654. catch { }
  1655. }
  1656. public void OnTimeoutConnect()
  1657. {
  1658. MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
  1659. }
  1660. public void OnErrorSend()
  1661. {
  1662. MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text"));
  1663. }
  1664. private void CameraPreviewDialog_Load(object sender, EventArgs e)
  1665. {
  1666. tmrUpdate.Enabled = true;
  1667. progressBar1.Maximum = 65;
  1668. checkBox1.Checked = Preview2Context.P2Config.CaptureRuler;
  1669. OnNameChange();
  1670. ConfigModel.OnCameraNameExChanged += OnNameChange;
  1671. }
  1672. void OnNameChange()
  1673. {
  1674. this.Text = DailogTitle + "-" + m_camera.GetNameEx();
  1675. }
  1676. private void btnZUp_Click(object sender, EventArgs e)
  1677. {
  1678. var value = (double)numZLen.Value;
  1679. m_Stage.Up(value);
  1680. }
  1681. private void btuZDown_Click(object sender, EventArgs e)
  1682. {
  1683. var value = (double)numZLen.Value;
  1684. m_Stage.Up(-value);
  1685. }
  1686. private void btnZClear_Click(object sender, EventArgs e)
  1687. {
  1688. m_Stage.FreeZ();
  1689. m_Stage.ClearPosZ();
  1690. }
  1691. /// <summary>
  1692. /// 判断鼠标是否在预览窗口内
  1693. /// </summary>
  1694. /// <returns></returns>
  1695. public bool IsLeave()
  1696. {
  1697. try
  1698. {
  1699. Point point = this.PointToClient(Control.MousePosition);
  1700. isLeave = point.X > this.ClientSize.Width || point.X < 0 || point.Y > this.ClientSize.Height || point.Y < 0 ? false : true;
  1701. }
  1702. catch (Exception)
  1703. {
  1704. isLeave = false;
  1705. }
  1706. return isLeave;
  1707. }
  1708. private void CameraPreviewDialog_Activated(object sender, EventArgs e)
  1709. {
  1710. initCmbConfigs();
  1711. }
  1712. private void checkBox1_Click(object sender, EventArgs e)
  1713. {
  1714. Preview2Context.P2Config.CaptureRuler = checkBox1.Checked;
  1715. }
  1716. }
  1717. }