From a9bc7819f4f7e66159a93e955944728c25ba3e53 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 07 一月 2026 14:10:11 +0800
Subject: [PATCH] 优化相机管理和界面布局 - 在 `CreateCameraForm.cs` 中添加对 `LBCameras` 的引用,并修复相机创建逻辑。 - 调整 `VisionForm.Designer.cs` 和 `2DCameraForm.Designer.cs` 中多个控件的大小和位置,改善界面布局。 - 在 `VisionForm.cs` 中添加对 `LBCameras` 的引用以支持新相机类型。 - 注释掉 `PHM6000Sensor.cs` 和 `PHM6000Profiler.cs` 中与相机信息获取相关的 P/Invoke 方法。 - 重构 `LBCamera.cs`,添加新的相机初始化和参数设置逻辑,确保与 PHM6000 SDK 的兼容性。 - 移除 `PHM6000Camera.cs` 中与 `PHM6000Camera` 相关的代码,简化相机管理逻辑。 Merge branch 'master' of http://lanpucloud.cn:1111/r/~zhengyabo/LB_TyreAppearanceInspectionSoftware

---
 LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs |  414 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 323 insertions(+), 91 deletions(-)

diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index 2bdc26b..a7c409b 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -1,9 +1,15 @@
-锘縰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
 {
@@ -11,124 +17,350 @@
     /// 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 List<byte[]> _lineDataBuffer = new List<byte[]>();
+        private readonly object _bufferLock = new object();
+        private int _currentLineCount = 0;
+
+        public LBCamera()
         {
-            return true;
+            Brand = CameraBrand.LBCamera;
+            _sensorConfig = new PHM6000SensorConfig();
+        }
+
+        #region ICamera Implementation
+
+        public override bool InitDevice(string sn, object handle = null)
+        {
+            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();
+
+                    // 鍒濆鍖栧洖璋�
+                    _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($"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;
+            }
             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)
+        public override bool StartGrabbing()
         {
-            delay = 0;
-            return true;
+            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 GetTriggerFliter(out double flitertime)
+        public override bool StopGrabbing()
         {
-            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)
-        {
+            if (!_isConnected) return true;
+            PHM6000Profiler.StopAcquisition(_cameraHandle);
+            isGrabbing = false;
             return true;
         }
 
         public override bool SoftTrigger()
         {
+            // 绾挎壂鐩告満閫氬父涓嶉渶瑕佷紶缁熻蒋瑙﹀彂锛屼絾鍦ㄦ煇浜涙ā寮忎笅鍙ā鎷�
             return true;
         }
 
-        public override bool StartGrabbing()
+        #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()
         {
-            return true;
+            SyncConfigFromCamera();
+            return _sensorConfig;
         }
 
-        public override bool StopGrabbing()
+        public void UpdateSensorConfig(PHM6000SensorConfig config)
         {
-            return true;
+            _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)
+            {
+                GenerateIntensityMap();
+            }
+        }
+
+        private void GenerateIntensityMap()
+        {
+            if (_cameraHandle == IntPtr.Zero) return;
+
+            int width = 0;
+            int height = 0;
+            
+            // 鐩存帴浠� SDK 鑾峰彇鍚堝苟鍚庣殑寮哄害鏁版嵁鎸囬拡 (unsigned char*)
+            IntPtr pIntensity = PHM6000Profiler.GetIntensityData(_cameraHandle, ref width, ref height);
+            
+            if (pIntensity == IntPtr.Zero || width <= 0 || height <= 0) return;
+
+            try
+            {
+                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);
+                
+                // 楂樻�ц兘鍐呭瓨鎷疯礉
+                int size = width * height;
+                byte[] managedData = new byte[size];
+                Marshal.Copy(pIntensity, managedData, 0, size);
+                Marshal.Copy(managedData, 0, bmpData.Scan0, size);
+                
+                bmp.UnlockBits(bmpData);
+
+                // 瑙﹀彂浜嬩欢閫氱煡 UI 鏇存柊浜害鍥�
+                ImageGrabbed?.Invoke(this, new CameraEventArgs(SN, bmp));
+            }
+            catch (Exception ex)
+            {
+                AsyncLogHelper.Error($"LBCamera: 鐢熸垚浜害鍥惧紓甯� - {ex.Message}");
+            }
+        }
+
+        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