From 7279c77f318cd7e38af279dc98a1fecec33f5e30 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期四, 08 一月 2026 16:07:01 +0800
Subject: [PATCH] 优化相机采集逻辑与亮度图处理功能

---
 LB_SmartVision/VisionForm.cs                        |    2 
 LB_VisionProcesses/Cameras/BaseCamera.cs            |   19 +
 LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs    |  370 +++++++++++++++++++--------------
 LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs    |    5 
 LB_SmartVisionCameraSDK/PHM6000/PHM6000Profiler.cs  |   29 ++
 LB_SmartVision/Forms/Pages/CameraPage/CameraForm.cs |    4 
 LB_VisionProcesses/Cameras/2DCameraForm.cs          |  207 +++++++++++++++---
 LB_VisionProcesses/Cameras/ICamera.cs               |    9 
 8 files changed, 449 insertions(+), 196 deletions(-)

diff --git a/LB_SmartVision/Forms/Pages/CameraPage/CameraForm.cs b/LB_SmartVision/Forms/Pages/CameraPage/CameraForm.cs
index bec33a9..61d8f3d 100644
--- a/LB_SmartVision/Forms/Pages/CameraPage/CameraForm.cs
+++ b/LB_SmartVision/Forms/Pages/CameraPage/CameraForm.cs
@@ -85,7 +85,9 @@
             else
                 camera.SetTriggerMode(TriggerMode.On, TriggerSource.Line0);
 
-            camera.StartGrabbing();
+            // 娉ㄩ噴鎺夎嚜鍔ㄥ惎鍔ㄩ噰闆嗛�昏緫,闃叉鍏抽棴绐椾綋鍚庣浉鏈鸿嚜鍔ㄨ繛缁噰闆�
+            // 鐢ㄦ埛闇�瑕佹墜鍔ㄧ偣鍑�"鍚姩閲囬泦"鎸夐挳鎵嶄細鍚姩鐩告満
+            // camera.StartGrabbing();
 
             Unsubscribe();
         }
diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index ede0177..0655293 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -699,7 +699,7 @@
                         }
                 }
                 camera.SN = CameraConnectionString.Key;
-                if (!camera.InitDevice(CameraConnectionString.Key, this.Handle))
+                if (!camera.InitDevice(CameraConnectionString.Key, IntPtr.Zero))
                 {
                     LogInfo($"鍒濆鍖栫浉鏈篬{CameraConnectionString.Key}]澶辫触", LogInfoType.ERROR);
                     if (camera != null)
diff --git a/LB_SmartVisionCameraSDK/PHM6000/PHM6000Profiler.cs b/LB_SmartVisionCameraSDK/PHM6000/PHM6000Profiler.cs
index 44cdddf..fdee162 100644
--- a/LB_SmartVisionCameraSDK/PHM6000/PHM6000Profiler.cs
+++ b/LB_SmartVisionCameraSDK/PHM6000/PHM6000Profiler.cs
@@ -874,6 +874,35 @@
         /// <param name="KeepImageSize">鍥惧儚澶у皬鏄惁淇濇寔</param>
         [DllImport("PHM6000API", CallingConvention = CallingConvention.StdCall, EntryPoint = "SetTransformParameter")]
         public static extern void SetTransformParameter(IntPtr pHandle, float fAngle, int xOffset, int yOffset, int hMirror, int vMirror, int KeepImageSize = 0);
+
+        #region PointCloud3D Functions
+        
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "AddZAPoints")]
+        public static extern int AddZAPoints(IntPtr pInstance, IntPtr points, int nCount, float fStepX, float fStepY, int nDownSample);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "ClearPCLPoints")]
+        public static extern int ClearPCLPoints(IntPtr pInstance);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "GetPointCloudBound")]
+        public static extern int GetPointCloudBound(IntPtr pInstance, ref double pMin, ref double pMax);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "RenderPCLWindow")]
+        public static extern void RenderPCLWindow(IntPtr pInstance, int nWidth, int nHeight);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "SetLookUpTableRange")]
+        public static extern void SetLookUpTableRange(IntPtr pInstance, double fMin, double fMax);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "ShowCubeAxes")]
+        public static extern void ShowCubeAxes(IntPtr pInstance, int bInit);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "ShowLookUpTable")]
+        public static extern void ShowLookUpTable(IntPtr pInstance, double x, double y, double xWide, double yWide);
+
+        [DllImport("PointCloud3D", CallingConvention = CallingConvention.StdCall, EntryPoint = "UpdatePCLPointColors")]
+        public static extern int UpdatePCLPointColors(IntPtr pInstance, string szColoring);
+
+        #endregion
+
         /// <summary>
         /// 灏嗙粨鏋勪綋鎸囬拡杞崲涓篸ouble锛堟ā鎷烠++鐨�(double)(int64)&鎿嶄綔锛�
         /// </summary>
diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index 107eba5..adccb8b 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -252,7 +252,18 @@
                         return;
                 }
 
-                if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), this.Handle))
+                IntPtr displayHandle = onlinePictureBox.Handle;
+                if (brand == CameraBrand.LBCamera)
+                {
+                    onlinePictureBox.Visible = false;
+                    displayHandle = this.panel_Picture.Handle;
+                }
+                else
+                {
+                    onlinePictureBox.Visible = true;
+                }
+
+                if (cmbSN.Items.Count > 0 && camera.InitDevice(cmbSN.Text.ToString(), displayHandle))
                 {
                     camera.ImageGrabbed -= GetImageBllComplete;
                     camera.ImageGrabbed += GetImageBllComplete;
@@ -295,8 +306,10 @@
 
             if (camera.CloseDevice())
             {
+                onlinePictureBox.Visible = true;
                 MessageBox.Show(camera.SN + "鏂紑鎴愬姛");
                 this.panel_Picture.Controls.Clear();
+                this.panel_Picture.Controls.Add(onlinePictureBox);
             }
         }
 
@@ -382,28 +395,42 @@
 
         /// <summary>
         /// 鐩告満鍥炶皟杩愯
+        /// 娉ㄦ剰锛氬浜嶭BCamera锛�3D绾挎壂鐩告満锛夛紝浣跨敤SDK鑷姩鏄剧ず妯″紡
+        /// SDK浼氳嚜鍔ㄥ皢鍥惧儚鏄剧ず鍒板搴旂殑鎺т欢涓婏紝姝ゅ洖璋冨彧鐢ㄤ簬缁熻閲囬泦娆℃暟
         /// </summary>
         /// <param name="CCDName"></param>
         /// <param name="image"></param>
         private void GetImageBllComplete(object sender, CameraEventArgs e)
         {
+            // 瀵逛簬LBCamera锛圫DK鑷姩鏄剧ず妯″紡锛夛紝Bitmap涓簄ull锛屾垜浠彧闇�瑕佺粺璁�
+            if (camera.Brand == CameraBrand.LBCamera)
+            {
+                if (e is LBCameraEventArgs args && args.IsComplete)
+                {
+                    total.iImageCount++;
+                    // 涓嶉渶瑕佹洿鏂皁nlinePictureBox.Image锛孲DK浼氳嚜鍔ㄦ樉绀�
+                }
+                return;
+            }
+
+            // 瀵逛簬2D鐩告満锛堟墜鍔ㄥ鐞嗘ā寮忥級
             if (e.Bitmap == null)
                 return;
 
-                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 +453,121 @@
 
             Task.Factory.StartNew(() =>
             {
-                //camera.GetCamConfig(out CameraConfig OriCamConfig);
-
+                // 璁剧疆鏇濆厜鍜屽鐩�
                 camera.SetExpouseTime(Convert.ToDouble(txtExp.Text));
                 camera.SetGain(Convert.ToDouble(txtGain.Text));
 
-                camera.GetImageWithSoftTrigger(out Bitmap bitmap);
+                bool success = false;
+                Bitmap bitmap = null;
 
-                this.BeginInvoke(new Action(() =>
+                // 璋冪敤鍩虹被鎺ュ彛鐨勫崟娆¢噰闆嗘柟娉�
+                // LBCamera閲嶅啓浜哠tartSingleGrab鏂规硶锛屽叾浠栫浉鏈鸿皟鐢ㄨ繑鍥瀎alse
+                success = camera.StartSingleGrab();
+
+                if (success)
                 {
-                    if (bitmap != null)
-                        this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
-                    else
-                        this.lblCapTime.Text = "-1ms";
-                }));
+                    // 瀵逛簬LBCamera锛圫DK鑷姩鏄剧ず妯″紡锛夛紝绛夊緟閲囬泦瀹屾垚鍗冲彲
+                    // 瀵逛簬2D鐩告満锛岀瓑寰匓itmap浜嬩欢
+                    if (camera.Brand == CameraBrand.LBCamera)
+                    {
+                        // SDK鑷姩鏄剧ず妯″紡涓嬶紝绛夊緟閲囬泦瀹屾垚
+                        using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+                        {
+                            bool captured = false;
+                            EventHandler<CameraEventArgs> handler = (s, evt) =>
+                            {
+                                if (evt is LBCameraEventArgs args && args.IsComplete)
+                                {
+                                    captured = true;
+                                    waitHandle.Set();
+                                }
+                            };
 
-                //澶嶅師鍘熼�氳鍙h缃�
-                //camera.SetCamConfig(OriCamConfig);
+                            camera.ImageGrabbed += handler;
+                            try
+                            {
+                                // 绛夊緟5绉掕秴鏃�
+                                if (waitHandle.WaitOne(5000))
+                                {
+                                    // SDK鑷姩鏄剧ず妯″紡锛屼笉杩斿洖Bitmap
+                                    // 浣嗛噰闆嗗凡瀹屾垚
+                                }
+                                this.BeginInvoke(new Action(() =>
+                                {
+                                    this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+                                }));
+                            }
+                            finally
+                            {
+                                camera.ImageGrabbed -= handler;
+                                camera.StopGrabbing();
+                            }
+                        }
+                    }
+                    else
+                    {
+                        // 瀵逛簬2D鐩告満锛岀瓑寰呭浘鍍忔暟鎹�
+                        using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+                        {
+                            Bitmap captured = null;
+                            EventHandler<CameraEventArgs> handler = (s, evt) =>
+                            {
+                                // 瀵逛簬2D鐩告満锛岀洿鎺ユ帴鏀跺浘鍍�
+                                if (!(evt is LBCameraEventArgs) && evt.Bitmap != null)
+                                {
+                                    captured = evt.Bitmap.Clone() as Bitmap;
+                                    waitHandle.Set();
+                                }
+                            };
+
+                            camera.ImageGrabbed += handler;
+                            try
+                            {
+                                // 绛夊緟5绉掕秴鏃�
+                                if (waitHandle.WaitOne(5000))
+                                {
+                                    bitmap = captured;
+                                }
+                            }
+                            finally
+                            {
+                                camera.ImageGrabbed -= handler;
+                                camera.StopGrabbing();
+                            }
+                        }
+
+                        this.BeginInvoke(new Action(() =>
+                        {
+                            if (bitmap != null)
+                            {
+                                this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+                                onlinePictureBox.Image = bitmap;
+                            }
+                            else
+                            {
+                                this.lblCapTime.Text = "-1ms";
+                            }
+                        }));
+                    }
+                }
+                else
+                {
+                    // 濡傛灉StartSingleGrab澶辫触锛屽洖閫�鍒颁紶缁熺殑GetImageWithSoftTrigger
+                    camera.GetImageWithSoftTrigger(out bitmap);
+
+                    this.BeginInvoke(new Action(() =>
+                    {
+                        if (bitmap != null)
+                        {
+                            this.lblCapTime.Text = $"{(DateTime.Now - StartTime).TotalMilliseconds}ms";
+                            onlinePictureBox.Image = bitmap;
+                        }
+                        else
+                        {
+                            this.lblCapTime.Text = "-1ms";
+                        }
+                    }));
+                }
             });
         }
 
