From 5ddadba291ea2d9dba78259973594a4664b94f57 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期四, 08 一月 2026 16:45:35 +0800
Subject: [PATCH] 简化相机逻辑,支持手动数据处理
---
LB_VisionProcesses/Cameras/2DCameraForm.cs | 146 ++++++++++++++++++++++++++++++++++++------------
1 files changed, 108 insertions(+), 38 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index 107eba5..ec8d41b 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -252,7 +252,10 @@
return;
}
- if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), this.Handle))
+ IntPtr displayHandle = onlinePictureBox.Handle;
+ onlinePictureBox.Visible = true;
+
+ if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), displayHandle))
{
camera.ImageGrabbed -= GetImageBllComplete;
camera.ImageGrabbed += GetImageBllComplete;
@@ -295,8 +298,10 @@
if (camera.CloseDevice())
{
+ onlinePictureBox.Visible = true;
MessageBox.Show(camera.SN + "鏂紑鎴愬姛");
this.panel_Picture.Controls.Clear();
+ this.panel_Picture.Controls.Add(onlinePictureBox);
}
}
@@ -382,28 +387,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 +434,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 +517,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 +537,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;
}
@@ -655,6 +716,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)
{
@@ -667,7 +732,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