From ebcc1d53f14112363bbf539bcaf0202aadcdc9d7 Mon Sep 17 00:00:00 2001
From: C3032 <1057644574@qq.com>
Date: 星期一, 13 四月 2026 12:58:58 +0800
Subject: [PATCH] 2D取图计数功能完成

---
 LB_VisionProcesses/Cameras/2DCameraForm.cs |  301 +++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 236 insertions(+), 65 deletions(-)

diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index 107eba5..2409faf 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -78,11 +78,25 @@
 
         UserPictureBox onlinePictureBox { get; set; }
         private System.Windows.Forms.Timer updateTimer;
-        Total total = new Total { iImageCount = 0, iScanCount = 0 };
+        Total total = new Total { iImageCount = 0, iScanCount = 0, ImagesPerTyre = 1 };
         DateTime startGrabtime = DateTime.Now;
+        
+        // 鍔ㄦ�佹坊鍔犵殑澧炵泭涓嬫媺妗�
+        private ComboBox cmbGain;
 
         private void CameraForm_Load(object sender, EventArgs e)
         {
+            // 鍒濆鍖栧鐩婁笅鎷夋
+            cmbGain = new ComboBox();
+            cmbGain.Visible = false; // 榛樿闅愯棌
+            cmbGain.DropDownStyle = ComboBoxStyle.DropDownList;
+            cmbGain.Size = txtGain.Size;
+            cmbGain.Location = txtGain.Location;
+            cmbGain.Font = txtGain.Font;
+            cmbGain.SelectedIndexChanged += CmbGain_SelectedIndexChanged;
+            this.txtGain.Parent.Controls.Add(cmbGain);
+            cmbGain.BringToFront();
+
             // 璁剧疆涓�涓畾鏃跺櫒锛屾瘡 100 姣瑙﹀彂涓�娆�
             updateTimer = new System.Windows.Forms.Timer();
             updateTimer.Interval = 1000;  // 璁剧疆瀹氭椂鍣ㄩ棿闅斾负 1000 姣
@@ -124,6 +138,16 @@
                 if (camConfig.OutputImage != null && camConfig.OutputImage is Bitmap bitmap)
                     onlinePictureBox.Image = bitmap;
                 lblCapTime.Text = $"{camConfig.RunTime}ms";
+            }
+        }
+
+        private void CmbGain_SelectedIndexChanged(object sender, EventArgs e)
+        {
+            if (camera != null && camera is LBCamera && cmbGain.Visible)
+            {
+                // LBCamera SetGain expects double which is cast to int for enum value
+                // ComboBox index corresponds directly to EnumAnalogGain value (0-4)
+                camera.SetGain((double)cmbGain.SelectedIndex);
             }
         }
 