@@ -453,12 +578,12 @@
 
             total.Clear();
 
-            // 灏濊瘯灏嗚緭鍏ュ瓧绗︿覆杞崲涓烘灇涓惧��
-            if (Enum.TryParse(cmbBrand.Text, true, out CameraBrand brand))
-            {
-                camera.StopGrabbing();
-                camera.StartWith_HardTriggerModel();
-            }
+            // 鍋滄褰撳墠閲囬泦
+            camera.StopGrabbing();
+
+            // 璋冪敤鍩虹被鎺ュ彛鐨勮繛缁噰闆嗘柟娉�
+            // LBCamera浼氳皟鐢⊿tartContinuousGrab鏂规硶锛屽叾浠栫浉鏈轰娇鐢ㄥ師鏈夌殑StartWith_HardTriggerModel
+            camera.StartContinuousGrab();
 
             startGrabtime = DateTime.Now;
 
@@ -473,18 +598,15 @@
 
             total.Clear();
 
-            // 灏濊瘯灏嗚緭鍏ュ瓧绗︿覆杞崲涓烘灇涓惧��
-            if (Enum.TryParse(cmbBrand.Text, true, out CameraBrand brand))
-            {
-                Task.Factory.StartNew(() =>
-                {
-                    camera.StopGrabbing();
-                    camera.StartWith_SoftTriggerModel();
+            // 鍋滄褰撳墠閲囬泦
+            camera.StopGrabbing();
 
-                });
-            }
+            // 璋冪敤鍩虹被鎺ュ彛鐨勮繛缁噰闆嗘柟娉�
+            // LBCamera浼氳皟鐢⊿tartContinuousGrab鏂规硶锛屽叾浠栫浉鏈轰娇鐢ㄥ師鏈夌殑StartWith_SoftTriggerModel
+            camera.StartContinuousGrab();
 
             startGrabtime = DateTime.Now;
