From e8df282b9b5de43250164efa2cb55a19de267c0b Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期二, 06 一月 2026 14:22:22 +0800
Subject: [PATCH] 3D相机功能实现和参数界面扩展,通过将 PHM6000SensorConfig 绑定到   PropertyGrid,用户可以直接在界面上调整所有 3D 参数

---
 LB_VisionProcesses/Cameras/LBCameras/PHM6000Camera.cs |  278 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 LB_VisionProcesses/Cameras/2DCameraForm.cs            |   20 +++
 2 files changed, 295 insertions(+), 3 deletions(-)

diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index 6dc7029..a37fb4d 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -1,6 +1,8 @@
 锘縰sing HalconDotNet;
+using LB_SmartVisionCameraDevice.PHM6000;
 using LB_VisionControl;
 using LB_VisionProcesses.Cameras.HRCameras;
+using LB_VisionProcesses.Cameras.LBCameras;
 using MVSDK_Net;
 using Newtonsoft.Json.Linq;
 using OpenCvSharp;
@@ -164,7 +166,7 @@
                         camera = new HRCamera();
                         break;
                     case CameraBrand.LBCamera:
-                        //camera = new LBCamera();
+                        camera = new PHM6000Camera();
                         break;
                     default:
                         Debug.WriteLine("鏈煡鍝佺墝");