@@ -232,31 +256,71 @@
             // 灏濊瘯灏嗚緭鍏ュ瓧绗︿覆杞崲涓烘灇涓惧��
             if (Enum.TryParse(cmbBrand.Text, true, out CameraBrand brand))
             {
-                if (camera != null)
+                string selectedSN = cmbSN.Text.ToString();
+                bool isReusingExisting = false;
+
+                // 1. 灏濊瘯浠庣幇鏈夊瓧鍏镐腑鏌ユ壘宸插垵濮嬪寲鐨勭浉鏈�
+                if (dicCameras != null && dicCameras.ContainsKey(selectedSN))
+                {
+                    var existingCamera = dicCameras[selectedSN];
+                    // 纭繚鍝佺墝鍖归厤
+                    if (existingCamera.Brand == brand)
+                    {
+                        camera = existingCamera;
+                        isReusingExisting = true;
+                    }
+                }
+
+                // 2. 濡傛灉娌℃湁澶嶇敤鐜版湁鐩告満锛屼笖褰撳墠camera瀵硅薄涓嶆槸鎴戜滑瑕佸鐢ㄧ殑瀵硅薄锛屽垯娓呯悊鏃у璞�
+                if (!isReusingExisting && camera != null)
                 {
                     camera.ImageGrabbed -= GetImageBllComplete;
                     camera.Dispose();
+                    camera = null;
                 }
 
-                // 浣跨敤 switch 璇彞鏉ュ垽鏂灇涓惧��
-                switch (brand)
+                // 3. 濡傛灉娌℃湁澶嶇敤锛屽垯鍒涘缓鏂板疄渚�
+                if (!isReusingExisting)
                 {
-                    case CameraBrand.LBCamera:
-                        camera = new LBCamera();
-                        break;
-                    case CameraBrand.HRCamera:
-                        camera = new HRCamera();
-                        break;
-                    default:
-                        Debug.WriteLine($"銆恵DateTime.Now:HH:mm:ss.fff}銆戞湭鐭ュ搧鐗�");
-                        return;
+                    // 浣跨敤 switch 璇彞鏉ュ垽鏂灇涓惧��
+                    switch (brand)
+                    {
+                        case CameraBrand.LBCamera:
+                            camera = new LBCamera();
+                            break;
+                        case CameraBrand.HRCamera:
+                            camera = new HRCamera();
+                            break;
+                        default:
+                            Debug.WriteLine($"銆恵DateTime.Now:HH:mm:ss.fff}銆戞湭鐭ュ搧鐗�");
+                            return;
+                    }
                 }
 
-                if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), this.Handle))
+                IntPtr displayHandle = onlinePictureBox.Handle;
+                onlinePictureBox.Visible = true;
+
+                bool initSuccess = false;
+                if (isReusingExisting)
                 {
+                    // 澶嶇敤鏃朵笉闇�瑕佸啀娆nitDevice锛屼絾鍙兘闇�瑕佹洿鏂版樉绀哄彞鏌�(瑙嗗叿浣揝DK瀹炵幇鑰屽畾锛孡BCamera閫氬父涓嶉渶瑕�)
+                    initSuccess = true; 
+                    MessageBox.Show(camera.SN + "宸茶繛鎺� (澶嶇敤)");
+                }
+                else
+                {
+                    if (cmbSN.Items.Count > 0 && camera.InitDevice(selectedSN, displayHandle))
+                    {
+                        initSuccess = true;
+                        MessageBox.Show(camera.SN + "鎵撳紑鎴愬姛");
+                    }
+                }
+
+                if (initSuccess)
+                {
+                    // 閲嶆柊缁戝畾鏄剧ず鍥炶皟
                     camera.ImageGrabbed -= GetImageBllComplete;
                     camera.ImageGrabbed += GetImageBllComplete;
-                    MessageBox.Show(camera.SN + "鎵撳紑鎴愬姛");
                     this.btnEdit.Enabled = true;
                 }
             }
@@ -264,18 +328,51 @@
             {
                 Debug.WriteLine($"銆恵DateTime.Now:HH:mm:ss.fff}銆戞棤鏁堢殑鏋氫妇鍊硷紒");
             }
