From 116ed6b584bbdb40c5b65e7cb57e039b6ae57800 Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期四, 08 一月 2026 11:12:25 +0800
Subject: [PATCH] 优化相机配置和图像获取逻辑
---
LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs | 211 +++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 189 insertions(+), 22 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index a856458..a5128a4 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -30,6 +30,12 @@
private readonly object _bufferLock = new object();
private int _currentLineCount = 0;
+ // 鏄剧ず鍙ユ焺鍜屽嚱鏁帮紙涓嶭LSystem绀轰緥淇濇寔涓�鑷达級
+ private IntPtr _lightPic = IntPtr.Zero;
+ private IntPtr _deepPic = IntPtr.Zero;
+ private IntPtr _pointPic = IntPtr.Zero;
+ private IntPtr _outlinePic = IntPtr.Zero;
+
public LBCamera()
{
Brand = CameraBrand.LBCamera;
@@ -93,7 +99,7 @@
if (!found)
{
- // 濡傛灉娌℃壘鍒颁絾 sn 鏈韩鐪嬭捣鏉ュ儚 IP锛屽皾璇曠洿鎺ヨ繛鎺ワ紙鍏煎鏃ч�昏緫锛�
+ // 濡傛灉娌℃壘鍒颁絾 sn 鏈韩鐪嬭捣鏉ュ儚 IP锛屽皾璇曠洿鎺ヨ繛鎺�
if (System.Net.IPAddress.TryParse(sn, out _))
{
targetIp = sn;
@@ -123,7 +129,7 @@
_acquisitionCallback = new AcquisitionCallbackZA(OnLineReceived);
_acquisitionCompletedCallback = new AcquisitionCompletedCallback(OnAcquisitionCompleted);
- PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
+ //PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, _acquisitionCallback, IntPtr.Zero);
PHM6000Profiler.RegisterAcquisitionCompletedCallback(_cameraHandle, _acquisitionCompletedCallback, IntPtr.Zero);
return true;
@@ -197,6 +203,63 @@
_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;
+ }
+
+ /// <summary>
+ /// 鍗曟閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
+ /// 璁剧疆閲囬泦妯″紡涓烘壂鎻忔ā寮忥紝鍗曟瑙﹀彂
+ /// </summary>
+ public bool StartSingleGrab()
+ {
+ if (!_isConnected) return false;
+ lock (_bufferLock)
+ {
+ _lineDataBuffer.Clear();
+ _currentLineCount = 0;
+ }
+
+ // 绂佺敤琛屽洖璋冿紙涓庣ず渚嬩竴鑷达級
+ PHM6000Profiler.SetAcquisitionCallbackZA(_cameraHandle, IntPtr.Zero, IntPtr.Zero);
+
+ // 璁剧疆閲囬泦妯″紡锛�1=鎵弿妯″紡锛�0=鍗曟妯″紡
+ PHM6000Profiler.SetAcquisitionMode(_cameraHandle, 1, 0);
+ int result = PHM6000Profiler.StartAcquisition(_cameraHandle, 0, 0, 0.0);
+ if (result == 0)
+ {
+ isGrabbing = true;
+ return true;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// 杩炵画閲囬泦妯″紡锛堥�傜敤浜庣嚎鎵浉鏈猴級
+ /// 璁剧疆閲囬泦妯″紡涓烘壂鎻忔ā寮忥紝杩炵画瑙﹀彂
+ /// </summary>
+ public bool StartContinuousGrab()
+ {
+ if (!_isConnected) return false;
+ lock (_bufferLock)
+ {
+ _lineDataBuffer.Clear();
+ _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);
@@ -214,6 +277,19 @@
PHM6000Profiler.StopAcquisition(_cameraHandle);
isGrabbing = false;
return true;
+ }
+
+ public override bool StartWith_SoftTriggerModel()
+ {
+ // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岃蒋浠惰Е鍙戣繛缁噰闆嗕娇鐢ㄨ繛缁噰闆嗘ā寮�
+ return StartContinuousGrab();
+ }
+
+ public override bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
+ {
+ // 瀵逛簬LBCamera锛堢嚎鎵浉鏈猴級锛岀‖浠惰Е鍙戜篃浣跨敤杩炵画閲囬泦妯″紡
+ // 澶栭儴纭欢淇″彿浼氳Е鍙戠浉鏈哄紑濮嬮噰闆�
+ return StartContinuousGrab();
}
public override bool SoftTrigger()
@@ -242,6 +318,56 @@
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 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)
+ {
+ bitmap = null;
+ if (!_isConnected) return false;
+
+ using (AutoResetEvent waitHandle = new AutoResetEvent(false))
+ {
+ Bitmap captured = null;
+ EventHandler<CameraEventArgs> handler = (s, e) =>
+ {
+ try
+ {
+ if (e.Bitmap != null)
+ {
+ captured = e.Bitmap.Clone() as Bitmap;
+ }
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera: GetImageWithSoftTrigger clone error - {ex.Message}");
+ }
+ waitHandle.Set();
+ };
+
+ this.ImageGrabbed += handler;
+
+ try
+ {
+ if (StartSingleGrab())
+ {
+ if (waitHandle.WaitOne(outtime))
+ {
+ bitmap = captured;
+ return bitmap != null;
+ }
+ }
+ }
+ finally
+ {
+ this.ImageGrabbed -= handler;
+ StopGrabbing();
+ }
+ }
+ return false;
+ }
public PHM6000SensorConfig GetSensorConfig()
{
@@ -282,27 +408,58 @@
private void OnAcquisitionCompleted(IntPtr pInstance, int nOption)
{
- // nOption: 0=涓�鎵规暟鎹粨鏉�, 1=鍏ㄩ儴瀹屾垚, 2=鐐逛簯灏辩华
- if (nOption == 1 || nOption == 0)
+ // 鏍规嵁SDK鏂囨。锛歯Option涓�0鏃惰〃绀轰竴鎵规暟鎹粨鏉燂紝涓�1鏃惰〃绀哄叏閮ㄩ噰闆嗗畬鎴�
+ // 涓轰簡鍏煎鎬э紝涔熷鐞唍Option == 2锛堢偣浜戝氨缁級
+ // 姝ゆ椂浣跨敤涓诲姩鑾峰彇鏂瑰紡鏇夸唬鍙兘浼氬鑷碿rash鐨凣enerateIntensityMap
+ if (nOption == 0 || nOption == 1 || nOption == 2)
{
- GenerateIntensityMap();
+ RetrieveDataAndGenerateImage();
}
}
- private void GenerateIntensityMap()
+ private void RetrieveDataAndGenerateImage()
{
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
{
+ List<byte[]> lineBuffers = new List<byte[]>();
+ ulong index = 0;
+ IntPtr ptr = IntPtr.Zero;
+
+ // 鍍忕ず渚嬩竴鏍烽�氳繃绱㈠紩鑾峰彇琛屾暟鎹�
+ 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++;
+ }
+
+ 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);
// 璁剧疆鐏板害璋冭壊鏉�
@@ -312,20 +469,30 @@
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);
+ 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}");
+ AsyncLogHelper.Error($"LBCamera: 鐢熸垚鍥惧儚寮傚父 - {ex.Message}");
}
}
--
Gitblit v1.9.3