using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using OpenCvSharp; using OpenCvSharp.Extensions; using PaintDotNet.Adjust; using PaintDotNet.Adjust.BaseImage; using PaintDotNet.Base.SettingModel; using TUCAMAPI; using TUCamera; namespace PaintDotNet.ImageCollect { internal class CameraRealTimeStitchingDialog : FloatingToolForm { #region /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text"); this.btnWorking.Text = PdnResources.GetString("Menu.Started.text"); this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text"); this.groupBox4.Text = PdnResources.GetString("Menu.Puzzleeffect.text"); this.Text = PdnResources.GetString("Menu.imagecapture.Reatimldexpansion.Reaestitching.text"); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.btnCreatImgFile = new System.Windows.Forms.Button(); this.btnWorking = new System.Windows.Forms.Button(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.picPreview = new System.Windows.Forms.PictureBox(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.picMatch = new System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); this.groupBox3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit(); this.groupBox4.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picMatch)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox1.Controls.Add(this.btnCreatImgFile); this.groupBox1.Controls.Add(this.btnWorking); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(1217, 87); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // btnCreatImgFile // this.btnCreatImgFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnCreatImgFile.Location = new System.Drawing.Point(1106, 28); this.btnCreatImgFile.Name = "btnCreatImgFile"; this.btnCreatImgFile.Size = new System.Drawing.Size(92, 34); this.btnCreatImgFile.TabIndex = 1; this.btnCreatImgFile.Text = "创建图片"; this.btnCreatImgFile.UseVisualStyleBackColor = true; this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click); // // btnWorking // this.btnWorking.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnWorking.Location = new System.Drawing.Point(993, 28); this.btnWorking.Name = "btnWorking"; this.btnWorking.Size = new System.Drawing.Size(89, 34); this.btnWorking.TabIndex = 0; this.btnWorking.Text = "开始"; this.btnWorking.UseVisualStyleBackColor = true; this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click); // // groupBox3 // this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.groupBox3.Controls.Add(this.picPreview); this.groupBox3.Location = new System.Drawing.Point(12, 105); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(466, 517); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "预览"; // // picPreview // this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.picPreview.Location = new System.Drawing.Point(20, 74); this.picPreview.Name = "picPreview"; this.picPreview.Size = new System.Drawing.Size(429, 364); this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.picPreview.TabIndex = 0; this.picPreview.TabStop = false; // // groupBox4 // this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox4.Controls.Add(this.picMatch); this.groupBox4.Location = new System.Drawing.Point(484, 105); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(745, 517); this.groupBox4.TabIndex = 4; this.groupBox4.TabStop = false; this.groupBox4.Text = "拼图效果"; // // picMatch // this.picMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.picMatch.Location = new System.Drawing.Point(17, 74); this.picMatch.Name = "picMatch"; this.picMatch.Size = new System.Drawing.Size(709, 416); this.picMatch.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.picMatch.TabIndex = 1; this.picMatch.TabStop = false; // // CameraRealTimeStitchingDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1241, 634); this.Controls.Add(this.groupBox4); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox1); this.Name = "CameraRealTimeStitchingDialog"; this.Text = "图像实时拼接"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraRealTimeStitchingDialog_FormClosing); this.Load += new System.EventHandler(this.CameraRealTimeStitchingDialog_Load); this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.groupBox3, 0); this.Controls.SetChildIndex(this.groupBox4, 0); this.groupBox1.ResumeLayout(false); this.groupBox3.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit(); this.groupBox4.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.picMatch)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btnCreatImgFile; private System.Windows.Forms.Button btnWorking; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.PictureBox picPreview; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.PictureBox picMatch; #endregion private AppWorkspace m_appWorkspace; private TUCamera.TUCamera m_camera; private CameraParamModel m_cameraParamModel; private Mat m_mat; private Thread m_stitchingThread; private bool is_depth; private Mat[] Coll_Ori = new Mat[2]; private Mat m_merge; private string m_filePath = Application.StartupPath + "\\Picture\\"; private bool m_isWorking; public CameraRealTimeStitchingDialog(AppWorkspace appWorkspace) { m_appWorkspace = appWorkspace; InitializeComponent(); InitializeLanguageText(); } private void CameraRealTimeStitchingDialog_Load(object sender, EventArgs e) { m_camera = TUCameraManager.GetInstance().GetCurrentCamera(); m_cameraParamModel = Startup.instance.cameraParamModel; CameraConfigs cameraConfig = CameraConfigs.GetInstance(); cameraConfig.CameraParamInit(); m_camera.m_bufHandler += CallbackDraw; m_camera.StartWaitForFrame(); } private byte[] m_pbuf; /// /// 绘制 /// /// /// public void CallbackDraw(Bitmap bitmap) { m_mat = BitmapConverter.ToMat(bitmap); picPreview.Image = (Image)bitmap.Clone(); } private void btnWorking_Click(object sender, EventArgs e) { if (m_isWorking) { m_isWorking = false; btnWorking.Text = PdnResources.GetString("Menu.Started.text"); return; } m_isWorking = true; btnWorking.Text = PdnResources.GetString("Menu.stop.text"); m_merge = null; m_stitchingThread = new Thread(new ThreadStart(WaitForStitchingThreadEntry)); m_stitchingThread.Start(); } /// /// 图像拼接 /// private void WaitForStitchingThreadEntry() { while (m_isWorking) { if (m_mat == null) { Thread.Sleep(100); continue; } Mat mat = m_mat;//.Clone(); if (m_merge == null) { AdjustIntent.InitMatchPicOneByOne(); } Rect rect; var result = AdjustIntent.MatchPicRealtime(mat, m_merge, out bool error, out rect); if (!error) { m_merge = result; } else { Console.WriteLine("拼图失败"); } var tmp = m_merge.Clone(); Cv2.Rectangle(tmp, rect, Scalar.Lime, 5, LineTypes.Link4, 4); picMatch.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(tmp); picMatch.Invalidate(); GC.Collect(); } } private void btnCreatImgFile_Click(object sender, EventArgs e) { if (m_merge == null) { MessageBox.Show(PdnResources.GetString("Menu.mageMosaicimagehanotbeengeneratedyet.Text")); return; } Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_merge); DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace(); Document document = Document.FromImage(bitmap); dw.Document = document; dw.xmlSaveModel = Startup.instance.ruleDB; dw.InitRulerInfo(); dw.fileText = "1"; m_appWorkspace.ActiveDocumentWorkspace = dw; } private void CameraRealTimeStitchingDialog_FormClosing(object sender, FormClosingEventArgs e) { if (m_stitchingThread != null) { m_isWorking = false; m_stitchingThread.Join(); m_stitchingThread.Abort(); m_stitchingThread.DisableComObjectEagerCleanup(); } m_camera.m_bufHandler -= CallbackDraw; m_camera.StopWaitForFrame(); if (m_mat != null && !m_mat.IsDisposed) { m_mat.Dispose(); } if (m_merge != null && !m_merge.IsDisposed) { m_merge.Dispose(); } GC.Collect(); } } }