+            
+            // ... (rest of the UI update logic)
 
             if (camera != null)
             {
                 this.BeginInvoke(new Action(() =>
                 {
-                    double exp = 0;
-                    double gain = 0;
-                    camera.GetExpouseTime(out exp);
-                    txtExp.Text = exp.ToString();
+                    if (camera is LBCamera lbCam)
+                    {
+                        // 鍒囨崲鍒颁笅鎷夋鏄剧ず
+                        txtGain.Visible = false;
+                        cmbGain.Visible = true;
+                        
+                        // 濉厖閫夐」 (瀵瑰簲 EnumAnalogGain: Gain_1_0, Gain_1_3, Gain_1_9, Gain_2_8, Gain_5_5)
+                        cmbGain.Items.Clear();
+                        cmbGain.Items.AddRange(new object[] { "1.0x", "1.3x", "1.9x", "2.8x", "5.5x" });
 
-                    camera.GetGain(out gain);
-                    txtGain.Text = gain.ToString();
+                        var config = lbCam.GetSensorConfig();
+                        txtExp.Text = config.ExposureTime.ToString();
+                        
+                        // 璁剧疆褰撳墠閫変腑鐨勫鐩�
+                        int gainIndex = (int)config.AnalogGain;
+                        if (gainIndex >= 0 && gainIndex < cmbGain.Items.Count)
+                        {
+                            cmbGain.SelectedIndex = gainIndex;
+                        }
+                        else
+                        {
+                            cmbGain.SelectedIndex = 0;
+                        }
+                    }
+                    else
+                    {
+                        // 鎭㈠鏂囨湰妗嗘樉绀�
+                        txtGain.Visible = true;
+                        cmbGain.Visible = false;
+
+                        double exp = 0;
+                        double gain = 0;
+                        camera.GetExpouseTime(out exp);
+                        txtExp.Text = exp.ToString();
+
+                        camera.GetGain(out gain);
+                        txtGain.Text = gain.ToString();
+                    }
 
                     camera.GetTriggerMode(out TriggerMode mode, out TriggerSource source);
 
@@ -295,8 +392,10 @@
 
             if (camera.CloseDevice())
             {
+                onlinePictureBox.Visible = true;
                 MessageBox.Show(camera.SN + "鏂紑鎴愬姛");
                 this.panel_Picture.Controls.Clear();
+                this.panel_Picture.Controls.Add(onlinePictureBox);
             }
         }
 
@@ -382,28 +481,31 @@
 
         /// <summary>
         /// 鐩告満鍥炶皟杩愯
+        /// 娉ㄦ剰锛氬浜嶭BCamera锛�3D绾挎壂鐩告満锛夛紝浣跨敤SDK鑷姩鏄剧ず妯″紡
+        /// SDK浼氳嚜鍔ㄥ皢鍥惧儚鏄剧ず鍒板搴旂殑鎺т欢涓婏紝姝ゅ洖璋冨彧鐢ㄤ簬缁熻閲囬泦娆℃暟
         /// </summary>
         /// <param name="CCDName"></param>
         /// <param name="image"></param>
         private void GetImageBllComplete(object sender, CameraEventArgs e)
         {
+            // 瀵逛簬2D鐩告満锛堟墜鍔ㄥ鐞嗘ā寮忥級鍜岀幇鍦ㄧ殑LBCamera锛堟墜鍔ㄥ鐞嗘ā寮忥級
             if (e.Bitmap == null)
                 return;
 
-                lock (e.Bitmap)
+            lock (e.Bitmap)
+            {
+                if (this.InvokeRequired) // 妫�鏌ユ槸鍚﹂渶瑕佸湪UI绾跨▼涓婅皟鐢�
                 {
-                    if (this.InvokeRequired) // 妫�鏌ユ槸鍚﹂渶瑕佸湪UI绾跨▼涓婅皟鐢�
-                    {
-                        this.Invoke(new Action(() =>
-                        {
-                            onlinePictureBox.Image = e.Bitmap;
-                        })); // 閫掑綊璋冪敤鑷韩锛屼絾杩欐鍦║I绾跨▼涓�
-                    }
-                    else
+                    this.Invoke(new Action(() =>
                     {
                         onlinePictureBox.Image = e.Bitmap;
-                    }
+                    })); // 閫掑綊璋冪敤鑷韩锛屼絾杩欐鍦║I绾跨▼涓�
                 }
+                else
+                {
+                    onlinePictureBox.Image = e.Bitmap;
+                }
+            }
             total.iImageCount++;
             try
             {
@@ -426,23 +528,79 @@
 
             Task.Factory.StartNew(() =>
             {
-                //camera.GetCamConfig(out CameraConfig OriCamConfig);
-
+                // 璁剧疆鏇濆厜鍜屽鐩�
                 camera.SetExpouseTime(Convert.ToDouble(txtExp.Text));
                 camera.SetGain(Convert.ToDouble(txtGain.Text));
 
-                camera.GetImageWithSoftTrigger(out Bitmap bitmap);
+                bool success = false;
+                Bitmap bitmap = null;
 
-                this.BeginInvoke(new Action(() =>
+                // 璋冪敤鍩虹被鎺ュ彛鐨勫崟娆¢噰闆嗘柟娉�
+                // LBCamera閲嶅啓浜哠tartSingleGrab鏂规硶锛屽叾浠栫浉鏈鸿皟鐢ㄨ繑鍥瀎alse
+                success = camera.StartSingleGrab();
+
+                if (success)
                 {
-                    if (bitmap != null)
-                        this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
-                    else
-                        this.lblCapTime.Text = "-1ms";
-                }));
+                    // 绛夊緟鍥惧儚鏁版嵁
+                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+                    {
+                        Bitmap captured = null;
+                        EventHandler<CameraEventArgs> handler = (s, evt) =>
+                        {
+                            if (evt.Bitmap != null)
+                            {
+                                captured = evt.Bitmap.Clone() as Bitmap;
+                                waitHandle.Set();
+                            }
+                        };
 
-                //澶嶅師鍘熼�氳鍙h缃�
-                //camera.SetCamConfig(OriCamConfig);
+                        camera.ImageGrabbed += handler;
+                        try
+                        {
+                            // 绛夊緟5绉掕秴鏃�
+                            if (waitHandle.WaitOne(5000))
+                            {
+                                bitmap = captured;
+                            }
+                        }
+                        finally
+                        {
+                            camera.ImageGrabbed -= handler;
+                            camera.StopGrabbing();
+                        }
+                    }
+
+                    this.BeginInvoke(new Action(() =>
+                    {
+                        if (bitmap != null)
+                        {
+                            this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+                            onlinePictureBox.Image = bitmap;
+                        }
+                        else
+                        {
+                            this.lblCapTime.Text = "-1ms";
+                        }
+                    }));
+                }
+                else
+                {
+                    // 濡傛灉StartSingleGrab澶辫触锛屽洖閫�鍒颁紶缁熺殑GetImageWithSoftTrigger
+                    camera.GetImageWithSoftTrigger(out bitmap);
+
+                    this.BeginInvoke(new Action(() =>
+                    {
+                        if (bitmap != null)
+                        {
+                            this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+                            onlinePictureBox.Image = bitmap;
+                        }
+                        else
+                        {
+                            this.lblCapTime.Text = "-1ms";
+                        }
+                    }));
+                }
             });
         }
 
@@ -453,12 +611,12 @@
 
             total.Clear();
 
-            // 灏濊瘯灏嗚緭鍏ュ瓧绗︿覆杞崲涓烘灇涓惧��
-            if (Enum.TryParse(cmbBrand.Text, true, out CameraBrand brand))
-            {
-                camera.StopGrabbing();
-                camera.StartWith_HardTriggerModel();
-            }
+            // 鍋滄褰撳墠閲囬泦
+            camera.StopGrabbing();
+
+            // 璋冪敤鍩虹被鎺ュ彛鐨勮繛缁噰闆嗘柟娉�
+            // LBCamera浼氳皟鐢⊿tartContinuousGrab鏂规硶锛屽叾浠栫浉鏈轰娇鐢ㄥ師鏈夌殑StartWith_HardTriggerModel
+            camera.StartContinuousGrab();
 
             startGrabtime = DateTime.Now;
 
@@ -473,18 +631,15 @@
 
             total.Clear();
 
-            // 灏濊瘯灏嗚緭鍏ュ瓧绗︿覆杞崲涓烘灇涓惧��
-            if (Enum.TryParse(cmbBrand.Text, true, out CameraBrand brand))
-            {
-                Task.Factory.StartNew(() =>
-                {
-                    camera.StopGrabbing();
-                    camera.StartWith_SoftTriggerModel();
+            // 鍋滄褰撳墠閲囬泦
+            camera.StopGrabbing();
 
-                });
-            }
+            // 璋冪敤鍩虹被鎺ュ彛鐨勮繛缁噰闆嗘柟娉�
+            // LBCamera浼氳皟鐢⊿tartContinuousGrab鏂规硶锛屽叾浠栫浉鏈轰娇鐢ㄥ師鏈夌殑StartWith_SoftTriggerModel
+            camera.StartContinuousGrab();
 
             startGrabtime = DateTime.Now;
+
             cmbSN.Enabled = false;
             cmbBrand.Enabled = false;
         }
@@ -561,7 +716,8 @@
 
         private void txtGain_TextChanged(object sender, EventArgs e)
         {
-            if (camera == null)
+            // 濡傛灉姝e湪浣跨敤涓嬫媺妗嗘ā寮�(LBCamera)锛屽拷鐣ユ枃鏈鐨勫彉鍖�
+            if (camera == null || (cmbGain != null && cmbGain.Visible))
                 return;
 
             double gain = 10;
@@ -655,19 +811,34 @@
 
         private void CameraForm_FormClosed(object sender, FormClosedEventArgs e)
         {
+            onlinePictureBox.Visible = true;
+            this.panel_Picture.Controls.Clear();
+            this.panel_Picture.Controls.Add(onlinePictureBox);
+            
             this.onlinePictureBox.Image = null;
             if (camera != null)
             {
                 camera.ImageGrabbed -= GetImageBllComplete;
-                camera.StopGrabbing();
+                
+                // 妫�鏌ヨ鐩告満鏄惁鐢变富绯荤粺绠$悊
+                bool isManagedBySystem = dicCameras != null && dicCameras.ContainsKey(camera.SN);
 
-                camera.GetTriggerMode(out _, out TriggerSource actualSource);
-                if (radioButtonSoft.Checked)
-                    camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
-                else
-                    camera.SetTriggerMode(TriggerMode.On, actualSource);
+                if (!isManagedBySystem)
+                {
+                    // 鍙湁闈炴墭绠$殑锛堜复鏃舵墦寮�鐨勶級鐩告満鎵嶅仠姝㈤噰闆嗗拰閲婃斁
+                    camera.StopGrabbing();
 
-                camera.StartGrabbing();
+                    camera.GetTriggerMode(out _, out TriggerSource actualSource);
+                    if (radioButtonSoft.Checked)
+                        camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
+                    else
+                        camera.SetTriggerMode(TriggerMode.On, actualSource);
+                    
+                    if (camera.Brand != CameraBrand.LBCamera)
+                    {
+                        camera.StartGrabbing();
+                    }
+                }
             }
 
             //璺緞涓虹┖璇存槑涓烘祴璇曟ā寮忥紝闇�瑕侀噴鏀剧浉鏈�

--
Gitblit v1.9.3