CameraPreviewDialog.cs 78 KB

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