From 0ab0fde3216783ee2694d8d4bfbb94237e25a4bb Mon Sep 17 00:00:00 2001
From: C3031 <shitiangui@lanpucloud.cn>
Date: 星期五, 06 三月 2026 16:52:56 +0800
Subject: [PATCH] 新增了图像增强算法及图像滤波算法

---
 LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs |  794 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 728 insertions(+), 66 deletions(-)

diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index 2bdc26b..810ffd3 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -1,134 +1,796 @@
-锘縰sing OpenCvSharp;
+using LB_SmartVisionCameraDevice.PHM6000;
+using LB_SmartVisionCameraSDK.PHM6000;
+using LB_SmartVisionCommon;
+using LB_VisionProcesses.Cameras;
+using Sunny.UI.Win32;
 using System;
 using System.Collections.Generic;
-using System.Linq;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Reflection;
+using System.Runtime.InteropServices;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 
 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 AcquisitionCallbackZA _acquisitionCallback;
+        private AcquisitionCompletedCallback _acquisitionCompletedCallback;
+        
+        private bool _isConnected = false;
+        private int _frameCount = 0; // 閲囬泦甯ц鏁�
+
+        // 鍥惧儚缂撳啿
+        private byte[] _rawPixelBuffer = null; // 鐢ㄤ簬瀛樺偍鏁村紶鍥剧殑鍍忕礌鏁版嵁 (8bpp)
+        private int _currentBitmapHeight = 0;
+        private int _currentBitmapWidth = 0;
+        private int _currentLineCount = 0;
+        private object _bufferLock = new object();
+        private bool _isBufferReady = false;
+
+        // 涓存椂琛岀紦鍐诧紝鐢ㄤ簬鎺ユ敹鍥炶皟鏁版嵁
+        private byte[] _tempLineBuffer = null;
+        private bool _isContinuous = false;
+        // 鏂板锛欳ollectedImages鎿嶄綔閿侊紝淇濊瘉绾跨▼瀹夊叏
+        private readonly object _collectedImagesLock = new object();
+
+        public LBCamera()
         {
-            return true;
+            Brand = CameraBrand.LBCamera;
+            _sensorConfig = new PHM6000SensorConfig();
+        }
+
+        #region ICamera Implementation
+
+        public override bool InitDevice(string sn, object handle = null)
+        {
+            // 濡傛灉宸茶繛鎺ワ紝鐩存帴杩斿洖true
+            if (_isConnected && _cameraHandle != IntPtr.Zero)
+            {
+                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鎴朓P
+                        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)
+                {
+                    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();
+
+                    // 鍒濆鍖栧苟娉ㄥ唽閲囬泦鍥炶皟 (鑾峰彇鏁版嵁鐢�)
+                    _acquisitionCallback = new AcquisitionCallbackZA(OnAcquisitionCallbackZA);
+                    PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
+
+                    // 鍒濆鍖栧苟娉ㄥ唽閲囬泦瀹屾垚鍥炶皟 (鐘舵�侀�氱煡鐢�)
+                    _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
+                    PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
+
+                    // 寮哄埗搴旂敤褰撳墠閰嶇疆锛堢‘淇濊Е鍙戞ā寮忕瓑鍙傛暟姝g‘锛岄伩鍏嶇浉鏈哄浜庢湭鐭ョ姸鎬侊級
+                    UpdateSensorConfig(_sensorConfig);
+
+                    AsyncLogHelper.Info($"LBCamera[{SN}]: Connected and initialized successfully (Manual Data 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;
-        }
-
-        public override bool GetExpouseTime(out double value)
-        {
-            value = 0;
-            return true;
-        }
-
-        public override bool GetGain(out double gain)
-        {
-            gain = 0;
-            return true;
-        }
-
-        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
-        {
-            lineStatus = LineStatus.Hight;
+            if (_isConnected && _cameraHandle != IntPtr.Zero)
+            {
+                StopGrabbing();
+                PHM6000Profiler.DestroyCameraEntry(_cameraHandle);
+                _cameraHandle = IntPtr.Zero;
+                _isConnected = false;
+                AsyncLogHelper.Info($"LBCamera[{SN}]: Closed");
+            }
             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');
+                            if (!string.IsNullOrEmpty(sn))
+                            {
+                                cameraList.Add(sn);
+                            }
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                AsyncLogHelper.Error($"LBCamera: 鑾峰彇璁惧鍒楄〃寮傚父 - {ex.Message}");
+            }
+            finally
+            {
+                if (tempHandle != IntPtr.Zero)
+                    PHM6000Profiler.DestroyCameraEntry(tempHandle);
+            }
+            return cameraList;
         }
 
-        public override bool GetTriggerDelay(out double delay)
+        private void InitBuffer()
         {
-            delay = 0;
+            lock (_bufferLock)
+            {
+                _currentBitmapHeight = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
+                // 瀹藉害鍦ㄧ涓�琛屾暟鎹埌杈炬椂纭畾
+                _currentBitmapWidth = 0;
+                _rawPixelBuffer = null;
+                _currentLineCount = 0;
+                _isBufferReady = false;
+            }
+        }
+
+        public override bool StartGrabbing()
+        {
+            // 榛樿杩炵画妯″紡
+            return StartSingleGrab();
+        }
+
+        public bool StartSingleGrab()
+        {
+            if (!_isConnected) return false;
+            
+            _isContinuous = false;
+            InitBuffer();
+            AsyncLogHelper.Info($"LBCamera[{SN}]: 寮�濮嬪崟娆¢噰闆�");
+
+            // 1=鎵弿妯″紡, 0=鍗曟
+            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 0);
+            int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
+
+            if (result == 0)
+            {
+                isGrabbing = true;
+                return true;
+            }
+            return false;
+        }
+
+        public override bool StartContinuousGrab()
+        {
+            if (!_isConnected) return false;
+
+            _isContinuous = true;
+            InitBuffer();
+            AsyncLogHelper.Info($"LBCamera[{SN}]:寮�濮嬭繛缁噰闆�");
+
+            // 1=鎵弿妯″紡, 1=杩炵画
+            PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 1);
+            int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
+
+            if (result == 0)
+            {
+                isGrabbing = true;
+                return true;
+            }
+            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 GetTriggerFliter(out double flitertime)
+        public override bool StartWith_SoftTriggerModel() => StartContinuousGrab();
+
+        public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0) => StartSingleGrab();
+
+        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, (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)
         {
-            flitertime = 0;
+            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 = TriggerSource.Line0;
+            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;
+        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;
+
+            // 璁$畻鐞嗚鏈�灏忚�楁椂 (浠呭綋浣跨敤鍥哄畾棰戠巼瑙﹀彂鏃�)
+            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))
+            {
+                Bitmap res = null;
+                EventHandler<CameraEventArgs> handler = (s, e) => {
+                    if(e.Bitmap != null) {
+                         res = e.Bitmap.Clone() as Bitmap;
+                         waitHandle.Set();
+                    }
+                };
+                ImageGrabbed += handler;
+                
+                if (StartSingleGrab())
+                {
+                    if (!waitHandle.WaitOne(actualTimeout))
+                    {
+                        AsyncLogHelper.Error($"LBCamera: GetImageWithSoftTrigger timeout after {actualTimeout}ms");
+                    }
+                }
+                else
+                {
+                    AsyncLogHelper.Error("LBCamera: StartSingleGrab failed");
+                }
+
+                ImageGrabbed -= handler;
+                
+                // 纭繚鍋滄閲囬泦
+                StopGrabbing();
+                
+                bitmap = res;
+                return bitmap != null;
+            }
         }
 
