From 98c0775fe3b61a37d90dd5756287f385a311adf0 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 15 四月 2026 20:09:49 +0800
Subject: [PATCH] 验证3D相机亮度图无法获取

---
 LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs |  233 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 185 insertions(+), 48 deletions(-)

diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index 123fa04..f82b884 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -2,6 +2,8 @@
 using LB_SmartVisionCameraSDK.PHM6000;
 using LB_SmartVisionCommon;
 using LB_VisionProcesses.Cameras;
+using OpenCvSharp;
+using OpenCvSharp.Extensions;
 using OpenVinoSharp.Extensions.model;
 using SharpCompress.Common;
 using Sunny.UI.Win32;
@@ -39,8 +41,8 @@
         // 閲囬泦鍥炶皟
         private AcquisitionCallbackZA _acquisitionCallback;
         private AcquisitionCompletedCallback _acquisitionCompletedCallback;
-        
-        private bool _isConnected = false;
+
+        public bool _isConnected = false;
         private int _frameCount = 0; // 閲囬泦甯ц鏁�
 
         // 鍥惧儚缂撳啿
@@ -98,11 +100,11 @@
                 {
                     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))
                         {
@@ -127,7 +129,7 @@
                     if (System.Net.IPAddress.TryParse(sn, out _))
                     {
                         targetIp = sn;
-                        targetPort = 5577; 
+                        targetPort = 5577;
                     }
                     else
                     {
@@ -142,7 +144,7 @@
 
                 var addr = Encoding.ASCII.GetBytes(targetIp);
                 int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort);
-                
+
                 if (result == 0)
                 {
                     _isConnected = true;
@@ -151,11 +153,11 @@
 
                     // 鍒濆鍖栧苟娉ㄥ唽閲囬泦鍥炶皟 (鑾峰彇鏁版嵁鐢�)
                     _acquisitionCallback = new AcquisitionCallbackZA(OnAcquisitionCallbackZA);
-                    PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
+                    PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, new IntPtr());
 
                     // 鍒濆鍖栧苟娉ㄥ唽閲囬泦瀹屾垚鍥炶皟 (鐘舵�侀�氱煡鐢�)
-                    _acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
-                    PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
+                    _acquisitionCompletedCallback += OnAcquisitionCompleted;
+                    PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, new IntPtr());
 
                     // 寮哄埗搴旂敤褰撳墠閰嶇疆锛堢‘淇濊Е鍙戞ā寮忕瓑鍙傛暟姝g‘锛岄伩鍏嶇浉鏈哄浜庢湭鐭ョ姸鎬侊級
                     UpdateSensorConfig(_sensorConfig);
@@ -169,8 +171,8 @@
                     AsyncLogHelper.Error($"LBCamera[{SN}]: ConnectToCamera failed, result={result}");
                 }
             }
