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/LBCameras/LBCamera.cs | 649 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 555 insertions(+), 94 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index 2bdc26b..c989044 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -1,134 +1,595 @@
-锘縰sing OpenCvSharp;
using System;
using System.Collections.Generic;
-using System.Linq;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Runtime.InteropServices;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
+using LB_SmartVisionCameraSDK.PHM6000;
+using LB_VisionProcesses.Cameras;
+using LB_SmartVisionCommon;
+using LB_SmartVisionCameraDevice.PHM6000;
namespace LB_VisionProcesses.Cameras.LBCameras
{
+ public class LBCameraEventArgs : CameraEventArgs
+ {
+ public bool IsComplete { get; set; }
+ public LBCameraEventArgs(string sn, Bitmap bitmap, bool isComplete) : base(sn, bitmap)
+ {
+ IsComplete = isComplete;
+ }
+ }
+
/// <summary>
/// LB3D宸ヤ笟鐩告満瀹炵幇绫�
/// 鍩轰簬PHM6000绯诲垪灏佽
/// </summary>
- internal class LBCamera : BaseCamera
+ public class LBCamera : BaseCamera
{
- public override bool AutoBalanceWhite()
+ private IntPtr _cameraHandle = IntPtr.Zero;
+ private PHM6000SensorConfig _sensorConfig;
+ 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 int _currentBitmapHeight = 0;
+ private int _currentBitmapWidth = 0;
+ private object _bitmapLock = new object();
+ private DateTime _lastUpdateTime = DateTime.MinValue;
+
+ // 鏄剧ず鍙ユ焺鍜屽嚱鏁帮紙涓嶭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;
+
+ public LBCamera()
{
- return true;
+ 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)
+ {
+ // 濡傛灉宸茶繛鎺ワ紝浠呮鏌ユ槸鍚﹂渶瑕佹洿鏂版樉绀哄彞鏌�
+ 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;
+ }
+
+ IntPtr tempHandle = IntPtr.Zero;
+ try
+ {
+ SN = sn;
+ // 1. 鍒涘缓涓存椂鍙ユ焺鐢ㄤ簬鍙戠幇璁惧
+ tempHandle = PHM6000Profiler.CreateCameraEntry();
+ if (tempHandle == IntPtr.Zero) return false;
+
+ // 2. 鍙戠幇鐩告満
+ int cameraCount = PHM6000Profiler.DiscoverCameras(tempHandle);
+ if (cameraCount <= 0)
+ {
+ PHM6000Profiler.DestroyCameraEntry(tempHandle);
+ return false;
+ }
+
+ string targetIp = string.Empty;
+ int targetPort = 0;
+ bool found = false;
+
+ // 3. 閬嶅巻鐩告満瀵绘壘鍖归厤鐨凷N
+ for (int i = 0; i < cameraCount; i++)
+ {
+ byte[] moduleTypeBytes = new byte[64];
+ byte[] serialNumberBytes = new byte[64];
+
+ if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
+ {
+ string currentSn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
+
+ // 濡傛灉浼犲叆鐨� sn 鏄� IP 鍦板潃锛屽垯鐩存帴灏濊瘯鍖归厤 IP
+ // 鎴栬�呭尮閰嶅簭鍒楀彿
+ if (currentSn == sn || sn.Contains(currentSn)) // 绠�鍗曞尮閰嶉�昏緫
+ {
+ byte[] addressBytes = new byte[64];
+ int port = 0;
+ if (PHM6000Profiler.GetCameraAddress(tempHandle, i, addressBytes, ref port) == 0)
+ {
+ targetIp = Encoding.UTF8.GetString(addressBytes).TrimEnd('\0');
+ targetPort = port;
+ found = true;
+ break;
+ }
+ }
+ }
+ }
+
+ // 閿�姣佷复鏃跺彞鏌�
+ PHM6000Profiler.DestroyCameraEntry(tempHandle);
+ tempHandle = IntPtr.Zero;
+
+ if (!found)
+ {
+ // 濡傛灉娌℃壘鍒颁絾 sn 鏈韩鐪嬭捣鏉ュ儚 IP锛屽皾璇曠洿鎺ヨ繛鎺�
+ if (System.Net.IPAddress.TryParse(sn, out _))
+ {
+ targetIp = sn;
+ targetPort = 5577; // 榛樿绔彛
+ }
+ else
+ {
+ AsyncLogHelper.Error($"LBCamera: 鏈壘鍒癝N涓� {sn} 鐨勭浉鏈�");
+ return false;
+ }
+ }
+
+ // 4. 鍒涘缓姝e紡鐩告満鍙ユ焺骞惰繛鎺�
+ _cameraHandle = PHM6000Profiler.CreateCameraEntry();
+ if (_cameraHandle == IntPtr.Zero) return false;
+
+ var addr = Encoding.ASCII.GetBytes(targetIp);
+ int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort);
+
+ if (result == 0)
+ {
+ _isConnected = true;
+ // 鍔犺浇鐩告満褰撳墠鍙傛暟鍒� _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);
+ }
+ }
+
+ // 鍒濆鍖栧洖璋�
+ _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
+
+ // 娉ㄥ唽閲囬泦瀹屾垚鍥炶皟锛堢被浼糒LSystem绀轰緥锛�
+ PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
+
+ // 璁剧疆2D鍜�3D鏄剧ず鍑芥暟锛堢被浼糒LSystem绀轰緥锛�
+ PHM6000Profiler.SetPilot2dFunc(_cameraHandle, _func2d);
+ PHM6000Profiler.SetVTK3dFunc(_cameraHandle, _func3d);
+
+ // 璁剧疆鏄剧ず鍙ユ焺锛堢被浼糒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");
+
+ return true;
+ }
+ else
+ {
+ AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}");
+ }
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera: InitDevice寮傚父 - {ex.Message}");
+ if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle);
+ }
+ return false;
}
public override bool CloseDevice()
{
- return true;
- }
+ if (_isConnected && _cameraHandle != IntPtr.Zero)
+ {
+ StopGrabbing();
- public override bool GetExpouseTime(out double value)
- {
- value = 0;
- return true;
- }
+ // 閿�姣佹樉绀哄彞鏌�
+ if (_lightPic != IntPtr.Zero)
+ {
+ Pilot2D.ClearAllPoints(_lightPic);
+ Pilot2D.DestroyPilot2DEntry(_lightPic);
+ _lightPic = IntPtr.Zero;
+ }
- public override bool GetGain(out double gain)
- {
- gain = 0;
- return true;
- }
+ PHM6000Profiler.DestroyCameraEntry(_cameraHandle);
+ _cameraHandle = IntPtr.Zero;
+ _isConnected = false;
- public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
- {
- lineStatus = LineStatus.Hight;
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Closed and cleaned up");
+ }
return true;
}
public override List<string> GetListEnum()
{
- return new List<string>();
+ List<string> cameraList = new List<string>();
+ IntPtr tempHandle = IntPtr.Zero;
+ try
+ {
+ tempHandle = PHM6000Profiler.CreateCameraEntry();
+ if (tempHandle != IntPtr.Zero)
+ {
+ int count = PHM6000Profiler.DiscoverCameras(tempHandle);
+ for (int i = 0; i < count; i++)
+ {
+ byte[] moduleTypeBytes = new byte[64];
+ byte[] serialNumberBytes = new byte[64];
+ 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);
+ }
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera: GetListEnum寮傚父 - {ex.Message}");
+ }
+ finally
+ {
+ if (tempHandle != IntPtr.Zero)
+ PHM6000Profiler.DestroyCameraEntry(tempHandle);
+ }
+ return cameraList;
}
- public override bool GetTriggerDelay(out double delay)
+ private void InitBuffer()
{
- delay = 0;
- return true;
- }
-
- public override bool GetTriggerFliter(out double flitertime)
- {
- flitertime = 0;
- return true;
- }
-
- public override bool GetTriggerMode(out TriggerMode mode, out TriggerSource source)
- {
- mode = TriggerMode.On;
- source = TriggerSource.Line0;
- return true;
- }
-
- public override bool GetTriggerPolarity(out TriggerPolarity polarity)
- {
- polarity = TriggerPolarity.RisingEdge;
- return true;
- }
-
- public override bool InitDevice(string SN, object Handle = null)
- {
- return true;
- }
-
- public override bool SetExpouseTime(double value)
- {
- return true;
- }
-
- public override bool SetGain(double gain)
- {
- return true;
- }
-
- public override bool SetLineMode(IOLines line, LineMode mode)
- {
- return true;
- }
-
- public override bool SetLineStatus(IOLines line, LineStatus linestatus)
- {
- return true;
- }
-
- public override bool SetTriggerDelay(double delay)
- {
- return true;
- }
-
- public override bool SetTriggerFliter(double flitertime)
- {
- return true;
- }
-
- public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0)
- {
- return true;
- }
-
- public override bool SetTriggerPolarity(TriggerPolarity polarity)
- {
- return true;
- }
-
- public override bool SoftTrigger()
- {
- return true;
+ lock (_bitmapLock)
+ {
+ _currentBitmapHeight = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
+ // 瀹藉害鏆傛椂鏈煡锛屽皢鍦ㄧ涓�琛屾暟鎹埌杈炬椂鍒濆鍖�
+ _currentBitmapWidth = 0;
+ if (_currentBitmap != null)
+ {
+ _currentBitmap.Dispose();
+ _currentBitmap = null;
+ }
+ _rawPixelBuffer = null;
+ _currentLineCount = 0;
+ }
}
public override bool StartGrabbing()
{
- return true;
+ // 绾挎壂鐩告満榛樿浣跨敤杩炵画閲囬泦妯″紡
+ // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
+ return StartSingleGrab();
+ }
+
+ /// <summary>
+ /// 鍗曟閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
+ /// 鍙傝�僉LSystem绀轰緥鐨凷tartScan鏂规硶锛歋etAcquisitionMode(1, 0)
+ /// 浣跨敤SDK鑷姩鏄剧ず妯″紡锛屼笉鎵嬪姩澶勭悊鏁版嵁
+ /// </summary>
+ public bool StartSingleGrab()
+ {
+ if (!_isConnected) return false;
+
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Starting single grab mode");
+
+ // 浣跨敤SDK鑷姩鏄剧ず妯″紡锛岀被浼糒LSystem绀轰緥
+ // 涓嶈缃瓵cquisitionCallbackZA锛岃SDK鑷姩澶勭悊鍥惧儚鏄剧ず
+ PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
+
+ // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�0=鍗曟妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
+ 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");
+
+ // 浣跨敤SDK鑷姩鏄剧ず妯″紡锛岀被浼糒LSystem绀轰緥
+ // 涓嶈缃瓵cquisitionCallbackZA锛岃SDK鑷姩澶勭悊鍥惧儚鏄剧ず
+ PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
+
+ // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�1=杩炵画妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
+ 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()
{
+ if (!_isConnected) return true;
+ PHM6000Profiler.StopAcquisition(_cameraHandle);
+ isGrabbing = false;
return true;
}
+
+
+ public override bool StartWith_SoftTriggerModel()
+ {
+ // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岃蒋浠惰Е鍙戣繛缁噰闆�
+ // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
+ return StartContinuousGrab();
+ }
+
+ public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
+ {
+ // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岀‖浠惰Е鍙戜篃浣跨敤杩炵画閲囬泦妯″紡
+ // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
+ // 澶栭儴纭欢淇″彿浼氳Е鍙戠浉鏈哄紑濮嬮噰闆�
+ return StartSingleGrab();
+ }
+
+ public override bool SoftTrigger()
+ {
+ // 绾挎壂鐩告満閫氬父涓嶉渶瑕佷紶缁熻蒋瑙﹀彂锛屼絾鍦ㄦ煇浜涙ā寮忎笅鍙ā鎷�
+ return 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 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 SetTriggerPolarity(TriggerPolarity polarity) => true;
+ public override bool GetTriggerPolarity(out TriggerPolarity polarity) { polarity = TriggerPolarity.RisingEdge; return true; }
+ public override bool SetTriggerFliter(double flitertime) => true;
+ public override bool GetTriggerFliter(out double flitertime) { flitertime = 0; return true; }
+ public override bool SetTriggerDelay(double delay) => true;
+ public override bool GetTriggerDelay(out double delay) { delay = 0; return true; }
+ public override bool SetLineMode(IOLines line, LineMode mode) => true;
+ 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)
+ {
+ bitmap = null;
+ if (!_isConnected) return false;
+
+ // 瀵逛簬绾挎壂鐩告満锛屼娇鐢⊿DK鑷姩鏄剧ず妯″紡
+ // 娉ㄦ剰锛歋DK鑷姩鏄剧ず妯″紡涓嬶紝鎴戜滑鏃犳硶鐩存帴鑾峰彇Bitmap
+ // 浣哠DK浼氳嚜鍔ㄥ皢鍥惧儚鏄剧ず鍒癬lightPic鍙ユ焺瀵瑰簲鐨勬帶浠朵笂
+ // 鎴戜滑鍙渶瑕佸惎鍔ㄥ崟娆¢噰闆嗭紝绛夊緟閲囬泦瀹屾垚鍗冲彲
+
+ 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();
+ }
+ };
+
+ this.ImageGrabbed += handler;
+
+ try
+ {
+ // 浣跨敤鍗曟閲囬泦妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
+ if (StartSingleGrab())
+ {
+ // 绛夊緟閲囬泦瀹屾垚
+ if (waitHandle.WaitOne(outtime))
+ {
+ // 鐢变簬SDK鑷姩鏄剧ず锛屾垜浠棤娉曡繑鍥濨itmap
+ // 浣嗛噰闆嗗凡瀹屾垚锛屽浘鍍忓凡鏄剧ず鍒扮晫闈笂
+ bitmap = null; // SDK鑷姩鏄剧ず妯″紡涓嬩笉杩斿洖Bitmap
+ return captured;
+ }
+ }
+ }
+ finally
+ {
+ this.ImageGrabbed -= handler;
+ StopGrabbing();
+ }
+ }
+ return false;
+ }
+
+ public PHM6000SensorConfig GetSensorConfig()
+ {
+ SyncConfigFromCamera();
+ return _sensorConfig;
+ }
+
+ 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);
+ }
+
+ #endregion
+
+ #endregion
+
+ #region Private Callback & Helpers
+
+ private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
+ {
+ // nOption鍚箟:
+ // 0 = 涓�鎵规暟鎹粨鏉�
+ // 1 = 鍏ㄩ儴閲囬泦瀹屾垚锛堜粎鍗曟妯″紡锛�
+ // 2 = 瀵瑰簲鏁版嵁澶勭悊瀹屾垚锛堟瘡娆¢噰闆嗗畬鎴愰兘浼氳Е鍙戯級
+
+ // 璁板綍鍥炶皟淇℃伅
+ AsyncLogHelper.Info($"LBCamera[{SN}]: OnAcquisitionCompleted called, nOption={nOption}");
+
+ if (nOption == 2)
+ {
+ // 杩炵画閲囬泦妯″紡涓嬶紝姣忓畬鎴愪竴甯DK閮戒細鑷姩缁х画閲囬泦涓嬩竴甯�
+ // SDK浼氳嚜鍔ㄥ鐞嗗浘鍍忔樉绀哄埌_lightPic鍙ユ焺
+ // 鎴戜滑鍙渶瑕佺粺璁¢噰闆嗘鏁�
+ _frameCount++;
+
+ AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} completed (continuous mode, SDK auto-display)");
+
+ // 瑙﹀彂浜嬩欢閫氱煡UI鏇存柊锛堜絾涓嶄紶閫払itmap锛屽洜涓篠DK鑷姩鏄剧ず锛�
+ ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, null, true));
+ }
+ }
+
+ private void SyncConfigFromCamera()
+ {
+ // 浠庣浉鏈鸿鍙栨墍鏈夊弬鏁板苟濉厖鍒� _sensorConfig
+ foreach (EnumNameId id in Enum.GetValues(typeof(EnumNameId)))
+ {
+ int iVal = 0; double dVal = 0; int eVal = 0;
+ if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)id, ref iVal, ref dVal, ref eVal) == 0)
+ {
+ // 瀹為檯椤圭洰涓簲浣跨敤鍙嶅皠灏嗗�煎啓鍥� _sensorConfig
+ }
+ }
+ }
+
+ private bool SetParam(EnumNameId id, float value)
+ {
+ if (!_isConnected) return false;
+ return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, value, 0) == 0;
+ }
+
+ private bool GetParam(EnumNameId id, out float 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 = (float)dVal;
+ return true;
+ }
+ return false;
+ }
+ #endregion
}
}
--
Gitblit v1.9.3