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 | 494 +++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 340 insertions(+), 154 deletions(-)
diff --git a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
index 810ffd3..f82b884 100644
--- a/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
+++ b/LB_VisionProcesses/Cameras/LBCameras/LBCamera.cs
@@ -2,9 +2,15 @@
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;
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
@@ -32,12 +38,11 @@
{
private IntPtr _cameraHandle = IntPtr.Zero;
private PHM6000SensorConfig _sensorConfig;
-
// 閲囬泦鍥炶皟
private AcquisitionCallbackZA _acquisitionCallback;
private AcquisitionCompletedCallback _acquisitionCompletedCallback;
-
- private bool _isConnected = false;
+
+ public bool _isConnected = false;
private int _frameCount = 0; // 閲囬泦甯ц鏁�
// 鍥惧儚缂撳啿
@@ -95,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))
{
@@ -124,7 +129,7 @@
if (System.Net.IPAddress.TryParse(sn, out _))
{
targetIp = sn;
- targetPort = 5577;
+ targetPort = 5577;
}
else
{
@@ -139,7 +144,7 @@
var addr = Encoding.ASCII.GetBytes(targetIp);
int result = PHM6000Profiler.ConnectToCamera(_cameraHandle, addr, targetPort);
-
+
if (result == 0)
{
_isConnected = true;
@@ -148,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);
@@ -166,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);
}
@@ -204,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);
}
@@ -246,7 +251,7 @@
public bool StartSingleGrab()
{
if (!_isConnected) return false;
-
+
_isContinuous = false;
InitBuffer();
AsyncLogHelper.Info($"LBCamera[{SN}]: 寮�濮嬪崟娆¢噰闆�");
@@ -288,7 +293,7 @@
_isContinuous = false;
if (!_isConnected) return true;
PHM6000Profiler.StopAcquisition(_cameraHandle);
-
+
// 鍋滄鏃跺鏋滄湁鏈樉绀虹殑缂撳瓨鏁版嵁锛屽皢鍏舵樉绀哄嚭鏉ワ紙鏀寔鏄剧ず涓嶅畬鏁寸殑甯э級
lock (_bufferLock)
{
@@ -299,7 +304,7 @@
_currentLineCount = 0;
}
}
-
+
isGrabbing = false;
return true;
}
@@ -321,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;
}
@@ -352,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;
@@ -376,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))
@@ -401,10 +440,10 @@
}
ImageGrabbed -= handler;
-
+
// 纭繚鍋滄閲囬泦
StopGrabbing();
-
+
bitmap = res;
return bitmap != null;
}
@@ -418,22 +457,81 @@
public void UpdateSensorConfig(PHM6000SensorConfig config)
{
- _sensorConfig = config;
- if (!_isConnected) return;
- SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
- SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
- if (config.LineScanTriggerSource == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
+ //_sensorConfig = config;
+ //if (!_isConnected) return;
+ //SetParam(EnumNameId.ExposureTime, (float)config.ExposureTime);
+ //SetParam(EnumNameId.AnalogGain, (float)config.AnalogGain);
+ //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.ScanLineCount, config.ScanLineCount, 0, 0);
+ //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.LineScanTriggerSource, 0, 0, (int)config.LineScanTriggerSource);
+ //PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.DataAcquisitionTriggerSource, 0, 0, (int)config.DataAcquisitionTriggerSource);
+ //if (config.LineScanTriggerSource == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
+ //{
+ // PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
+ //}
+
+ int result = 0;
+ var type = config.GetType();
+ var propLineScan = type.GetProperty(nameof(config.LineScanTriggerSource));
+ var val = (EnumLineScanTriggerSource)propLineScan.GetValue(config);
+ var props = config.GetType().GetProperties();
+
+ //鎺掗櫎Y杞�
+ props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.YResolution)).ToArray();
+ //鎺掗櫎涓嶉渶瑕佺殑椤�
+ if (val == EnumLineScanTriggerSource.鍥哄畾棰戠巼)
{
- PHM6000Profiler.SetProfilerParameter(_cameraHandle, (int)EnumNameId.SoftwareTriggerRate, 0, config.SoftwareTriggerRate, 0);
+ props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.EncoderTriggerDirection) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerInterval) && d.Name != nameof(PHM6000SensorConfig.EncoderTriggerSignalCountingMode)).ToArray();
+ }
+ else
+ {
+ props = props.Where(d => d.Name != nameof(PHM6000SensorConfig.SoftwareTriggerRate)).ToArray();
+ }
+ foreach (var p in props)
+ {
+ //璺宠繃鑷畾涔夊弬鏁�
+ var iscustomAttr = p.GetCustomAttribute<IsCustomAttribute>();
+ if (iscustomAttr != null) continue;
+ //鍒ゆ柇鏄�6030浼犳劅鍣ㄨ繕鏄櫘閫氫紶鎰熷櫒
+ if (SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGain))
+ {
+ continue;
+ }
+ if (!SN.StartsWith("LX030") && p.Name == nameof(config.AnalogGainFor6030))
+ {
+ continue;
+ }
+ var id = Convert.ToInt32(Enum.Parse(typeof(EnumNameId), p.Name));
+ if (p.PropertyType == typeof(int))
+ {
+ var value = Convert.ToInt32(p.GetValue(config));
+ result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, value, 0, 0);
+ }
+ else if (p.PropertyType == typeof(float))
+ {
+ var value = Convert.ToDouble(p.GetValue(config));
+ result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, value, 0);
+ }
+ else
+ {
+ var value = Convert.ToInt32(p.GetValue(config));
+ result = PHM6000Profiler.SetProfilerParameter(_cameraHandle, id, 0, 0, value);
+ }
+ if (result == -1)
+ {
+ var disattr = p.GetCustomAttribute<DisplayNameAttribute>();
+ var name = disattr?.DisplayName ?? p.Name;
+ throw new Exception($"璁剧疆鍙傛暟{name}鏃朵笉鎴愬姛锛�");
+ }
+ }
+ var finalResult = PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
+ if (finalResult != 0)
+ {
}
PHM6000Profiler.SaveAllParametersToDevice(_cameraHandle);
}
#endregion
-
+
#endregion
#region Callbacks
@@ -468,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)
@@ -483,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)
{
@@ -502,7 +602,7 @@
{
// 濡傛灉鍦ㄨ繛缁ā寮忎笅鏀跺埌缁撴潫淇″彿锛屽皾璇曡嚜鍔ㄩ噸鍚噰闆�
AsyncLogHelper.Info($"LBCamera[{SN}]: Continuous mode frame ended, restarting...");
- Task.Run(() =>
+ Task.Run(() =>
{
if (_isContinuous && _isConnected)
{
@@ -529,18 +629,108 @@
}
}
}
+ 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();
+ }
}
private void CreateAndFireBitmap()
{
+ Bitmap bmp = null;
+ BitmapData bmpData = null;
+
try
{
int width = _currentBitmapWidth;
- int height = _currentLineCount; // 浣跨敤瀹為檯閲囬泦鍒扮殑琛屾暟
+ int height = _currentLineCount;
- if (width <= 0 || height <= 0 || _rawPixelBuffer == null) return;
+ // 鍩虹鍚堟硶鎬ф牎楠�
+ if (width <= 0 || height <= 0 || _rawPixelBuffer == null || _rawPixelBuffer.Length < width * height)
+ {
+ AsyncLogHelper.Warn($"LBCamera[{SN}]: 鍥惧儚鍙傛暟鏃犳晥锛岃烦杩囩敓鎴�");
+ return;
+ }
- Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
+ // 1. 鍒涘缓8浣嶇伆搴︿綅鍥�
+ bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
// 璁剧疆鐏板害璋冭壊鏉�
ColorPalette palette = bmp.Palette;
@@ -550,9 +740,8 @@
}
bmp.Palette = palette;
- // 鎷疯礉鏁版嵁
- BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
-
+ // 2. 楂樻晥鍐呭瓨鎷疯礉锛堟敮鎸丼tride瀵归綈锛屾暣琛屽鍒讹級
+ bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// 娉ㄦ剰锛欱itmap Stride 鍙兘涓嶇瓑浜� Width锛岄渶瑕侀�愯鎷疯礉
int stride = bmpData.Stride;
IntPtr ptr = bmpData.Scan0;
@@ -567,141 +756,127 @@
}
bmp.UnlockBits(bmpData);
+ bmpData = null; // 鏍囪宸茶В閿�
+
+
_frameCount++;
- AsyncLogHelper.Info($"LBCamera[{SN}]: Frame {_frameCount} generated ({width}x{height})");
- //绌哄�兼牎楠岋細杞崲澶辫触鍒欑洿鎺ヨ繑鍥�
- if (bmp == null)
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 鐢熸垚绗� {_frameCount} 甯� ({nWidth}x{nHeight})");
+
+ // 3. 鑾峰彇/鍒涘缓绾跨▼瀹夊叏闃熷垪
+ var queue = CollectedImages.GetOrAdd(SN, new ConcurrentQueue<Bitmap>());
+
+ // 4. 闃熷垪闄愭祦锛岄槻姝㈠唴瀛樻孩鍑�
+ if (queue.Count >= MAX_QUEUE_CAPACITY)
{
- AsyncLogHelper.Warn(SN + "甯ц浆鎹负Bitmap澶辫触锛岃烦杩囧鐞�");
- return;
- }
- // 绾跨▼瀹夊叏鍦板皢Bitmap娣诲姞鍒癈ollectedImages瀛楀吀
- lock (_collectedImagesLock)
- {
- // 纭繚褰撳墠鐩告満SN瀵瑰簲鐨勫垪琛ㄥ瓨鍦�
- if (!CollectedImages.ContainsKey(SN))
+ if (queue.TryDequeue(out Bitmap old))
{
- CollectedImages[SN] = new List<Bitmap>();
+ old.Dispose(); // 涓㈠純鏈�鏃у抚锛岄噴鏀惧唴瀛�
+ AsyncLogHelper.Warn($"LBCamera[{SN}]: 闃熷垪宸叉弧锛岃嚜鍔ㄤ涪寮冩渶鏃у抚");
}
- CollectedImages[SN].Add(bmp);
- AsyncLogHelper.Info(SN + $"鍥惧儚宸插姞鍏ョ紦瀛橈紝褰撳墠缂撳瓨鏁伴噺锛歿CollectedImages[SN].Count}");
}
- // 澶勭悊CollectedImages涓殑鍥惧儚锛氶亶鍘嗘秷璐瑰垪琛ㄧ涓�涓厓绱犵洿鍒颁负绌�
- ProcessCollectedImages();
- //// 寮傛瑙﹀彂浜嬩欢锛岄伩鍏嶉樆濉濻DK鍥炶皟绾跨▼
- //Task.Factory.StartNew(() =>
+ // 5. 鍏ラ槦
+ queue.Enqueue(bmp);
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 鍥惧儚鍏ラ槦锛屽綋鍓嶉槦鍒楋細{queue.Count}");
+
+ // 6. 鍚姩闃熷垪锛堝崟渚嬶紝閬垮厤澶氱嚎绋嬮噸澶嶏級
+ StartConsumeQueue();
+ //Task.Factory.StartNew(() =>
//{
- // try
+ // CallBackImg = (Bitmap)bitmap.Clone();
+ // if (CallBackImg == null)
// {
- // ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, bmp, true));
- // CallBackImg = (Bitmap)bmp.Clone();
- // if (CallBackImg == null)
- // {
- // return;
- // }
- // if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
- // {
- // if (mode == TriggerMode.On && source != TriggerSource.Software)
- // TriggerRunMessageReceived?.Invoke(SN, source.ToString()); // 瑙﹀彂杩愯浜嬩欢
- // }
- // bmp.Dispose();
+ // return;
// }
- // catch (Exception ex)
+ // if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
// {
- // AsyncLogHelper.Error($"LBCamera: Event Invoke error - {ex.Message}");
- // bmp.Dispose(); // 寮傚父鏃堕噴鏀捐祫婧�
+ // if (mode == TriggerMode.On && source != TriggerSource.Software)
+ // TriggerRunMessageReceived?.Invoke(SN, source.ToString()); // 瑙﹀彂杩愯浜嬩欢
// }
+ // bitmap.Dispose();
//});
}
catch (Exception ex)
{
- AsyncLogHelper.Error($"LBCamera: CreateBitmap error - {ex.Message}");
+ AsyncLogHelper.Error($"LBCamera[{SN}]: 鍒涘缓鍥惧儚澶辫触 - {ex.Message}", ex);
+ }
+ finally
+ {
+ // 寮哄埗璧勬簮閲婃斁锛岀粷瀵规潨缁濇硠婕�
+ if (bmpData != null)
+ {
+ try { bitmap1?.UnlockBits(bmpData); } catch { }
+ }
+ //娉ㄦ剰锛歜mp 宸插叆闃燂紝涓嶈兘鍦ㄨ繖閲岄噴鏀撅紝鐢辫皟鐢ㄨ�呴噴鏀�
}
}
+
+ /// <summary>
+ /// 鍚姩闃熷垪锛堜繚璇佸崟绾跨▼锛�
+ /// </summary>
+ private void StartConsumeQueue()
+ {
+ // 浣跨敤杞婚噺绾у垽鏂紝閬垮厤閲嶅鍚姩娑堣垂浠诲姟
+ if (CollectedImages.TryGetValue(SN, out var queue) && !queue.IsEmpty)
+ {
+ Task.Factory.StartNew(ProcessImageQueue, TaskCreationOptions.LongRunning);
+ }
+ }
+
/// <summary>
/// 澶勭悊CollectedImages涓殑缂撳瓨鍥惧儚
/// 鏍稿績閫昏緫锛氶亶鍘嗗彇绗竴涓浘鍍� -> 璧嬪�肩粰CallBackImg -> 瑙﹀彂浜嬩欢 -> 閲婃斁骞剁Щ闄�
/// </summary>
- private void ProcessCollectedImages()
+ private void ProcessImageQueue()
{
- Task.Factory.StartNew(() =>
+ try
{
- // 鍔犻攣淇濊瘉绾跨▼瀹夊叏锛岄槻姝㈠绾跨▼鍚屾椂鎿嶄綔鍒楄〃
- lock (_collectedImagesLock)
+ if (!CollectedImages.TryGetValue(SN, out var queue) || queue.IsEmpty)
+ return;
+
+ // 鐭攣锛氫粎鍑洪槦锛屼笉闃诲鐢熶骇
+ while (queue.TryDequeue(out Bitmap bitmap))
{
- // 鏍¢獙褰撳墠鐩告満鐨勫浘鍍忓垪琛ㄦ槸鍚﹀瓨鍦ㄤ笖鏈夋暟鎹�
- if (!CollectedImages.ContainsKey(SN) || CollectedImages[SN].Count == 0)
- {
- AsyncLogHelper.Info(SN + "褰撳墠鏃犵紦瀛樺浘鍍忥紝璺宠繃澶勭悊");
- return;
- }
- // 寰幆澶勭悊锛氱洿鍒板垪琛ㄤ负绌�
- while (CollectedImages[SN].Count > 0)
+ using (bitmap) // 鑷姩閲婃斁锛歶sing 鏄渶瀹夊叏鐨勬柟寮�
{
try
{
- // 1 鍙栧垪琛ㄧ涓�涓储寮曠殑鍥惧儚璧嬪�肩粰CallBackImg
- Bitmap firstBitmap = CollectedImages[SN][0];
- ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, firstBitmap, true));
- CallBackImg = (Bitmap)firstBitmap.Clone(); // 鍏嬮殕閬垮厤鍘熷璞¤閲婃斁鍚庡紩鐢ㄥけ鏁�
+ // 鍏抽敭锛氫簨浠朵紶閫掑厠闅嗗璞★紝缁濆瀹夊叏锛屼笉浼犻�掑師璧勬簮
+ using (Bitmap clone = (Bitmap)bitmap.Clone())
+ {
+ // 瑙﹀彂鍥惧儚浜嬩欢
+ ImageGrabbed?.Invoke(this, new LBCameraEventArgs(SN, clone, true));
+ CallBackImg = (Bitmap)clone.Clone();
+ }
- // 2 鑾峰彇瑙﹀彂妯″紡骞跺垽鏂槸鍚﹁Е鍙戣繍琛屼簨浠�
+ // 瑙﹀彂妯″紡鍒ゆ柇
if (GetTriggerMode(out TriggerMode mode, out TriggerSource source))
{
- // 纭Е鍙戞ā寮忎笅瑙﹀彂杩愯浜嬩欢
if (mode == TriggerMode.On && source != TriggerSource.Software)
{
- AsyncLogHelper.Info(SN + $"瑙﹀彂纭Е鍙戜簨浠讹紝瑙﹀彂婧愶細{source}");
TriggerRunMessageReceived?.Invoke(SN, source.ToString());
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
+ }
+ else
+ {
+ TriggerRunMessageReceived?.Invoke(SN, source.ToString());
+ AsyncLogHelper.Info($"LBCamera[{SN}]: 纭Е鍙戜簨浠� - {source}");
}
}
- else
- {
- AsyncLogHelper.Warn(SN + "鑾峰彇瑙﹀彂妯″紡澶辫触锛岃烦杩囦簨浠惰Е鍙�");
- }
-
- // 3 閲婃斁绗竴涓浘鍍忚祫婧愬苟浠庡垪琛ㄧЩ闄�
- // 鍏堥噴鏀綛itmap鍐呭瓨锛屽啀绉婚櫎鍒楄〃鍏冪礌
- firstBitmap.Dispose();
- CollectedImages[SN].RemoveAt(0);
- AsyncLogHelper.Info(SN + $"宸叉秷璐圭紦瀛樺浘鍍忥紝鍓╀綑缂撳瓨鏁伴噺锛歿CollectedImages[SN].Count}");
}
catch (Exception ex)
{
- AsyncLogHelper.Error(SN + $"澶勭悊缂撳瓨鍥惧儚寮傚父锛歿ex.Message}", ex);
- // 鍗曚釜鍥惧儚澶勭悊澶辫触鏃讹紝绉婚櫎璇ュ浘鍍忛伩鍏嶉樆濉炲悗缁鐞�
- if (CollectedImages[SN].Count > 0)
- {
- try
- {
- CollectedImages[SN][0]?.Dispose(); // 灏濊瘯閲婃斁
- CollectedImages[SN].RemoveAt(0);
- }
- catch (Exception innerEx)
- {
- AsyncLogHelper.Error(SN + $"娓呯悊寮傚父鍥惧儚澶辫触锛歿innerEx.Message}", innerEx);
- }
- }
- // 鍗曚釜鍥惧儚澶勭悊澶辫触涓嶇粓姝㈠惊鐜紝缁х画澶勭悊涓嬩竴涓�
- // 4. 鎵�鏈夊浘鍍忓鐞嗗畬鎴愬悗锛屾竻绌篊allBackImg
- if (CallBackImg != null)
- {
- CallBackImg.Dispose();
- CallBackImg = null;
- }
- continue;
- }
- // 4. 鎵�鏈夊浘鍍忓鐞嗗畬鎴愬悗锛屾竻绌篊allBackImg
- if (CallBackImg != null)
- {
- CallBackImg.Dispose();
- CallBackImg = null;
+ AsyncLogHelper.Error($"LBCamera[{SN}]: 澶勭悊鍗曞抚鍥惧儚寮傚父 - {ex.Message}", ex);
+ continue; // 鍗曞抚寮傚父锛岀户缁鐞嗕笅涓�甯�
}
}
}
- });
+ }
+ catch (Exception ex)
+ {
+ AsyncLogHelper.Error($"LBCamera[{SN}]: 娑堣垂闃熷垪寮傚父 - {ex.Message}", ex);
+ }
}
@@ -720,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))
{
@@ -741,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