-        public override bool GetTriggerPolarity(out TriggerPolarity polarity)
+        public PHM6000SensorConfig GetSensorConfig()
         {
-            polarity = TriggerPolarity.RisingEdge;
-            return true;
+            SyncConfigFromCamera();
+            return _sensorConfig;
         }
 
-        public override bool InitDevice(string SN, object Handle = null)
+        public void UpdateSensorConfig(PHM6000SensorConfig config)
         {
-            return true;
+            _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);
         }
 
-        public override bool SetExpouseTime(double value)
+        #endregion
+        
+        #endregion
+
+        #region Callbacks
+
+        private void OnAcquisitionCallbackZA(IntPtr pInstance, IntPtr buffer, int points)
         {
-            return true;
+            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 鍙互澶嶇敤锛屼笉闇�瑕佺疆绌�
+                }
+            }
         }
 
-        public override bool SetGain(double gain)
+        private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
         {
-            return true;
+            // nOption: 0=Batch End, 1=All End(Single), 2=Processing End
+            // 姝ゅ涓昏鐢ㄤ簬鏃ュ織鎴栫姸鎬佺洃鎺�
+            // 瀹為檯鍥惧儚鐢熸垚鍦� Data Callback 涓畬鎴�
+            if (nOption == 1) // 鍗曟閲囬泦缁撴潫
+            {
+                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;
+                        }
+                    }
+                }
+            }
         }
 
