From 7279c77f318cd7e38af279dc98a1fecec33f5e30 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期四, 08 一月 2026 16:07:01 +0800
Subject: [PATCH] 优化相机采集逻辑与亮度图处理功能
---
LB_VisionProcesses/Cameras/2DCameraForm.cs | 215 +++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 181 insertions(+), 34 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index 6dc7029..adccb8b 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -1,6 +1,8 @@
锘縰sing HalconDotNet;
+using LB_SmartVisionCameraDevice.PHM6000;
using LB_VisionControl;
using LB_VisionProcesses.Cameras.HRCameras;
+using LB_VisionProcesses.Cameras.LBCameras;
using MVSDK_Net;
using Newtonsoft.Json.Linq;
using OpenCvSharp;
@@ -164,7 +166,7 @@
camera = new HRCamera();
break;
case CameraBrand.LBCamera:
- //camera = new LBCamera();
+ camera = new LBCamera();
break;
default:
Debug.WriteLine("鏈煡鍝佺墝");
@@ -240,7 +242,7 @@
switch (brand)
{
case CameraBrand.LBCamera:
- //camera = new LBCamera();
+ camera = new LBCamera();
break;
case CameraBrand.HRCamera:
camera = new HRCamera();
@@ -250,7 +252,18 @@
return;
}
- if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), this.Handle))
+ IntPtr displayHandle = onlinePictureBox.Handle;
+ if (brand == CameraBrand.LBCamera)
+ {
+ onlinePictureBox.Visible = false;
+ displayHandle = this.panel_Picture.Handle;
+ }
+ else
+ {
+ onlinePictureBox.Visible = true;
+ }
+
+ if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), displayHandle))
{
camera.ImageGrabbed -= GetImageBllComplete;
camera.ImageGrabbed += GetImageBllComplete;
@@ -293,8 +306,10 @@
if (camera.CloseDevice())
{
+ onlinePictureBox.Visible = true;
MessageBox.Show(camera.SN + "鏂紑鎴愬姛");
this.panel_Picture.Controls.Clear();
+ this.panel_Picture.Controls.Add(onlinePictureBox);
}
}
@@ -313,7 +328,19 @@
PropertyGrid pg = new PropertyGrid();
pg.Dock = DockStyle.Fill;
- pg.SelectedObject = new CameraAdvancedSettings(camera);
+
+ if (camera is LBCamera phmCamera)
+ {
+ pg.SelectedObject = phmCamera.GetSensorConfig();
+ pg.PropertyValueChanged += (s, ev) =>
+ {
+ phmCamera.UpdateSensorConfig((PHM6000SensorConfig)pg.SelectedObject);
+ };
+ }
+ else
+ {
+ pg.SelectedObject = new CameraAdvancedSettings(camera);
+ }
editForm.Controls.Add(pg);
editForm.ShowDialog();
@@ -368,11 +395,25 @@
/// <summary>
/// 鐩告満鍥炶皟杩愯
+ /// 娉ㄦ剰锛氬浜嶭BCamera锛�3D绾挎壂鐩告満锛夛紝浣跨敤SDK鑷姩鏄剧ず妯″紡
+ /// SDK浼氳嚜鍔ㄥ皢鍥惧儚鏄剧ず鍒板搴旂殑鎺т欢涓婏紝姝ゅ洖璋冨彧鐢ㄤ簬缁熻閲囬泦娆℃暟
/// </summary>
/// <param name="CCDName"></param>
/// <param name="image"></param>
private void GetImageBllComplete(object sender, CameraEventArgs e)
{
+ // 瀵逛簬LBCamera锛圫DK鑷姩鏄剧ず妯″紡锛夛紝Bitmap涓簄ull锛屾垜浠彧闇�瑕佺粺璁�
+ if (camera.Brand == CameraBrand.LBCamera)
+ {
+ if (e is LBCameraEventArgs args && args.IsComplete)
+ {
+ total.iImageCount++;
+ // 涓嶉渶瑕佹洿鏂皁nlinePictureBox.Image锛孲DK浼氳嚜鍔ㄦ樉绀�
+ }
+ return;
+ }
+
+ // 瀵逛簬2D鐩告満锛堟墜鍔ㄥ鐞嗘ā寮忥級
if (e.Bitmap == null)
return;
@@ -390,7 +431,6 @@
onlinePictureBox.Image = e.Bitmap;
}
}
-
total.iImageCount++;
try
{
@@ -413,23 +453,121 @@
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";
- }));
+ // 瀵逛簬LBCamera锛圫DK鑷姩鏄剧ず妯″紡锛夛紝绛夊緟閲囬泦瀹屾垚鍗冲彲
+ // 瀵逛簬2D鐩告満锛岀瓑寰匓itmap浜嬩欢
+ if (camera.Brand == CameraBrand.LBCamera)
+ {
+ // SDK鑷姩鏄剧ず妯″紡涓嬶紝绛夊緟閲囬泦瀹屾垚
+ using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+ {
+ bool captured = false;
+ EventHandler<CameraEventArgs> handler = (s, evt) =>
+ {
+ if (evt is LBCameraEventArgs args && args.IsComplete)
+ {
+ captured = true;
+ waitHandle.Set();
+ }
+ };
- //澶嶅師鍘熼�氳鍙h缃�
- //camera.SetCamConfig(OriCamConfig);
+ camera.ImageGrabbed += handler;
+ try
+ {
+ // 绛夊緟5绉掕秴鏃�
+ if (waitHandle.WaitOne(5000))
+ {
+ // SDK鑷姩鏄剧ず妯″紡锛屼笉杩斿洖Bitmap
+ // 浣嗛噰闆嗗凡瀹屾垚
+ }
+ this.BeginInvoke(new Action(() =>
+ {
+ this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+ }));
+ }
+ finally
+ {
+ camera.ImageGrabbed -= handler;
+ camera.StopGrabbing();
+ }
+ }
+ }
+ else
+ {
+ // 瀵逛簬2D鐩告満锛岀瓑寰呭浘鍍忔暟鎹�
+ using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+ {
+ Bitmap captured = null;
+ EventHandler<CameraEventArgs> handler = (s, evt) =>
+ {
+ // 瀵逛簬2D鐩告満锛岀洿鎺ユ帴鏀跺浘鍍�
+ if (!(evt is LBCameraEventArgs) && evt.Bitmap != null)
+ {
+ captured = evt.Bitmap.Clone() as Bitmap;
+ waitHandle.Set();
+ }
+ };
+
+ 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";
+ }
+ }));
+ }
});
}
@@ -440,12 +578,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;
@@ -460,18 +598,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;
}
@@ -489,8 +624,11 @@
Task.Factory.StartNew(() =>
{
camera.StopGrabbing();
- camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
- camera.StartGrabbing();
+ if (brand != CameraBrand.LBCamera)
+ {
+ camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
+ camera.StartGrabbing();
+ }
});
}
@@ -639,6 +777,10 @@
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)
{
@@ -651,7 +793,12 @@
else
camera.SetTriggerMode(TriggerMode.On, actualSource);
- camera.StartGrabbing();
+ // LBCamera鍦⊿tartGrabbing鏃朵細鐩存帴寮�鍚縺鍏夊拰閲囬泦锛屽洜姝ゅ叧闂獥鍙f椂涓嶅簲鑷姩閲嶅惎閲囬泦
+ // 鍏朵粬鐩告満(濡�2D鐩告満)閫氬父闇�瑕佷繚鎸丟rabbing鐘舵�佷互鎺ユ敹瑙﹀彂
+ if (camera.Brand != CameraBrand.LBCamera)
+ {
+ camera.StartGrabbing();
+ }
}
//璺緞涓虹┖璇存槑涓烘祴璇曟ā寮忥紝闇�瑕侀噴鏀剧浉鏈�
--
Gitblit v1.9.3