-            catch (Exception ex) 
-            { 
+            catch (Exception ex)
+            {
                 AsyncLogHelper.Error($"LBCamera: InitDevice寮傚父 - {ex.Message}");
                 if (tempHandle != IntPtr.Zero) PHM6000Profiler.DestroyCameraEntry(tempHandle);
             }
@@ -207,7 +209,7 @@
                         if (PHM6000Profiler.GetCameraInformation(tempHandle, i, moduleTypeBytes, serialNumberBytes) == 0)
                         {
                             string sn = Encoding.UTF8.GetString(serialNumberBytes).TrimEnd('\0');
-                            if (!string.IsNullOrEmpty(sn))
+                            if (!string.IsNullOrEmpty(sn) && sn.Contains("L"))
                             {
                                 cameraList.Add(sn);
                             }
@@ -249,7 +251,7 @@
         public bool StartSingleGrab()
         {
             if (!_isConnected) return false;
-            
+
             _isContinuous = false;
             InitBuffer();
             AsyncLogHelper.Info($"LBCamera[{SN}]: 寮�濮嬪崟娆¢噰闆�");
@@ -291,7 +293,7 @@
             _isContinuous = false;
             if (!_isConnected) return true;
             PHM6000Profiler.StopAcquisition(_cameraHandle);
-            
+
             // 鍋滄鏃跺鏋滄湁鏈樉绀虹殑缂撳瓨鏁版嵁锛屽皢鍏舵樉绀哄嚭鏉ワ紙鏀寔鏄剧ず涓嶅畬鏁寸殑甯э級
             lock (_bufferLock)
             {
@@ -302,7 +304,7 @@
                     _currentLineCount = 0;
                 }
             }
-            
+
             isGrabbing = false;
             return true;
         }
@@ -324,16 +326,16 @@
         {
             if (!_isConnected) return false;
 
-            if (triggerEnum == TriggerSource.Software)
-            {
-                _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.鍥哄畾棰戠巼;
-                _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.杞Е鍙�;
-            }
-            else
-            {
-                _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.缂栫爜鍣�;
-                _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.澶栭儴瑙﹀彂;
-            }
+            //if (triggerEnum == TriggerSource.Software)
+            //{
+            //    _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.鍥哄畾棰戠巼;
+            //    _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.杞Е鍙�;
+            //}
+            //else
+            //{
+            //    _sensorConfig.LineScanTriggerSource = EnumLineScanTriggerSource.缂栫爜鍣�;
+            //    _sensorConfig.DataAcquisitionTriggerSource = EnumDataAcquisitionTriggerSource.澶栭儴瑙﹀彂;
+            //}
             UpdateSensorConfig(_sensorConfig);
             return true;
         }
@@ -355,13 +357,45 @@
         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) 
+        public override void GetCamConfig(out CameraConfig config)
+        {
+            config = new CameraConfig(null);
+            //UpdateSensorConfig(config);
+        }
+        public override bool GetImage(out Bitmap bitmap, int outtime = 14500)
+        {
+            bitmap = null;
+            try
+            {
+                // 璁剧疆瓒呮椂鏃堕棿
+                DateTime lastTime = DateTime.Now.AddMilliseconds(outtime);
+                // 鍒ゆ柇鏄惁瓒呮椂
+                while (lastTime > DateTime.Now)// 璁剧疆瓒呮椂鏃堕棿涓� 3 绉�
+                {
+                    if (CallBackImg != null)
+                    {
+                        lock (CallBackImg)
+                        {
+                            // 淇濆瓨鏃� Bitmap 骞堕噴鏀�
+                            bitmap = CallBackImg; // 鍒涘缓鍓湰
+                        }
+
+                        //// 閲婃斁鏃ц祫婧�
+                        //CallBackImg.Dispose();
+                        //CallBackImg = null;
+                        return true;
+                    }
+                }
+
+                return false;
+            }
+            catch { return bitmap == null ? false : true; }
+        }
+        public override bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
         {
             // 绠�鍗曞疄鐜帮細杞Е鍙戠瓑寰�
             bitmap = null;
-            if(!_isConnected) return false;
+            if (!_isConnected) return false;
 
             // 璁$畻鐞嗚鏈�灏忚�楁椂 (浠呭綋浣跨敤鍥哄畾棰戠巼瑙﹀彂鏃�)
             int minTime = 0;
@@ -379,18 +413,20 @@
                 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();
+                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))
@@ -404,10 +440,10 @@
                 }
 
                 ImageGrabbed -= handler;
-                
+
                 // 纭繚鍋滄閲囬泦
                 StopGrabbing();
-                
+
                 bitmap = res;
                 return bitmap != null;
             }
@@ -495,7 +531,7 @@
         }
 
         #endregion
-        
+
         #endregion
 
         #region Callbacks
@@ -530,7 +566,7 @@
                 // 鎻愬彇鐏板害(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)