+
             cmbSN.Enabled = false;
             cmbBrand.Enabled = false;
         }
@@ -655,6 +777,10 @@
 
         private void CameraForm_FormClosed(object sender, FormClosedEventArgs e)
         {
+            onlinePictureBox.Visible = true;
+            this.panel_Picture.Controls.Clear();
+            this.panel_Picture.Controls.Add(onlinePictureBox);
+            
             this.onlinePictureBox.Image = null;
             if (camera != null)
             {
@@ -667,7 +793,12 @@
                 else
                     camera.SetTriggerMode(TriggerMode.On, actualSource);
 
-                camera.StartGrabbing();
+                // LBCamera鍦⊿tartGrabbing鏃朵細鐩存帴寮�鍚縺鍏夊拰閲囬泦锛屽洜姝ゅ叧闂獥鍙f椂涓嶅簲鑷姩閲嶅惎閲囬泦
+                // 鍏朵粬鐩告満(濡�2D鐩告満)閫氬父闇�瑕佷繚鎸丟rabbing鐘舵�佷互鎺ユ敹瑙﹀彂
+                if (camera.Brand != CameraBrand.LBCamera)
+                {
+                    camera.StartGrabbing();
+                }
             }
 
             //璺緞涓虹┖璇存槑涓烘祴璇曟ā寮忥紝闇�瑕侀噴鏀剧浉鏈�
diff --git a/LB_VisionProcesses/Cameras/BaseCamera.cs b/LB_VisionProcesses/Cameras/BaseCamera.cs
index 4f9d877..215aff7 100644
--- a/LB_VisionProcesses/Cameras/BaseCamera.cs
+++ b/LB_VisionProcesses/Cameras/BaseCamera.cs
@@ -186,12 +186,29 @@
 
         #region  protected abstract
         /// <summary>
-        /// 寮�濮嬮噰鍥�
+        /// 寮�濮嬮噰鍥撅紙榛樿杩炵画閲囬泦锛�
         /// </summary>
         /// <returns></returns>
         public abstract bool StartGrabbing();
 
         /// <summary>
+        /// 鍗曟閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
+        /// </summary>
+        /// <returns></returns>
+        public virtual bool StartSingleGrab()
+        {
+            // 榛樿瀹炵幇锛氭櫘閫氱浉鏈轰笉鏀寔鍗曟閲囬泦锛岀洿鎺ヨ繑鍥瀎alse
+            // 绾挎壂鐩告満闇�瑕侀噸鍐欐鏂规硶
+            return false;
+        }
+
+        /// <summary>
+        /// 杩炵画閲囬泦妯″紡
+        /// </summary>
+        /// <returns></returns>
+        public abstract bool StartContinuousGrab();
+
+        /// <summary>
         /// 鍋滄閲囧浘
         /// </summary>
         /// <returns></returns>
diff --git a/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs b/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
index 48d15c7..685b06d 100644
--- a/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
+++ b/LB_VisionProcesses/Cameras/HRCameras/HRCamera.cs
@@ -1247,6 +1247,11 @@
             config.Params.Inputs.Add("澧炵泭", gain);
         }
 