-        public override bool SetLineMode(IOLines line, LineMode mode)
+        private void CreateAndFireBitmap()
         {
-            return true;
+            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})");
+                //绌哄�兼牎楠岋細杞崲澶辫触鍒欑洿鎺ヨ繑鍥�
+                if (bmp == null)
+                {
+                    AsyncLogHelper.Warn(SN + "甯ц浆鎹负Bitmap澶辫触锛岃烦杩囧鐞�");
+                    return;
+                }
+                // 绾跨▼瀹夊叏鍦板皢Bitmap娣诲姞鍒癈ollectedImages瀛楀吀
+                lock (_collectedImagesLock)
+                {
+                    // 纭繚褰撳墠鐩告満SN瀵瑰簲鐨勫垪琛ㄥ瓨鍦�
+                    if (!CollectedImages.ContainsKey(SN))
+                    {
+                        CollectedImages[SN] = new List<Bitmap>();
+                    }
+                    CollectedImages[SN].Add(bmp);
+                    AsyncLogHelper.Info(SN + $"鍥惧儚宸插姞鍏ョ紦瀛橈紝褰撳墠缂撳瓨鏁伴噺锛歿CollectedImages[SN].Count}");
+                }
+
+                // 澶勭悊CollectedImages涓殑鍥惧儚锛氶亶鍘嗘秷璐瑰垪琛ㄧ涓�涓厓绱犵洿鍒颁负绌�
+                ProcessCollectedImages();
+                //// 寮傛瑙﹀彂浜嬩欢锛岄伩鍏嶉樆濉濻DK鍥炶皟绾跨▼
+                //Task.Factory.StartNew(() => 
+                //{
+                //    try
+                //    {
+                //        ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, bmp, true));
+                //        CallBackImg = (Bitmap)bmp.Clone();
+                //        if (CallBackImg == null)
+                //        {
+                //            return;
+                //        }
+                //        if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
+                //        {
+                //            if (mode == TriggerMode.On && source != TriggerSource.Software)
+                //                TriggerRunMessageReceived?.Invoke(SN, source.ToString());  // 瑙﹀彂杩愯浜嬩欢
+                //        }
+                //        bmp.Dispose();
+                //    }
+                //    catch (Exception ex)
+                //    {
+                //        AsyncLogHelper.Error($"LBCamera: Event Invoke error - {ex.Message}");
+                //        bmp.Dispose(); // 寮傚父鏃堕噴鏀捐祫婧�
+                //    }
+                //});
+            }
+            catch (Exception ex)
+            {
+                AsyncLogHelper.Error($"LBCamera: CreateBitmap error - {ex.Message}");
+            }
+        }
+        /// <summary>
+        /// 澶勭悊CollectedImages涓殑缂撳瓨鍥惧儚
+        /// 鏍稿績閫昏緫锛氶亶鍘嗗彇绗竴涓浘鍍� -> 璧嬪�肩粰CallBackImg -> 瑙﹀彂浜嬩欢 -> 閲婃斁骞剁Щ闄�
+        /// </summary>
+        private void ProcessCollectedImages()
+        {
+            Task.Factory.StartNew(() =>
+            {
+                // 鍔犻攣淇濊瘉绾跨▼瀹夊叏锛岄槻姝㈠绾跨▼鍚屾椂鎿嶄綔鍒楄〃
+                lock (_collectedImagesLock)
+                {
+                    // 鏍¢獙褰撳墠鐩告満鐨勫浘鍍忓垪琛ㄦ槸鍚﹀瓨鍦ㄤ笖鏈夋暟鎹�
+                    if (!CollectedImages.ContainsKey(SN) || CollectedImages[SN].Count == 0)
+                    {
+                        AsyncLogHelper.Info(SN + "褰撳墠鏃犵紦瀛樺浘鍍忥紝璺宠繃澶勭悊");
+                        return;
+                    }
+                    // 寰幆澶勭悊锛氱洿鍒板垪琛ㄤ负绌�
+                    while (CollectedImages[SN].Count > 0)
+                    {
+                        try
+                        {
+                            // 1 鍙栧垪琛ㄧ涓�涓储寮曠殑鍥惧儚璧嬪�肩粰CallBackImg
+                            Bitmap firstBitmap = CollectedImages[SN][0];
+                            ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, firstBitmap, true));
+                            CallBackImg = (Bitmap)firstBitmap.Clone(); // 鍏嬮殕閬垮厤鍘熷璞¤閲婃斁鍚庡紩鐢ㄥけ鏁�
+
+                            // 2 鑾峰彇瑙﹀彂妯″紡骞跺垽鏂槸鍚﹁Е鍙戣繍琛屼簨浠�
+                            if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
+                            {
+                                // 纭Е鍙戞ā寮忎笅瑙﹀彂杩愯浜嬩欢
+                                if (mode == TriggerMode.On && source != TriggerSource.Software)
+                                {
+                                    AsyncLogHelper.Info(SN + $"瑙﹀彂纭Е鍙戜簨浠讹紝瑙﹀彂婧愶細{source}");
+                                    TriggerRunMessageReceived?.Invoke(SN, source.ToString());
+                                }
+                            }
+                            else
+                            {
+                                AsyncLogHelper.Warn(SN + "鑾峰彇瑙﹀彂妯″紡澶辫触锛岃烦杩囦簨浠惰Е鍙�");
+                            }
+
+                            // 3 閲婃斁绗竴涓浘鍍忚祫婧愬苟浠庡垪琛ㄧЩ闄�
+                            // 鍏堥噴鏀綛itmap鍐呭瓨锛屽啀绉婚櫎鍒楄〃鍏冪礌
+                            firstBitmap.Dispose();
+                            CollectedImages[SN].RemoveAt(0);
+                            AsyncLogHelper.Info(SN + $"宸叉秷璐圭紦瀛樺浘鍍忥紝鍓╀綑缂撳瓨鏁伴噺锛歿CollectedImages[SN].Count}");
+                        }
+                        catch (Exception ex)
+                        {
+                            AsyncLogHelper.Error(SN + $"澶勭悊缂撳瓨鍥惧儚寮傚父锛歿ex.Message}", ex);
+                            // 鍗曚釜鍥惧儚澶勭悊澶辫触鏃讹紝绉婚櫎璇ュ浘鍍忛伩鍏嶉樆濉炲悗缁鐞�
+                            if (CollectedImages[SN].Count > 0)
+                            {
+                                try
+                                {
+                                    CollectedImages[SN][0]?.Dispose(); // 灏濊瘯閲婃斁
+                                    CollectedImages[SN].RemoveAt(0);
+                                }
+                                catch (Exception innerEx)
+                                {
+                                    AsyncLogHelper.Error(SN + $"娓呯悊寮傚父鍥惧儚澶辫触锛歿innerEx.Message}", innerEx);
+                                }
+                            }
+                            // 鍗曚釜鍥惧儚澶勭悊澶辫触涓嶇粓姝㈠惊鐜紝缁х画澶勭悊涓嬩竴涓�
+                            // 4. 鎵�鏈夊浘鍍忓鐞嗗畬鎴愬悗锛屾竻绌篊allBackImg
+                            if (CallBackImg != null)
+                            {
+                                CallBackImg.Dispose();
+                                CallBackImg = null;
+                            }
+                            continue;
+                        }
+                        // 4. 鎵�鏈夊浘鍍忓鐞嗗畬鎴愬悗锛屾竻绌篊allBackImg
+                        if (CallBackImg != null)
+                        {
+                            CallBackImg.Dispose();
+                            CallBackImg = null;
+                        }
+                    }
+                }
+            });
         }
 