@@ -545,13 +581,15 @@
                 if (_currentLineCount >= _currentBitmapHeight)
                 {
                     CreateAndFireBitmap();
-                    
+
                     // 閲嶇疆锛屽噯澶囦笅涓�甯� (濡傛灉鏄繛缁噰闆�)
                     _currentLineCount = 0;
                     // _rawPixelBuffer 鍙互澶嶇敤锛屼笉闇�瑕佺疆绌�
                 }
             }
         }
+        private Bitmap bitmap1;
+        private int nWidth = 0, nHeight = 0;
 
         private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
         {
@@ -564,7 +602,7 @@
                 {
                     // 濡傛灉鍦ㄨ繛缁ā寮忎笅鏀跺埌缁撴潫淇″彿锛屽皾璇曡嚜鍔ㄩ噸鍚噰闆�
                     AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting...");
-                    Task.Run(() => 
+                    Task.Run(() =>
                     {
                         if (_isContinuous && _isConnected)
                         {
@@ -590,6 +628,87 @@
                         }
                     }
                 }
+            }
+            else if (nOption == 2)
+            {
+                AsyncLogHelper.Info($"LBCamera[{SN}]: Processing End...");
+                IntPtr INTPTRImage = PHM6000Profiler.GetIntensityData(_cameraHandle, ref nWidth, ref nHeight);
+                bitmap1 = IntensityPtrToMatCloned(INTPTRImage, nWidth, nHeight);
+            }
+        }
+
+        public Bitmap ConvertIntensityToBitmap(IntPtr dataPtr, int width, int height)
+        {
+
+            if (dataPtr == IntPtr.Zero || width <= 0 || height <= 0)
+                throw new InvalidOperationException("鑾峰彇寮哄害鏁版嵁澶辫触鎴栧昂瀵告棤鏁堛��");
+
+            // 鍒涘缓 8bpp 绱㈠紩浣嶅浘
+            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
+
+            // 璁剧疆鐏板害璋冭壊鏉匡紙蹇呴』锛屽惁鍒欐樉绀哄紓甯革級
+            ColorPalette palette = bitmap.Palette;
+            for (int i = 0; i < 256; i++)
+            {
+                palette.Entries[i] = Color.FromArgb(i, i, i);
+            }
+            bitmap.Palette = palette;
+
+            // 閿佸畾浣嶅浘鏁版嵁
+            BitmapData bmpData = bitmap.LockBits(
+                new Rectangle(0, 0, width, height),
+                ImageLockMode.WriteOnly,
+                PixelFormat.Format8bppIndexed);
+
+            try
+            {
+                int srcStride = width;                     // 婧愭暟鎹瘡琛屽瓧鑺傛暟锛堟棤濉厖锛�
+                int dstStride = bmpData.Stride;             // 浣嶅浘姣忚瀛楄妭鏁帮紙鍙兘瀵归綈锛�
+                int copyBytesPerRow = Math.Min(srcStride, dstStride);
+
+                IntPtr srcRowPtr = dataPtr;
+                IntPtr dstRowPtr = bmpData.Scan0;
+
+                // 閫愯澶嶅埗锛屽鐞嗗彲鑳界殑姝ュ箙宸紓
+                for (int y = 0; y < height; y++)
+                {
+                    // 浣跨敤 Buffer.MemoryCopy 鎴� Marshal.Copy 鎸夎澶嶅埗
+                    unsafe
+                    {
+                        Buffer.MemoryCopy(
+                            srcRowPtr.ToPointer(),
+                            dstRowPtr.ToPointer(),
+                            dstStride,          // 鐩爣缂撳啿鍖哄墿浣欏ぇ灏忥紙鑷冲皯涓�琛岋級
+                            copyBytesPerRow);    // 瀹為檯澶嶅埗瀛楄妭鏁�
+                    }
+
+                    // 绉诲姩鍒颁笅涓�琛�
+                    srcRowPtr = IntPtr.Add(srcRowPtr, srcStride);
+                    dstRowPtr = IntPtr.Add(dstRowPtr, dstStride);
+                }
+            }
+            finally
+            {
+                bitmap.UnlockBits(bmpData);
+            }
+
+            return bitmap;
+        }
+
+
+        public static Bitmap IntensityPtrToMatCloned(IntPtr dataPtr, int width, int height)
+        {
+
+            if (dataPtr == IntPtr.Zero || width <= 0 || height <= 0)
+            {
+                throw new Exception("Failed to get intensity data.");
+            }
+
+            // 鍏堝垱寤哄紩鐢ㄥ閮ㄦ暟鎹殑 Mat
+            using (Mat temp = Mat.FromPixelData(height, width, MatType.CV_8UC1, dataPtr, width))
+            {
+                // 鍏嬮殕涓�浠界嫭绔嬪唴瀛樼殑 Mat
+                return temp.ToBitmap();
             }
         }
 
@@ -639,8 +758,10 @@
                 bmp.UnlockBits(bmpData);
                 bmpData = null; // 鏍囪宸茶В閿�
 
+
+
                 _frameCount++;
-                AsyncLogHelper.Info($"LBCamera[{SN}]: 鐢熸垚绗� {_frameCount} 甯� ({width}x{height})");
+                AsyncLogHelper.Info($"LBCamera[{SN}]: 鐢熸垚绗� {_frameCount} 甯� ({nWidth}x{nHeight})");
 
                 // 3. 鑾峰彇/鍒涘缓绾跨▼瀹夊叏闃熷垪
                 var queue = CollectedImages.GetOrAdd(SN, new ConcurrentQueue<Bitmap>());
@@ -685,9 +806,9 @@
                 // 寮哄埗璧勬簮閲婃斁锛岀粷瀵规潨缁濇硠婕�
                 if (bmpData != null)
                 {
-                    try { bmp?.UnlockBits(bmpData); } catch { }
+                    try { bitmap1?.UnlockBits(bmpData); } catch { }
                 }
-                // 娉ㄦ剰锛歜mp 宸插叆闃燂紝涓嶈兘鍦ㄨ繖閲岄噴鏀撅紝鐢辫皟鐢ㄨ�呴噴鏀�
+                //娉ㄦ剰锛歜mp 宸插叆闃燂紝涓嶈兘鍦ㄨ繖閲岄噴鏀撅紝鐢辫皟鐢ㄨ�呴噴鏀�
             }
         }
 