@@ -240,7 +242,7 @@
                 switch (brand)
                 {
                     case CameraBrand.LBCamera:
-                        //camera = new LBCamera();
+                        camera = new PHM6000Camera();
                         break;
                     case CameraBrand.HRCamera:
                         camera = new HRCamera();
@@ -313,7 +315,19 @@
 
                 PropertyGrid pg = new PropertyGrid();
                 pg.Dock = DockStyle.Fill;
-                pg.SelectedObject = new CameraAdvancedSettings(camera);
+
+                if (camera is PHM6000Camera phmCamera)
+                {
+                    pg.SelectedObject = phmCamera.GetSensorConfig();
+                    pg.PropertyValueChanged += (s, ev) =>
+                    {
+                        phmCamera.UpdateSensorConfig((PHM6000SensorConfig)pg.SelectedObject);
+                    };
+                }
+                else
+                {
+                    pg.SelectedObject = new CameraAdvancedSettings(camera);
+                }
 
                 editForm.Controls.Add(pg);
                 editForm.ShowDialog();
diff --git a/LB_VisionProcesses/Cameras/LBCameras/PHM6000Camera.cs b/LB_VisionProcesses/Cameras/LBCameras/PHM6000Camera.cs
new file mode 100644
index 0000000..640fcd0
--- /dev/null
+++ b/LB_VisionProcesses/Cameras/LBCameras/PHM6000Camera.cs
@@ -0,0 +1,278 @@
+using System;
+using System.Collections.Generic;
+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 PHM6000Camera : BaseCamera
+    {
+        private IntPtr _cameraHandle = IntPtr.Zero;
+        private PHM6000SensorConfig _sensorConfig;
+        private AcquisitionCallbackZA _acquisitionCallback;
+        private AcquisitionCompletedCallback _acquisitionCompletedCallback;
+        private bool _isConnected = false;
+
+        // 鍥惧儚缂撳啿
+        private List<byte[]> _lineDataBuffer = new List<byte[]>();
+        private readonly object _bufferLock = new object();
+        private int _currentLineCount = 0;
+
+        public PHM6000Camera()
+        {
+            Brand = CameraBrand.LBCamera;
+            _sensorConfig = new PHM6000SensorConfig();
+        }
+
+        #region ICamera Implementation
+
+        public override bool InitDevice(string sn, object handle = null)
+        {
+            try
+            {
+                SN = sn;
+                _cameraHandle = PHM6000Profiler.CreateCameraEntry();
+                if (_cameraHandle == IntPtr.Zero) return false;
+
+                // 浼樺厛浠庨厤缃姞杞� IP锛屽鏋� sn 鏄� IP 鏍煎紡鍒欒鐩�
+                string ip = "192.168.0.10";
+                if (System.Net.IPAddress.TryParse(sn, out _)) ip = sn;
+
+                int port = 32001;
+                var addr = Encoding.ASCII.GetBytes(ip);
+                int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, port);
+                
+                if (result == 0)
+                {
+                    _isConnected = true;
+                    // 鍔犺浇鐩告満褰撳墠鍙傛暟鍒� _sensorConfig
+                    SyncConfigFromCamera();
+
+                    // 鍒濆鍖栧洖璋�
+                    _acquisitionCallback = new AcquisitionCallbackZA(OnLineReceived);
+                    _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
+                    
+                    PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
+                    PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
+                    
+                    return true;
+                }
+            }
+            catch (Exception ex) { AsyncLogHelper.Error($"PHM6000: InitDevice寮傚父 - {ex.Message}"); }
+            return false;
+        }
+
+        public override bool CloseDevice()
+        {
+            if (_isConnected && _cameraHandle != IntPtr.Zero)
+            {
+                StopGrabbing();
+                PHM6000Profiler.DestroyCameraEntry(_cameraHandle);
+                _cameraHandle = IntPtr.Zero;
+                _isConnected = false;
+            }
+            return true;
+        }
+
+        public override List<string> GetListEnum()
+        {
+            List<string> cameraList = new List<string>();
+            IntPtr tempHandle = PHM6000Profiler.CreateCameraEntry();
+            int count = PHM6000Profiler.DiscoverCameras(tempHandle);
+            // 绠�鍖栵細瀹為檯搴斿惊鐜幏鍙栬澶� IP
+            PHM6000Profiler.DestroyCameraEntry(tempHandle);
+            return cameraList;
+        }
+
+        public override bool StartGrabbing()
+        {
+            if (!_isConnected) return false;
+            lock (_bufferLock)
+            {
+                _lineDataBuffer.Clear();
+                _currentLineCount = 0;
+            }
+
+            // 璁剧疆閲囬泦妯″紡锛�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()
+        {
+            if (!_isConnected) return true;
+            PHM6000Profiler.StopAcquisition(_cameraHandle);
+            isGrabbing = false;
+            return true;
+        }
+
+        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 PHM6000SensorConfig GetSensorConfig()
+        {
+            SyncConfigFromCamera();
+            return _sensorConfig;
+        }
+
+        public void UpdateSensorConfig(PHM6000SensorConfig config)
+        {
+            _sensorConfig = config;
+            // 绠�鍗曠ず渚嬶細璁剧疆鏇濆厜鍜屽鐩�
+            SetExpouseTime(config.ExposureTime);
+            SetGain((double)config.AnalogGain);
+            // 鏇村鍙傛暟鍚屾閫昏緫搴斿湪姝ゅ瀹炵幇
+        }
+
+        #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)
+        {
+            // nOption: 0=涓�鎵规暟鎹粨鏉�, 1=鍏ㄩ儴瀹屾垚, 2=鐐逛簯灏辩华
+            if (nOption == 1 || nOption == 0)
+            {
+                GenerateBitmapAndNotify();
+            }
+        }
+
+        private void GenerateBitmapAndNotify()
+        {
+            Bitmap bmp = null;
+            lock (_bufferLock)
+            {
+                if (_lineDataBuffer.Count == 0) return;
+
+                // 榛樿瀹藉害 4096 (绾挎壂浼犳劅鍣ㄥ吀鍨嬪��)
+                int width = 4096; 
+                int height = _lineDataBuffer.Count;
+
+                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);
+                
+                IntPtr scan0 = bmpData.Scan0;
+                for (int i = 0; i < height; i++)
+                {
+                    // 浠呮彁鍙� Alpha (寮哄害鍥�)
+                    // 娉ㄦ剰锛歀BPointZA 缁撴瀯涓� alpha 鏄 8 涓瓧鑺�
+                    byte[] lineBytes = _lineDataBuffer[i];
+                    byte[] intensityRow = new byte[width];
+                    
+                    for (int x = 0; x < Math.Min(width, lineBytes.Length / 8); x++)
+                    {
+                        intensityRow[x] = lineBytes[x * 8 + 7]; // alpha 浣�
+                    }
+                    
+                    Marshal.Copy(intensityRow, 0, scan0 + (i * bmpData.Stride), width);
+                }
+                
+                bmp.UnlockBits(bmpData);
+                _lineDataBuffer.Clear();
+            }
+
+            if (bmp != null)
+            {
+                // 瑙﹀彂浜嬩欢
+                ImageGrabbed?.Invoke(this, new CameraEventArgs(SN, bmp));
+            }
+        }
+
+        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