From a0c982ba8abdbd7569a6cae07812127a757fd410 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期三, 07 一月 2026 16:02:34 +0800
Subject: [PATCH] 更新相机实例化和UI线程处理逻辑
---
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..a856458 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