-        public override bool SetLineStatus(IOLines line, LineStatus linestatus)
+
+        private void SyncConfigFromCamera()
         {
-            return true;
+            try
+            {
+                if (!_isConnected) return;
+
+                PropertyInfo[] props = _sensorConfig.GetType().GetProperties();
+                foreach (PropertyInfo p in props)
+                {
+                    // 璺宠繃鑷畾涔夊弬鏁�
+                    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}");
+            }
         }
 
-        public override bool SetTriggerDelay(double delay)
+        private bool SetParam(EnumNameId id, float value)
         {
-            return true;
+            if (!_isConnected) return false;
+            return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, value, 0) == 0;
         }
 
-        public override bool SetTriggerFliter(double flitertime)
+        private bool SetParam(EnumNameId id, int value)
         {
-            return true;
+            if (!_isConnected) return false;
+            // 瀵逛簬鏋氫妇绫诲瀷锛岄�氬父閫氳繃 enumValue (鏈�鍚庝竴涓弬鏁�) 浼犻��
+            return PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)id, 0, 0, value) == 0;
         }
 
-        public override bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0)
+        private bool GetParam(EnumNameId id, out float value)
         {
-            return true;
+            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;
         }
 
-        public override bool SetTriggerPolarity(TriggerPolarity polarity)
+        private bool GetParam(EnumNameId id, out int value)
         {
-            return true;
+            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;
         }
-
-        public override bool SoftTrigger()
-        {
-            return true;
-        }
-
-        public override bool StartGrabbing()
-        {
-            return true;
-        }
-
-        public override bool StopGrabbing()
-        {
-            return true;
-        }
+        #endregion
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3