+        public override bool StartContinuousGrab()
+        {
+            return true;
+        }
+
         #endregion
     }
 }
diff --git a/LB_VisionProcesses/Cameras/ICamera.cs b/LB_VisionProcesses/Cameras/ICamera.cs
index e173d9c..27c6c3a 100644
--- a/LB_VisionProcesses/Cameras/ICamera.cs
+++ b/LB_VisionProcesses/Cameras/ICamera.cs
@@ -79,11 +79,18 @@
         bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000);
 
         /// <summary>
-        /// 杞Е鍙�
+        /// 杞Е鍙戝崟娆�
         /// </summary>
         /// <returns></returns>
         bool SoftTrigger();
 
+        /// <summary>
+        /// 杩炵画閲囬泦妯″紡
+        /// </summary>
+        /// <returns></returns>
+        bool StartContinuousGrab();
+
+
         #endregion
 
         #region SettingConfig
diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index a5128a4..c989044 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -13,6 +13,15 @@
 
 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绯诲垪灏佽
@@ -21,14 +30,22 @@
     {
         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 int _currentBitmapHeight = 0;
+        private int _currentBitmapWidth = 0;
+        private object _bitmapLock = new object();
+        private DateTime _lastUpdateTime = DateTime.MinValue;
 
         // 鏄剧ず鍙ユ焺鍜屽嚱鏁帮紙涓嶭LSystem绀轰緥淇濇寔涓�鑷达級
         private IntPtr _lightPic = IntPtr.Zero;
@@ -36,16 +53,71 @@
         private IntPtr _pointPic = IntPtr.Zero;
         private IntPtr _outlinePic = IntPtr.Zero;
 
+        // 鏄剧ず鎺у埗鍑芥暟缁撴瀯浣�
+        private PILOT2D_FUNC _func2d;
+        private VTK3D_FUNC _func3d;
+
         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)
         {
+            // 濡傛灉宸茶繛鎺ワ紝浠呮鏌ユ槸鍚﹂渶瑕佹洿鏂版樉绀哄彞鏌�
+            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
             {
@@ -125,14 +197,41 @@
                     // 鍔犺浇鐩告満褰撳墠鍙傛暟鍒� _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(OnLineReceived);
                     _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
                     
-                    //PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
+                    // 娉ㄥ唽閲囬泦瀹屾垚鍥炶皟锛堢被浼糒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) 
@@ -148,9 +247,20 @@
             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");
             }
             return true;
         }
@@ -194,79 +304,90 @@
             return cameraList;
         }
 
-        public override bool StartGrabbing()
+        private void InitBuffer()
         {
-            if (!_isConnected) return false;
-            lock (_bufferLock)
+            lock (_bitmapLock)
             {
-                _lineDataBuffer.Clear();
+                _currentBitmapHeight = _sensorConfig.ScanLineCount > 0 ? _sensorConfig.ScanLineCount : 5000;
+                // 瀹藉害鏆傛椂鏈煡锛屽皢鍦ㄧ涓�琛屾暟鎹埌杈炬椂鍒濆鍖�
+                _currentBitmapWidth = 0;
+                if (_currentBitmap != null)
+                {
+                    _currentBitmap.Dispose();
+                    _currentBitmap = null;
+                }
+                _rawPixelBuffer = null;
                 _currentLineCount = 0;
             }
+        }
 