@@ -737,6 +858,11 @@
                                     TriggerRunMessageReceived?.Invoke(SN, source.ToString());
                                     AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
                                 }
+                                else
+                                {
+                                    TriggerRunMessageReceived?.Invoke(SN, source.ToString());
+                                    AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
+                                }
                             }
                         }
                         catch (Exception ex)
@@ -769,12 +895,16 @@
 
                     if (Enum.TryParse(typeof(EnumNameId), p.Name, out object nameIdObj))
                     {
-                        EnumNameId nameId = (EnumNameId)nameIdObj;
+                        //EnumNameId nameId = (EnumNameId)nameIdObj;
+                        var nameId = Enum.Parse(typeof(EnumNameId), p.Name);
                         int intValue = 0;
                         double doubleValue = 0;
                         int enumValue = 0;
-                        
-                        if (PHM6000Profiler.GetProfilerParameter(_cameraHandle, (int)nameId, ref intValue, ref doubleValue, ref enumValue) == 0)
+                        var id = Convert.ToInt32(nameId);
+                        var rst = PHM6000Profiler.GetProfilerParameter(_cameraHandle, id, ref intValue, ref doubleValue, ref enumValue);
+
+
+                        if (rst == 0)
                         {
                             if (p.PropertyType == typeof(int))
                             {
@@ -790,7 +920,14 @@
                             }
                             else // Enum or other types
                             {
-                                p.SetValue(_sensorConfig, enumValue);
+                                if (p.Name.Equals("ROI"))
+                                {
+
+                                }
+                                else
+                                {
+                                    p.SetValue(_sensorConfig, enumValue);
+                                }
                             }
                         }
                     }

--
Gitblit v1.9.3