From 37aff9db0932e3e274b3c26650060f7d2d336888 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期五, 09 一月 2026 10:07:30 +0800
Subject: [PATCH] 添加增益选择下拉框并优化相机初始化逻辑
---
LB_VisionProcesses/Cameras/BaseCamera.cs | 2
LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs | 567 +++++++++++++++++++++----------------
LB_VisionProcesses/Cameras/2DCameraForm.cs | 282 ++++++++++--------
3 files changed, 487 insertions(+), 364 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index adccb8b..5aabcf7 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -80,9 +80,23 @@
private System.Windows.Forms.Timer updateTimer;
Total total = new Total { iImageCount = 0, iScanCount = 0 };
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,42 +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;
+ }
}
IntPtr displayHandle = onlinePictureBox.Handle;
- if (brand == CameraBrand.LBCamera)
+ onlinePictureBox.Visible = true;
+
+ bool initSuccess = false;
+ if (isReusingExisting)
{
- onlinePictureBox.Visible = false;
- displayHandle = this.panel_Picture.Handle;
+ // 澶嶇敤鏃朵笉闇�瑕佸啀娆nitDevice锛屼絾鍙兘闇�瑕佹洿鏂版樉绀哄彞鏌�(瑙嗗叿浣揝DK瀹炵幇鑰屽畾锛孡BCamera閫氬父涓嶉渶瑕�)
+ initSuccess = true;
+ MessageBox.Show(camera.SN + "宸茶繛鎺� (澶嶇敤)");
}
else
{
- onlinePictureBox.Visible = true;
+ if (cmbSN.Items.Count > 0 && camera.InitDevice(selectedSN, displayHandle))
+ {
+ initSuccess = true;
+ MessageBox.Show(camera.SN + "鎵撳紑鎴愬姛");
+ }
}
- if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), displayHandle))
+ if (initSuccess)
{
+ // 閲嶆柊缁戝畾鏄剧ず鍥炶皟
camera.ImageGrabbed -= GetImageBllComplete;
camera.ImageGrabbed += GetImageBllComplete;
- MessageBox.Show(camera.SN + "鎵撳紑鎴愬姛");
this.btnEdit.Enabled = true;
}
}
@@ -275,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);
@@ -402,18 +488,7 @@
/// <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鐩告満锛堟墜鍔ㄥ鐞嗘ā寮忥級
+ // 瀵逛簬2D鐩告満锛堟墜鍔ㄥ鐞嗘ā寮忥級鍜岀幇鍦ㄧ殑LBCamera锛堟墜鍔ㄥ鐞嗘ā寮忥級
if (e.Bitmap == null)
return;
@@ -466,89 +541,47 @@
if (success)
{
- // 瀵逛簬LBCamera锛圫DK鑷姩鏄剧ず妯″紡锛夛紝绛夊緟閲囬泦瀹屾垚鍗冲彲
- // 瀵逛簬2D鐩告満锛岀瓑寰匓itmap浜嬩欢
- if (camera.Brand == CameraBrand.LBCamera)
+ // 绛夊緟鍥惧儚鏁版嵁
+ using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
- // SDK鑷姩鏄剧ず妯″紡涓嬶紝绛夊緟閲囬泦瀹屾垚
- using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+ Bitmap captured = null;
+ EventHandler<CameraEventArgs> handler = (s, evt) =>
{
- bool captured = false;
- EventHandler<CameraEventArgs> handler = (s, evt) =>
+ if (evt.Bitmap != null)
{
- if (evt is LBCameraEventArgs args && args.IsComplete)
- {
- captured = true;
- waitHandle.Set();
- }
- };
-
- camera.ImageGrabbed += handler;
- try
- {
- // 绛夊緟5绉掕秴鏃�
- if (waitHandle.WaitOne(5000))
- {
- // SDK鑷姩鏄剧ず妯″紡锛屼笉杩斿洖Bitmap
- // 浣嗛噰闆嗗凡瀹屾垚
- }
- this.BeginInvoke(new Action(() =>
- {
- this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
- }));
+ captured = evt.Bitmap.Clone() as Bitmap;
+ waitHandle.Set();
}
- finally
+ };
+
+ camera.ImageGrabbed += handler;
+ try
+ {
+ // 绛夊緟5绉掕秴鏃�
+ if (waitHandle.WaitOne(5000))
{
- camera.ImageGrabbed -= handler;
- camera.StopGrabbing();
+ bitmap = captured;
}
}
- }
- else
- {
- // 瀵逛簬2D鐩告満锛岀瓑寰呭浘鍍忔暟鎹�
- using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+ finally
{
- 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();
- }
+ 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";
- }
- }));
}
+
+ this.BeginInvoke(new Action(() =>
+ {
+ if (bitmap != null)
+ {
+ this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+ onlinePictureBox.Image = bitmap;
+ }
+ else
+ {
+ this.lblCapTime.Text = "-1ms";
+ }
+ }));
}
else
{
@@ -683,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;
@@ -785,19 +819,25 @@
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);
-
- // LBCamera鍦⊿tartGrabbing鏃朵細鐩存帴寮�鍚縺鍏夊拰閲囬泦锛屽洜姝ゅ叧闂獥鍙f椂涓嶅簲鑷姩閲嶅惎閲囬泦
- // 鍏朵粬鐩告満(濡�2D鐩告満)閫氬父闇�瑕佷繚鎸丟rabbing鐘舵�佷互鎺ユ敹瑙﹀彂
- if (camera.Brand != CameraBrand.LBCamera)
+ if (!isManagedBySystem)
{
- camera.StartGrabbing();
+ // 鍙湁闈炴墭绠$殑锛堜复鏃舵墦寮�鐨勶級鐩告満鎵嶅仠姝㈤噰闆嗗拰閲婃斁
+ camera.StopGrabbing();
+
+ 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();
+ }
}
}
diff --git a/LB_VisionProcesses/Cameras/BaseCamera.cs b/LB_VisionProcesses/Cameras/BaseCamera.cs
index 215aff7..a5b371a 100644
--- a/LB_VisionProcesses/Cameras/BaseCamera.cs
+++ b/LB_VisionProcesses/Cameras/BaseCamera.cs
@@ -186,7 +186,7 @@
#region protected abstract
/// <summary>
- /// 寮�濮嬮噰鍥撅紙榛樿杩炵画閲囬泦锛�
+ /// 寮�濮嬮噰鍥撅紙榛樿鍗曟閲囬泦锛�
/// </summary>
/// <returns></returns>
public abstract bool StartGrabbing();
diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index c989044..6e27e23 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
+using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
@@ -30,91 +31,39 @@
{
private IntPtr _cameraHandle = IntPtr.Zero;
private PHM6000SensorConfig _sensorConfig;
+
+ // 閲囬泦鍥炶皟
+ private AcquisitionCallbackZA _acquisitionCallback;
private AcquisitionCompletedCallback _acquisitionCompletedCallback;
+
private bool _isConnected = false;
private int _frameCount = 0; // 閲囬泦甯ц鏁�
// 鍥惧儚缂撳啿
- private List<byte[]> _lineDataBuffer = new List<byte[]>();
- private readonly object _bufferLock = new object();
- private int _currentLineCount = 0;
-
- // 瀹炴椂鏄剧ず鐢ㄧ殑Bitmap
- private Bitmap _currentBitmap = null;
- private byte[] _rawPixelBuffer = null; // 鐢ㄤ簬瀛樺偍鍍忕礌鏁版嵁锛岄伩鍏嶉绻丩ockBits
+ private byte[] _rawPixelBuffer = null; // 鐢ㄤ簬瀛樺偍鏁村紶鍥剧殑鍍忕礌鏁版嵁 (8bpp)
private int _currentBitmapHeight = 0;
private int _currentBitmapWidth = 0;
- private object _bitmapLock = new object();
- private DateTime _lastUpdateTime = DateTime.MinValue;
+ private int _currentLineCount = 0;
+ private object _bufferLock = new object();
+ private bool _isBufferReady = false;
- // 鏄剧ず鍙ユ焺鍜屽嚱鏁帮紙涓嶭LSystem绀轰緥淇濇寔涓�鑷达級
- private IntPtr _lightPic = IntPtr.Zero;
- private IntPtr _deepPic = IntPtr.Zero;
- private IntPtr _pointPic = IntPtr.Zero;
- private IntPtr _outlinePic = IntPtr.Zero;
-
- // 鏄剧ず鎺у埗鍑芥暟缁撴瀯浣�
- private PILOT2D_FUNC _func2d;
- private VTK3D_FUNC _func3d;
+ // 涓存椂琛岀紦鍐诧紝鐢ㄤ簬鎺ユ敹鍥炶皟鏁版嵁
+ private byte[] _tempLineBuffer = null;
+ private bool _isContinuous = false;
public LBCamera()
{
Brand = CameraBrand.LBCamera;
_sensorConfig = new PHM6000SensorConfig();
-
- // 鍒濆鍖�2D鏄剧ず鍑芥暟
- _func2d = new PILOT2D_FUNC
- {
- AddBarycentreDataZA = Pilot2D.AddBarycentreDataZA,
- AddDepthData = Pilot2D.AddDepthData,
- AddIntensityData = Pilot2D.AddIntensityData,
- ClearAllPoints = Pilot2D.ClearAllPoints,
- RefreshPilot2D = Pilot2D.RefreshPilot2D,
- SetImageSize = Pilot2D.SetImageSize,
- };
-
- // 鍒濆鍖�3D鏄剧ず鍑芥暟
- _func3d = new VTK3D_FUNC
- {
- AddZAPoints = PHM6000Profiler.AddZAPoints,
- ClearPCLPoints = PHM6000Profiler.ClearPCLPoints,
- GetPointCloudBound = PHM6000Profiler.GetPointCloudBound,
- RenderPCLWindow = PHM6000Profiler.RenderPCLWindow,
- SetLookUpTableRange = PHM6000Profiler.SetLookUpTableRange,
- ShowCubeAxes = PHM6000Profiler.ShowCubeAxes,
- ShowLookUpTable = PHM6000Profiler.ShowLookUpTable,
- UpdatePCLPointColors = PHM6000Profiler.UpdatePCLPointColors,
- };
}
#region ICamera Implementation
public override bool InitDevice(string sn, object handle = null)
{
- // 濡傛灉宸茶繛鎺ワ紝浠呮鏌ユ槸鍚﹂渶瑕佹洿鏂版樉绀哄彞鏌�
+ // 濡傛灉宸茶繛鎺ワ紝鐩存帴杩斿洖true
if (_isConnected && _cameraHandle != IntPtr.Zero)
{
- if (handle != null && handle is IntPtr hPtr && hPtr != IntPtr.Zero)
- {
- // 閿�姣佹棫鐨勬樉绀哄彞鏌�
- if (_lightPic != IntPtr.Zero)
- {
- Pilot2D.ClearAllPoints(_lightPic);
- Pilot2D.DestroyPilot2DEntry(_lightPic);
- }
-
- // 鍒涘缓鏂扮殑鏄剧ず鍙ユ焺
- _lightPic = Pilot2D.CreatePilot2DEntry(hPtr);
- if (_lightPic != IntPtr.Zero)
- {
- var config = GetSensorConfig();
- Pilot2D.SetImageSize(_lightPic, 4096, config.ScanLineCount > 0 ? config.ScanLineCount : 5000);
-
- // 鏇存柊鐩告満缁戝畾鐨勬樉绀哄彞鏌�
- PHM6000Profiler.SetShowHandles(_cameraHandle, _lightPic, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
- AsyncLogHelper.Info($"LBCamera[{SN}]: Display handle updated");
- }
- }
return true;
}
@@ -148,9 +97,8 @@
{
string currentSn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
- // 濡傛灉浼犲叆鐨� sn 鏄� IP 鍦板潃锛屽垯鐩存帴灏濊瘯鍖归厤 IP
- // 鎴栬�呭尮閰嶅簭鍒楀彿
- if (currentSn == sn || sn.Contains(currentSn)) // 绠�鍗曞尮閰嶉�昏緫
+ // 鍖归厤SN鎴朓P
+ if (currentSn == sn || sn.Contains(currentSn))
{
byte[] addressBytes = new byte[64];
int port = 0;
@@ -165,17 +113,15 @@
}
}
- // 閿�姣佷复鏃跺彞鏌�
PHM6000Profiler.DestroyCameraEntry(tempHandle);
tempHandle = IntPtr.Zero;
if (!found)
{
- // 濡傛灉娌℃壘鍒颁絾 sn 鏈韩鐪嬭捣鏉ュ儚 IP锛屽皾璇曠洿鎺ヨ繛鎺�
if (System.Net.IPAddress.TryParse(sn, out _))
{
targetIp = sn;
- targetPort = 5577; // 榛樿绔彛
+ targetPort = 5577;
}
else
{
@@ -197,35 +143,18 @@
// 鍔犺浇鐩告満褰撳墠鍙傛暟鍒� _sensorConfig
SyncConfigFromCamera();
- // 鍒涘缓浜害鍥炬樉绀哄彞鏌勶紙绫讳技LLSystem绀轰緥锛�
- IntPtr hPtr = IntPtr.Zero;
- if (handle is IntPtr p) hPtr = p;
-
- if (hPtr != IntPtr.Zero)
- {
- _lightPic = Pilot2D.CreatePilot2DEntry(hPtr);
- if (_lightPic != IntPtr.Zero)
- {
- // 璁剧疆鍥惧儚灏哄锛屽搴�4096锛岄珮搴︽牴鎹厤缃�
- var config = GetSensorConfig();
- Pilot2D.SetImageSize(_lightPic, 4096, config.ScanLineCount > 0 ? config.ScanLineCount : 5000);
- }
- }
+ // 鍒濆鍖栧苟娉ㄥ唽閲囬泦鍥炶皟 (鑾峰彇鏁版嵁鐢�)
+ _acquisitionCallback = new AcquisitionCallbackZA(OnAcquisitionCallbackZA);
+ PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
- // 鍒濆鍖栧洖璋�
+ // 鍒濆鍖栧苟娉ㄥ唽閲囬泦瀹屾垚鍥炶皟 (鐘舵�侀�氱煡鐢�)
_acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
-
- // 娉ㄥ唽閲囬泦瀹屾垚鍥炶皟锛堢被浼糒LSystem绀轰緥锛�
PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
- // 璁剧疆2D鍜�3D鏄剧ず鍑芥暟锛堢被浼糒LSystem绀轰緥锛�
- PHM6000Profiler.SetPilot2dFunc(_cameraHandle, _func2d);
- PHM6000Profiler.SetVTK3dFunc(_cameraHandle, _func3d);
+ // 寮哄埗搴旂敤褰撳墠閰嶇疆锛堢‘淇濊Е鍙戞ā寮忕瓑鍙傛暟姝g‘锛岄伩鍏嶇浉鏈哄浜庢湭鐭ョ姸鎬侊級
+ UpdateSensorConfig(_sensorConfig);
- // 璁剧疆鏄剧ず鍙ユ焺锛堢被浼糒LSystem绀轰緥鐨凷etShowHandles锛�
- PHM6000Profiler.SetShowHandles(_cameraHandle, _lightPic, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
-
- AsyncLogHelper.Info($"LBCamera[{SN}]: Connected and initialized successfully with SDK auto-display mode");
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Connected and initialized successfully (Manual Data Mode)");
return true;
}
@@ -247,20 +176,10 @@
if (_isConnected && _cameraHandle != IntPtr.Zero)
{
StopGrabbing();
-
- // 閿�姣佹樉绀哄彞鏌�
- if (_lightPic != IntPtr.Zero)
- {
- Pilot2D.ClearAllPoints(_lightPic);
- Pilot2D.DestroyPilot2DEntry(_lightPic);
- _lightPic = IntPtr.Zero;
- }
-
PHM6000Profiler.DestroyCameraEntry(_cameraHandle);
_cameraHandle = IntPtr.Zero;
_isConnected = false;
-
- AsyncLogHelper.Info($"LBCamera[{SN}]: Closed and cleaned up");
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Closed");
}
return true;
}
@@ -282,8 +201,6 @@
if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
{
string sn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
- string type = Encoding.UTF8.GetString(moduleTypeBytes).TrimEnd('\0');
- // 鏍煎紡鍙傝�冿細PHM6000[SN123456]
if (!string.IsNullOrEmpty(sn))
{
cameraList.Add(sn);
@@ -294,7 +211,7 @@
}
catch (Exception ex)
{
- AsyncLogHelper.Error($"LBCamera: GetListEnum寮傚父 - {ex.Message}");
+ AsyncLogHelper.Error($"LBCamera: 鑾峰彇璁惧鍒楄〃寮傚父 - {ex.Message}");
}
finally
{
@@ -306,132 +223,121 @@
private void InitBuffer()
{
- lock (_bitmapLock)
+ lock (_bufferLock)
{
_currentBitmapHeight = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
- // 瀹藉害鏆傛椂鏈煡锛屽皢鍦ㄧ涓�琛屾暟鎹埌杈炬椂鍒濆鍖�
+ // 瀹藉害鍦ㄧ涓�琛屾暟鎹埌杈炬椂纭畾
_currentBitmapWidth = 0;
- if (_currentBitmap != null)
- {
- _currentBitmap.Dispose();
- _currentBitmap = null;
- }
_rawPixelBuffer = null;
_currentLineCount = 0;
+ _isBufferReady = false;
}
}
public override bool StartGrabbing()
{
- // 绾挎壂鐩告満榛樿浣跨敤杩炵画閲囬泦妯″紡
- // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
+ // 榛樿杩炵画妯″紡
return StartSingleGrab();
}
- /// <summary>
- /// 鍗曟閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
- /// 鍙傝�僉LSystem绀轰緥鐨凷tartScan鏂规硶锛歋etAcquisitionMode(1, 0)
- /// 浣跨敤SDK鑷姩鏄剧ず妯″紡锛屼笉鎵嬪姩澶勭悊鏁版嵁
- /// </summary>
public bool StartSingleGrab()
{
if (!_isConnected) return false;
+
+ _isContinuous = false;
+ InitBuffer();
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 寮�濮嬪崟娆¢噰闆�");
- AsyncLogHelper.Info($"LBCamera[{SN}]: Starting single grab mode");
-
- // 浣跨敤SDK鑷姩鏄剧ず妯″紡锛岀被浼糒LSystem绀轰緥
- // 涓嶈缃瓵cquisitionCallbackZA锛岃SDK鑷姩澶勭悊鍥惧儚鏄剧ず
- PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
-
- // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�0=鍗曟妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
+ // 1=鎵弿妯″紡, 0=鍗曟
PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 0);
int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
if (result == 0)
{
isGrabbing = true;
- AsyncLogHelper.Info($"LBCamera[{SN}]: Single grab started successfully");
return true;
- }
- else
- {
- AsyncLogHelper.Error($"LBCamera[{SN}]: Failed to start single grab, result={result}");
}
return false;
}
- /// <summary>
- /// 杩炵画閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
- /// 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
- /// 浣跨敤SDK鑷姩鏄剧ず妯″紡锛屼笉鎵嬪姩澶勭悊鏁版嵁
- /// </summary>
public override bool StartContinuousGrab()
{
if (!_isConnected) return false;
- AsyncLogHelper.Info($"LBCamera[{SN}]: Starting continuous grab mode");
+ _isContinuous = true;
+ InitBuffer();
+ AsyncLogHelper.Info($"LBCamera[{SN}]:寮�濮嬭繛缁噰闆�");
- // 浣跨敤SDK鑷姩鏄剧ず妯″紡锛岀被浼糒LSystem绀轰緥
- // 涓嶈缃瓵cquisitionCallbackZA锛岃SDK鑷姩澶勭悊鍥惧儚鏄剧ず
- PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
-
- // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�1=杩炵画妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
+ // 1=鎵弿妯″紡, 1=杩炵画
PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 1);
int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
if (result == 0)
{
isGrabbing = true;
- AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous grab started successfully");
return true;
- }
- else
- {
- AsyncLogHelper.Error($"LBCamera[{SN}]: Failed to start continuous grab, result={result}");
}
return false;
}
public override bool StopGrabbing()
{
+ _isContinuous = false;
if (!_isConnected) return true;
PHM6000Profiler.StopAcquisition(_cameraHandle);
+
+ // 鍋滄鏃跺鏋滄湁鏈樉绀虹殑缂撳瓨鏁版嵁锛屽皢鍏舵樉绀哄嚭鏉ワ紙鏀寔鏄剧ず涓嶅畬鏁寸殑甯э級
+ lock (_bufferLock)
+ {
+ if (_currentLineCount > 0)
+ {
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Flushing partial buffer ({_currentLineCount} lines) on stop");
+ CreateAndFireBitmap();
+ _currentLineCount = 0;
+ }
+ }
+
isGrabbing = false;
return true;
}
+ public override bool StartWith_SoftTriggerModel() => StartContinuousGrab();
- public override bool StartWith_SoftTriggerModel()
- {
- // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岃蒋浠惰Е鍙戣繛缁噰闆�
- // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
- return StartContinuousGrab();
- }
+ public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0) => StartSingleGrab();
- public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
- {
- // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岀‖浠惰Е鍙戜篃浣跨敤杩炵画閲囬泦妯″紡
- // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
- // 澶栭儴纭欢淇″彿浼氳Е鍙戠浉鏈哄紑濮嬮噰闆�
- return StartSingleGrab();
- }
-
- public override bool SoftTrigger()
- {
- // 绾挎壂鐩告満閫氬父涓嶉渶瑕佷紶缁熻蒋瑙﹀彂锛屼絾鍦ㄦ煇浜涙ā寮忎笅鍙ā鎷�
- return true;
- }
+ public override bool SoftTrigger() => true;
#region 鍙傛暟璁剧疆鏄犲皠
public override bool SetExpouseTime(double value) => SetParam(EnumNameId.ExposureTime, (float)value);
public override bool GetExpouseTime(out double value) { float v; bool r = GetParam(EnumNameId.ExposureTime, out v); value = v; return r; }
- public override bool SetGain(double gain) => SetParam(EnumNameId.AnalogGain, (float)gain);
- public override bool GetGain(out double gain) { float v; bool r = GetParam(EnumNameId.AnalogGain, out v); gain = v; return r; }
+ public override bool SetGain(double gain) => SetParam(EnumNameId.AnalogGain, (int)gain);
+ public override bool GetGain(out double gain) { int v; bool r = GetParam(EnumNameId.AnalogGain, out v); gain = v; return r; }
- // 鍏朵粬鎺ュ彛鍗犱綅瀹炵幇
- public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0) => true;
- public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source) { mode = TriggerMode.Off; source = TriggerSource.Software; return true; }
+ public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0)
+ {
+ if (!_isConnected) return false;
+
+ if (triggerEnum == TriggerSource.Software)
+ {
+ _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.鍥哄畾棰戠巼;
+ _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.杞Е鍙�;
+ }
+ else
+ {
+ _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.缂栫爜鍣�;
+ _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.澶栭儴瑙﹀彂;
+ }
+ UpdateSensorConfig(_sensorConfig);
+ return true;
+ }
+
+ public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source)
+ {
+ mode = TriggerMode.On;
+ source = _sensorConfig.DataAcquisitionTriggerSource == EnumDataAcquisitionTriggerSource.杞Е鍙� ? TriggerSource.Software : TriggerSource.Line0;
+ return true;
+ }
public override bool SetTriggerPolarity(TriggerPolarity polarity) => true;
public override bool GetTriggerPolarity(out TriggerPolarity polarity) { polarity = TriggerPolarity.RisingEdge; return true; }
public override bool SetTriggerFliter(double flitertime) => true;
@@ -442,58 +348,63 @@
public override bool SetLineStatus(IOLines line, LineStatus linestatus) => true;
public override bool GetLineStatus(IOLines line, out LineStatus lineStatus) { lineStatus = LineStatus.Low; return true; }
public override bool AutoBalanceWhite() => true;
-
- // 涓嶅疄鐜扮殑鏂规硶
public override void SetCamConfig(CameraConfig config) { }
public override void GetCamConfig(out CameraConfig config) { config = new CameraConfig(null); }
public override bool GetImage(out Bitmap bitmap, int outtime = 3000) { bitmap = null; return false; }
- public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
+ public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
{
+ // 绠�鍗曞疄鐜帮細杞Е鍙戠瓑寰�
bitmap = null;
- if (!_isConnected) return false;
+ if(!_isConnected) return false;
- // 瀵逛簬绾挎壂鐩告満锛屼娇鐢⊿DK鑷姩鏄剧ず妯″紡
- // 娉ㄦ剰锛歋DK鑷姩鏄剧ず妯″紡涓嬶紝鎴戜滑鏃犳硶鐩存帴鑾峰彇Bitmap
- // 浣哠DK浼氳嚜鍔ㄥ皢鍥惧儚鏄剧ず鍒癬lightPic鍙ユ焺瀵瑰簲鐨勬帶浠朵笂
- // 鎴戜滑鍙渶瑕佸惎鍔ㄥ崟娆¢噰闆嗭紝绛夊緟閲囬泦瀹屾垚鍗冲彲
+ // 璁$畻鐞嗚鏈�灏忚�楁椂 (浠呭綋浣跨敤鍥哄畾棰戠巼瑙﹀彂鏃�)
+ int minTime = 0;
+ if (_sensorConfig.LineScanTriggerSource == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
+ {
+ float rate = _sensorConfig.SoftwareTriggerRate > 0 ? _sensorConfig.SoftwareTriggerRate : 1000f;
+ int lines = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
+ minTime = (int)((lines / rate) * 1000);
+ }
+ // 濡傛灉浼犲叆瓒呮椂鏃堕棿涓嶅锛岃嚜鍔ㄥ欢闀�
+ int actualTimeout = outtime;
+ if (actualTimeout < minTime + 2000)
+ {
+ actualTimeout = minTime + 3000; // 棰勭暀3绉掍綑閲�
+ AsyncLogHelper.Warn($"LBCamera: Provided timeout {outtime}ms is too short for {minTime}ms scan. Extended to {actualTimeout}ms.");
+ }
+
using (AutoResetEvent waitHandle = new AutoResetEvent(false))
{
- bool captured = false;
- EventHandler<CameraEventArgs> handler = (s, e) =>
- {
- // 瀵逛簬绾挎壂鐩告満锛屼粎褰揑sComplete涓簍rue鏃舵墠琛ㄧず閲囬泦瀹屾垚
- if (e is LBCameraEventArgs args && args.IsComplete)
- {
- captured = true;
- waitHandle.Set();
+ Bitmap res = null;
+ EventHandler<CameraEventArgs> handler = (s, e) => {
+ if(e.Bitmap != null) {
+ res = e.Bitmap.Clone() as Bitmap;
+ waitHandle.Set();
}
};
-
- this.ImageGrabbed += handler;
-
- try
+ ImageGrabbed += handler;
+
+ if (StartSingleGrab())
{
- // 浣跨敤鍗曟閲囬泦妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
- if (StartSingleGrab())
+ if (!waitHandle.WaitOne(actualTimeout))
{
- // 绛夊緟閲囬泦瀹屾垚
- if (waitHandle.WaitOne(outtime))
- {
- // 鐢变簬SDK鑷姩鏄剧ず锛屾垜浠棤娉曡繑鍥濨itmap
- // 浣嗛噰闆嗗凡瀹屾垚锛屽浘鍍忓凡鏄剧ず鍒扮晫闈笂
- bitmap = null; // SDK鑷姩鏄剧ず妯″紡涓嬩笉杩斿洖Bitmap
- return captured;
- }
+ AsyncLogHelper.Error($"LBCamera: GetImageWithSoftTrigger timeout after {actualTimeout}ms");
}
}
- finally
+ else
{
- this.ImageGrabbed -= handler;
- StopGrabbing();
+ AsyncLogHelper.Error("LBCamera: StartSingleGrab failed");
}
+
+ ImageGrabbed -= handler;
+
+ // 纭繚鍋滄閲囬泦
+ StopGrabbing();
+
+ bitmap = res;
+ return bitmap != null;
}
- return false;
}
public PHM6000SensorConfig GetSensorConfig()
@@ -505,27 +416,16 @@
public void UpdateSensorConfig(PHM6000SensorConfig config)
{
_sensorConfig = config;
-
if (!_isConnected) return;
-
- // 鍩虹鍥惧儚鍙傛暟
SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
-
- // 鎵弿鎺у埗鍙傛暟 - 鍐冲畾鍗曟閲囬泦鐨勬暟鎹噺
PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
-
- // 瑙﹀彂璁剧疆 - 鍐冲畾閲囬泦閫熷害
PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
-
- // 濡傛灉鏄蒋瑙﹀彂(鍥哄畾棰戠巼)锛岃缃鐜�
if (config.LineScanTriggerSource == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
{
PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
}
-
- // 纭繚鍙傛暟鐢熸晥
PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
}
@@ -533,42 +433,205 @@
#endregion
- #region Private Callback & Helpers
+ #region Callbacks
+
+ private void OnAcquisitionCallbackZA(IntPtr pInstance, IntPtr buffer, int points)
+ {
+ if (buffer == IntPtr.Zero || points <= 0) return;
+
+ lock (_bufferLock)
+ {
+ // 鍒濆鍖栫紦鍐插尯
+ if (_rawPixelBuffer == null)
+ {
+ _currentBitmapWidth = points;
+ if (_currentBitmapHeight <= 0) _currentBitmapHeight = 2000; // 榛樿闃插憜
+ _rawPixelBuffer = new byte[_currentBitmapWidth * _currentBitmapHeight];
+ _currentLineCount = 0;
+ }
+
+ if (_currentLineCount >= _currentBitmapHeight) return; // 缂撳啿鍖烘弧锛屽拷鐣ュ浣欐暟鎹�
+
+ // 鍑嗗涓存椂缂撳啿鍖烘帴鏀惰鏁版嵁 (LBPointZA = 8 bytes)
+ int lineBytes = points * 8;
+ if (_tempLineBuffer == null || _tempLineBuffer.Length != lineBytes)
+ {
+ _tempLineBuffer = new byte[lineBytes];
+ }
+
+ // 鎷疯礉闈炴墭绠″唴瀛樺埌鎵樼鏁扮粍
+ Marshal.Copy(buffer, _tempLineBuffer, 0, lineBytes);
+
+ // 鎻愬彇鐏板害(Intensity/Alpha)鏁版嵁濉厖鍒� _rawPixelBuffer
+ // LBPointZA缁撴瀯: float(4) + res(3) + alpha(1). Alpha鍦ㄥ亸绉�7
+ int bufferOffset = _currentLineCount * _currentBitmapWidth;
+
+ for (int i = 0; i < points; i++)
+ {
+ if (bufferOffset + i < _rawPixelBuffer.Length)
+ {
+ _rawPixelBuffer[bufferOffset + i] = _tempLineBuffer[i * 8 + 7];
+ }
+ }
+
+ _currentLineCount++;
+
+ // 濡傛灉杈惧埌棰勫畾楂樺害锛岀敓鎴愬浘鍍�
+ if (_currentLineCount >= _currentBitmapHeight)
+ {
+ CreateAndFireBitmap();
+
+ // 閲嶇疆锛屽噯澶囦笅涓�甯� (濡傛灉鏄繛缁噰闆�)
+ _currentLineCount = 0;
+ // _rawPixelBuffer 鍙互澶嶇敤锛屼笉闇�瑕佺疆绌�
+ }
+ }
+ }
private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
{
- // nOption鍚箟:
- // 0 = 涓�鎵规暟鎹粨鏉�
- // 1 = 鍏ㄩ儴閲囬泦瀹屾垚锛堜粎鍗曟妯″紡锛�
- // 2 = 瀵瑰簲鏁版嵁澶勭悊瀹屾垚锛堟瘡娆¢噰闆嗗畬鎴愰兘浼氳Е鍙戯級
-
- // 璁板綍鍥炶皟淇℃伅
- AsyncLogHelper.Info($"LBCamera[{SN}]: OnAcquisitionCompleted called, nOption={nOption}");
-
- if (nOption == 2)
+ // nOption: 0=Batch End, 1=All End(Single), 2=Processing End
+ // 姝ゅ涓昏鐢ㄤ簬鏃ュ織鎴栫姸鎬佺洃鎺�
+ // 瀹為檯鍥惧儚鐢熸垚鍦� Data Callback 涓畬鎴�
+ if (nOption == 1) // 鍗曟閲囬泦缁撴潫
{
- // 杩炵画閲囬泦妯″紡涓嬶紝姣忓畬鎴愪竴甯DK閮戒細鑷姩缁х画閲囬泦涓嬩竴甯�
- // SDK浼氳嚜鍔ㄥ鐞嗗浘鍍忔樉绀哄埌_lightPic鍙ユ焺
- // 鎴戜滑鍙渶瑕佺粺璁¢噰闆嗘鏁�
+ if (_isContinuous && isGrabbing)
+ {
+ // 濡傛灉鍦ㄨ繛缁ā寮忎笅鏀跺埌缁撴潫淇″彿锛屽皾璇曡嚜鍔ㄩ噸鍚噰闆�
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting...");
+ Task.Run(() =>
+ {
+ if (_isContinuous && _isConnected)
+ {
+ PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
+ }
+ });
+ }
+ else
+ {
+ isGrabbing = false;
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Single grab completed by SDK");
+
+ // 鍗曟閲囬泦缁撴潫鏃讹紝濡傛灉鏈夋湭鏄剧ず鐨勭紦鍐叉暟鎹紝绔嬪嵆鐢熸垚鍥惧儚
+ // 闃叉鍥犳暟鎹噺涓嶈冻(灏忎簬ScanLineCount)瀵艰嚧GetImageWithSoftTrigger涓�鐩寸瓑寰�
+ lock (_bufferLock)
+ {
+ if (_currentLineCount > 0)
+ {
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Flushing partial buffer ({_currentLineCount} lines) on completion");
+ CreateAndFireBitmap();
+ _currentLineCount = 0;
+ }
+ }
+ }
+ }
+ }
+
+ private void CreateAndFireBitmap()
+ {
+ try
+ {
+ int width = _currentBitmapWidth;
+ int height = _currentLineCount; // 浣跨敤瀹為檯閲囬泦鍒扮殑琛屾暟
+
+ if (width <= 0 || height <= 0 || _rawPixelBuffer == null) return;
+
+ Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
+
+ // 璁剧疆鐏板害璋冭壊鏉�
+ ColorPalette palette = bmp.Palette;
+ for (int i = 0; i < 256; i++)
+ {
+ palette.Entries[i] = Color.FromArgb(i, i, i);
+ }
+ bmp.Palette = palette;
+
+ // 鎷疯礉鏁版嵁
+ BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
+
+ // 娉ㄦ剰锛欱itmap Stride 鍙兘涓嶇瓑浜� Width锛岄渶瑕侀�愯鎷疯礉
+ int stride = bmpData.Stride;
+ IntPtr ptr = bmpData.Scan0;
+
+ for (int y = 0; y < height; y++)
+ {
+ // 纭繚涓嶈秺鐣�
+ if ((y * width) + width <= _rawPixelBuffer.Length)
+ {
+ Marshal.Copy(_rawPixelBuffer, y * width, ptr + y * stride, width);
+ }
+ }
+
+ bmp.UnlockBits(bmpData);
+
_frameCount++;
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} generated ({width}x{height})");
- AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} completed (continuous mode, SDK auto-display)");
-
- // 瑙﹀彂浜嬩欢閫氱煡UI鏇存柊锛堜絾涓嶄紶閫払itmap锛屽洜涓篠DK鑷姩鏄剧ず锛�
- ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, null, true));
+ // 寮傛瑙﹀彂浜嬩欢锛岄伩鍏嶉樆濉濻DK鍥炶皟绾跨▼
+ Task.Run(() =>
+ {
+ try
+ {
+ ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, bmp, true));
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera: Event Invoke error - {ex.Message}");
+ bmp.Dispose(); // 寮傚父鏃堕噴鏀捐祫婧�
+ }
+ });
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera: CreateBitmap error - {ex.Message}");
}
}
private void SyncConfigFromCamera()
{
- // 浠庣浉鏈鸿鍙栨墍鏈夊弬鏁板苟濉厖鍒� _sensorConfig
- foreach (EnumNameId id in Enum.GetValues(typeof(EnumNameId)))
+ try
{
- int iVal = 0; double dVal = 0; int eVal = 0;
- if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)id, ref iVal, ref dVal, ref eVal) == 0)
+ if (!_isConnected) return;
+
+ PropertyInfo[] props = _sensorConfig.GetType().GetProperties();
+ foreach (PropertyInfo p in props)
{
- // 瀹為檯椤圭洰涓簲浣跨敤鍙嶅皠灏嗗�煎啓鍥� _sensorConfig
+ // 璺宠繃鑷畾涔夊弬鏁�
+ var iscustomAttr = p.GetCustomAttribute<IsCustomAttribute>();
+ if (iscustomAttr != null) continue;
+
+ if (Enum.TryParse(typeof(EnumNameId), p.Name, out object nameIdObj))
+ {
+ EnumNameId nameId = (EnumNameId)nameIdObj;
+ int intValue = 0;
+ double doubleValue = 0;
+ int enumValue = 0;
+
+ if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)nameId, ref intValue, ref doubleValue, ref enumValue) == 0)
+ {
+ if (p.PropertyType == typeof(int))
+ {
+ p.SetValue(_sensorConfig, intValue);
+ }
+ else if (p.PropertyType == typeof(float))
+ {
+ p.SetValue(_sensorConfig, (float)doubleValue);
+ }
+ else if (p.PropertyType == typeof(double))
+ {
+ p.SetValue(_sensorConfig, doubleValue);
+ }
+ else // Enum or other types
+ {
+ p.SetValue(_sensorConfig, enumValue);
+ }
+ }
+ }
}
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera: SyncConfigFromCamera error - {ex.Message}");
}
}
@@ -576,6 +639,13 @@
{
if (!_isConnected) return false;
return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, value, 0) == 0;
+ }
+
+ private bool SetParam(EnumNameId id, int value)
+ {
+ if (!_isConnected) return false;
+ // 瀵逛簬鏋氫妇绫诲瀷锛岄�氬父閫氳繃 enumValue (鏈�鍚庝竴涓弬鏁�) 浼犻��
+ return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, 0, value) == 0;
}
private bool GetParam(EnumNameId id, out float value)
@@ -590,6 +660,19 @@
}
return false;
}
+
+ private bool GetParam(EnumNameId id, out int value)
+ {
+ value = 0;
+ if (!_isConnected) return false;
+ int iVal = 0; double dVal = 0; int eVal = 0;
+ if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)id, ref iVal, ref dVal, ref eVal) == 0)
+ {
+ value = eVal; // Assuming it returns in enumValue
+ return true;
+ }
+ return false;
+ }
#endregion
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3