-            // 绂佺敤琛屽洖璋�
-            PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
-
-            // 璁剧疆閲囬泦妯″紡锛�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 StartGrabbing()
+        {
+            // 绾挎壂鐩告満榛樿浣跨敤杩炵画閲囬泦妯″紡
+            // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
+            return StartSingleGrab();
         }
 
         /// <summary>
         /// 鍗曟閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
-        /// 璁剧疆閲囬泦妯″紡涓烘壂鎻忔ā寮忥紝鍗曟瑙﹀彂
+        /// 鍙傝�僉LSystem绀轰緥鐨凷tartScan鏂规硶锛歋etAcquisitionMode(1, 0)
+        /// 浣跨敤SDK鑷姩鏄剧ず妯″紡锛屼笉鎵嬪姩澶勭悊鏁版嵁
         /// </summary>
         public bool StartSingleGrab()
         {
             if (!_isConnected) return false;
-            lock (_bufferLock)
-            {
-                _lineDataBuffer.Clear();
-                _currentLineCount = 0;
-            }
 
-            // 绂佺敤琛屽洖璋冿紙涓庣ず渚嬩竴鑷达級
+            AsyncLogHelper.Info($"LBCamera[{SN}]: Starting single grab mode");
+
+            // 浣跨敤SDK鑷姩鏄剧ず妯″紡锛岀被浼糒LSystem绀轰緥
+            // 涓嶈缃瓵cquisitionCallbackZA锛岃SDK鑷姩澶勭悊鍥惧儚鏄剧ず
             PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
 
-            // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�0=鍗曟妯″紡
+            // 璁剧疆閲囬泦妯″紡锛�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 bool StartContinuousGrab()
+        public override bool StartContinuousGrab()
         {
             if (!_isConnected) return false;
-            lock (_bufferLock)
-            {
-                _lineDataBuffer.Clear();
-                _currentLineCount = 0;
-            }
 
-            // 绂佺敤琛屽洖璋冿紙涓庣ず渚嬩竴鑷达級
+            AsyncLogHelper.Info($"LBCamera[{SN}]: Starting continuous grab mode");
+
+            // 浣跨敤SDK鑷姩鏄剧ず妯″紡锛岀被浼糒LSystem绀轰緥
+            // 涓嶈缃瓵cquisitionCallbackZA锛岃SDK鑷姩澶勭悊鍥惧儚鏄剧ず
             PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
 
-            // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�1=杩炵画妯″紡
+            // 璁剧疆閲囬泦妯″紡锛�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;
         }
@@ -279,17 +400,20 @@
             return true;
         }
 
+
         public override bool StartWith_SoftTriggerModel()
         {
-            // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岃蒋浠惰Е鍙戣繛缁噰闆嗕娇鐢ㄨ繛缁噰闆嗘ā寮�
+            // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岃蒋浠惰Е鍙戣繛缁噰闆�
+            // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
             return StartContinuousGrab();
         }
 
         public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
         {
             // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岀‖浠惰Е鍙戜篃浣跨敤杩炵画閲囬泦妯″紡
+            // 鍙傝�僉LSystem绀轰緥鐨凜ontinuousScan鏂规硶锛歋etAcquisitionMode(1, 1)
             // 澶栭儴纭欢淇″彿浼氳Е鍙戠浉鏈哄紑濮嬮噰闆�
-            return StartContinuousGrab();
+            return StartSingleGrab();
         }
 
         public override bool SoftTrigger()
@@ -328,35 +452,38 @@
             bitmap = null;
             if (!_isConnected) return false;
 
+            // 瀵逛簬绾挎壂鐩告満锛屼娇鐢⊿DK鑷姩鏄剧ず妯″紡
+            // 娉ㄦ剰锛歋DK鑷姩鏄剧ず妯″紡涓嬶紝鎴戜滑鏃犳硶鐩存帴鑾峰彇Bitmap
+            // 浣哠DK浼氳嚜鍔ㄥ皢鍥惧儚鏄剧ず鍒癬lightPic鍙ユ焺瀵瑰簲鐨勬帶浠朵笂
+            // 鎴戜滑鍙渶瑕佸惎鍔ㄥ崟娆¢噰闆嗭紝绛夊緟閲囬泦瀹屾垚鍗冲彲
+
             using (AutoResetEvent waitHandle = new AutoResetEvent(false))
             {
-                Bitmap captured = null;
+                bool captured = false;
                 EventHandler<CameraEventArgs> handler = (s, e) =>
                 {
-                    try
+                    // 瀵逛簬绾挎壂鐩告満锛屼粎褰揑sComplete涓簍rue鏃舵墠琛ㄧず閲囬泦瀹屾垚
+                    if (e is LBCameraEventArgs args && args.IsComplete)
                     {
-                        if (e.Bitmap != null)
-                        {
-                            captured = e.Bitmap.Clone() as Bitmap;
-                        }
+                        captured = true;
+                        waitHandle.Set();
                     }
-                    catch (Exception ex)
-                    {
-                        AsyncLogHelper.Error($"LBCamera: GetImageWithSoftTrigger clone error - {ex.Message}");
-                    }
-                    waitHandle.Set();
                 };
 
                 this.ImageGrabbed += handler;
 
                 try
                 {
+                    // 浣跨敤鍗曟閲囬泦妯″紡锛堜笌LLSystem绀轰緥淇濇寔涓�鑷达級
                     if (StartSingleGrab())
                     {
+                        // 绛夊緟閲囬泦瀹屾垚
                         if (waitHandle.WaitOne(outtime))
                         {
-                            bitmap = captured;
-                            return bitmap != null;
+                            // 鐢变簬SDK鑷姩鏄剧ず锛屾垜浠棤娉曡繑鍥濨itmap
+                            // 浣嗛噰闆嗗凡瀹屾垚锛屽浘鍍忓凡鏄剧ず鍒扮晫闈笂
+                            bitmap = null;  // SDK鑷姩鏄剧ず妯″紡涓嬩笉杩斿洖Bitmap
+                            return captured;
                         }
                     }
                 }
@@ -378,121 +505,57 @@
         public void UpdateSensorConfig(PHM6000SensorConfig config)
         {
             _sensorConfig = config;
-            // 绠�鍗曠ず渚嬶細璁剧疆鏇濆厜鍜屽鐩�
-            SetExpouseTime(config.ExposureTime);
-            SetGain((double)config.AnalogGain);
-            // 鏇村鍙傛暟鍚屾閫昏緫搴斿湪姝ゅ瀹炵幇
+            
+            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 OnLineReceived(IntPtr pInstance, IntPtr buffer, int points)
-        {
-                // 瀹炴椂鍥炶皟澶勭悊锛氱疮绉鏁版嵁
-                if (!isGrabbing) return;
-
-                int lineSize = points * Marshal.SizeOf(typeof(LBPointZA));
-                byte[] lineData = new byte[lineSize];
-                Marshal.Copy(buffer, lineData, 0, lineSize);
-
-                lock (_bufferLock)
-                {
-                    _lineDataBuffer.Add(lineData);
-                    _currentLineCount++;
-                }
-        }
-
         private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
         {
-                // 鏍规嵁SDK鏂囨。锛歯Option涓�0鏃惰〃绀轰竴鎵规暟鎹粨鏉燂紝涓�1鏃惰〃绀哄叏閮ㄩ噰闆嗗畬鎴�
-                // 涓轰簡鍏煎鎬э紝涔熷鐞唍Option == 2锛堢偣浜戝氨缁級
-                // 姝ゆ椂浣跨敤涓诲姩鑾峰彇鏂瑰紡鏇夸唬鍙兘浼氬鑷碿rash鐨凣enerateIntensityMap
-                if (nOption == 0 || nOption == 1 || nOption == 2)
-                {
-                    RetrieveDataAndGenerateImage();
-                }
-        }
+            // nOption鍚箟:
+            // 0 = 涓�鎵规暟鎹粨鏉�
+            // 1 = 鍏ㄩ儴閲囬泦瀹屾垚锛堜粎鍗曟妯″紡锛�
+            // 2 = 瀵瑰簲鏁版嵁澶勭悊瀹屾垚锛堟瘡娆¢噰闆嗗畬鎴愰兘浼氳Е鍙戯級
 
-        private void RetrieveDataAndGenerateImage()
-        {
-            if (_cameraHandle == IntPtr.Zero) return;
+            // 璁板綍鍥炶皟淇℃伅
+            AsyncLogHelper.Info($"LBCamera[{SN}]: OnAcquisitionCompleted called, nOption={nOption}");
 
-            try
+            if (nOption == 2)
             {
-                List<byte[]> lineBuffers = new List<byte[]>();
-                ulong index = 0;
-                IntPtr ptr = IntPtr.Zero;
+                // 杩炵画閲囬泦妯″紡涓嬶紝姣忓畬鎴愪竴甯DK閮戒細鑷姩缁х画閲囬泦涓嬩竴甯�
+                // SDK浼氳嚜鍔ㄥ鐞嗗浘鍍忔樉绀哄埌_lightPic鍙ユ焺
+                // 鎴戜滑鍙渶瑕佺粺璁¢噰闆嗘鏁�
+                _frameCount++;
 
-                // 鍍忕ず渚嬩竴鏍烽�氳繃绱㈠紩鑾峰彇琛屾暟鎹�
-                while ((ptr = PHM6000Profiler.GetLineDataByIndex(_cameraHandle, index)) != IntPtr.Zero)
-                {
-                    try
-                    {
-                        LBLineDataZA lineData = PHM6000Profiler.ConvertToLBLineDataZA(ptr);
-                        
-                        // 鎻愬彇寮哄害鏁版嵁 (Alpha閫氶亾)
-                        if (lineData.data != null && lineData.data.Length > 0)
-                        {
-                            int lineWidth = lineData.data.Length;
-                            byte[] intensityLine = new byte[lineWidth];
-                            for (int i = 0; i < lineWidth; i++)
-                            {
-                                intensityLine[i] = lineData.data[i].alpha;
-                            }
-                            lineBuffers.Add(intensityLine);
-                        }
-                    }
-                    catch (Exception ex)
-                    {
-                        // 蹇界暐鍗曡杞崲閿欒
-                    }
-                    index++;
-                }
+                AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} completed (continuous mode, SDK auto-display)");
 
-                if (lineBuffers.Count == 0) return;
-
-                int height = lineBuffers.Count;
-                int width = lineBuffers[0].Length;
-
-                if (width <= 0 || height <= 0) 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);
-                
-                try
-                {
-                    int stride = bmpData.Stride;
-                    IntPtr scan0 = bmpData.Scan0;
-
-                    for (int y = 0; y < height; y++)
-                    {
-                        if (lineBuffers[y].Length == width) // 纭繚瀹藉害涓�鑷�
-                        {
-                            Marshal.Copy(lineBuffers[y], 0, scan0 + y * stride, width);
-                        }
-                    }
-                }
-                finally
-                {
-                    bmp.UnlockBits(bmpData);
-                }
-
-                // 瑙﹀彂浜嬩欢閫氱煡 UI 鏇存柊浜害鍥�
-                ImageGrabbed?.Invoke(this, new CameraEventArgs(SN, bmp));
-            }
-            catch (Exception ex)
-            {
-                AsyncLogHelper.Error($"LBCamera: 鐢熸垚鍥惧儚寮傚父 - {ex.Message}");
+                // 瑙﹀彂浜嬩欢閫氱煡UI鏇存柊锛堜絾涓嶄紶閫払itmap锛屽洜涓篠DK鑷姩鏄剧ず锛�
+                ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, null, true));
             }
         }
 
@@ -527,7 +590,6 @@
             }
             return false;
         }
-
         #endregion
     }
 }

--
Gitblit v1.9.3