From 4091d5009b63ba1d6f24788ca79877c37001e0f6 Mon Sep 17 00:00:00 2001
From: C3032 <1057644574@qq.com>
Date: 星期五, 20 三月 2026 11:08:39 +0800
Subject: [PATCH] 华睿读码器集成完成,使用抽象接口-基类-各品牌子类的架构,实现可插拔设计,遵循开闭原则,方便后续集成其他品牌读码器

---
 LB_VisionProcesses/BarcodeReaders/Huayray/HRBarcodeReader.cs |   79 +++++++++++++++++++++++++++++++++------
 1 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/LB_VisionProcesses/BarcodeReaders/Huayray/HRBarcodeReader.cs b/LB_VisionProcesses/BarcodeReaders/Huayray/HRBarcodeReader.cs
index 872c066..aa9be07 100644
--- a/LB_VisionProcesses/BarcodeReaders/Huayray/HRBarcodeReader.cs
+++ b/LB_VisionProcesses/BarcodeReaders/Huayray/HRBarcodeReader.cs
@@ -59,15 +59,39 @@
 
             try
             {
+                // 鍏堣繘琛屼竴娆℃灇涓撅紝纭繚 SDK 鑳藉鍙戠幇璁惧锛堥儴鍒� SDK 瑕佹眰鍦ㄦ墦寮�鍓嶅繀椤绘墽琛屾灇涓撅級
+                GetDeviceList();
+
+                // 濡傛灉涔嬪墠澶辫触杩囷紝閲嶆柊鍒涘缓鐩告満瀵硅薄纭繚骞插噣鐘舵��
+                if (_camera == null)
+                {
+                    _camera = new EidCamera();
+                }
+
+                // 纭繚涔嬪墠鐨勫彞鏌勫凡閲婃斁
+                try { _camera.eidReleaseHandle_Net(); } catch { }
+
                 // 鍒涘缓鍙ユ焺
                 int nRet = _camera.eidCreateDevice_Net(sn, EidCamera.EidDeviceDataType.eidDeviceDataTypeSN);
-                if (nRet != EidCamera.eidErrorOK) return false;
+                if (nRet != EidCamera.eidErrorOK)
+                {
+                    _camera = null;
+                    return false;
+                }
+
+                // 鍒嗛厤鍥哄畾鍙ユ焺缁欏鎵橈紝闃叉琚� GC 鍥炴敹
+                if (!_callbackHandle.IsAllocated)
+                {
+                    _callbackHandle = GCHandle.Alloc(_frameCallback);
+                }
 
                 // 鎵撳紑璁惧
                 nRet = _camera.eidOpenDevice_Net();
                 if (nRet != EidCamera.eidErrorOK)
                 {
                     _camera.eidReleaseHandle_Net();
+                    if (_callbackHandle.IsAllocated) _callbackHandle.Free();
+                    _camera = null;
                     return false;
                 }
 
@@ -77,6 +101,8 @@
                 {
                     _camera.eidCloseDevice_Net();
                     _camera.eidReleaseHandle_Net();
+                    if (_callbackHandle.IsAllocated) _callbackHandle.Free();
+                    _camera = null;
                     return false;
                 }
 
@@ -86,21 +112,39 @@
             }
             catch
             {
+                // 鍙戠敓寮傚父鏃剁‘淇濋噴鏀捐祫婧�
+                try { _camera?.eidCloseDevice_Net(); } catch { }
+                try { _camera?.eidReleaseHandle_Net(); } catch { }
+                if (_callbackHandle.IsAllocated) _callbackHandle.Free();
+                // 鍙戠敓寮傚父鏃堕噴鏀剧浉鏈哄璞★紝纭繚涓嬫鍒涘缓鏂板疄渚�
+                _camera = null;
                 return false;
             }
         }
 
         public override bool Close()
         {
-            if (!IsConnected) return true;
-
             try
             {
-                StopGrabbing();
-                _camera.eidCloseDevice_Net();
-                _camera.eidReleaseHandle_Net();
+                if (IsConnected)
+                {
+                    StopGrabbing();
+                    _camera?.eidCloseDevice_Net();
+                }
+                // 鏃犺鏄惁杩炴帴锛岄兘灏濊瘯閲婃斁鍙ユ焺
+                _camera?.eidReleaseHandle_Net();
+
+                // 閲婃斁鍥炶皟鍙ユ焺
+                if (_callbackHandle.IsAllocated)
+                {
+                    _callbackHandle.Free();
+                }
+
+                // 閲婃斁鐩告満瀵硅薄寮曠敤锛岀‘淇漇DK璧勬簮瀹屽叏閲婃斁
+                _camera = null;
+
                 this.IsConnected = false;
-                this.SN = string.Empty;
+                this.IsGrabbing = false;
                 return true;
             }
             catch
@@ -166,7 +210,7 @@
         {
             try
             {
-                List<string> barcodes = new List<string>();
+                List<BarcodeInfo> barcodeInfos = new List<BarcodeInfo>();
                 // 瑙f瀽鏉$爜
                 for (int i = 0; i < frameInfo.codeNum; i++)
                 {
@@ -177,16 +221,25 @@
                     string data = Marshal.PtrToStringAnsi(codeInfo.data);
                     if (!string.IsNullOrEmpty(data))
                     {
-                        barcodes.Add(data);
+                        BarcodeInfo info = new BarcodeInfo
+                        {
+                            Text = data,
+                            Points = new Point[]
+                            {
+                                new Point((int)codeInfo.position[0].x, (int)codeInfo.position[0].y),
+                                new Point((int)codeInfo.position[1].x, (int)codeInfo.position[1].y),
+                                new Point((int)codeInfo.position[2].x, (int)codeInfo.position[2].y),
+                                new Point((int)codeInfo.position[3].x, (int)codeInfo.position[3].y)
+                            }
+                        };
+                        barcodeInfos.Add(info);
                     }
                 }
 
-                // 杞崲鍥惧儚 (濡傛灉闇�瑕�)
+                // 杞崲鍥惧儚
                 Bitmap bitmap = null;
                 if (frameInfo.imageDataLen > 0 && frameInfo.imageData != IntPtr.Zero)
                 {
-                    // 杩欓噷绠�鍖栧鐞嗭紝濡傛灉鏄疛peg鍒欑洿鎺ヤ粠鍐呭瓨鍔犺浇锛屽鏋滄槸Raw鍒欓渶杞崲
-                    // 瀹為檯椤圭洰涓彲鏍规嵁 frameInfo.format 杩涜澶勭悊
                     if (frameInfo.isJpeg)
                     {
                         byte[] managedArray = new byte[frameInfo.imageDataLen];
@@ -199,7 +252,7 @@
                 }
 
                 // 瑙﹀彂浜嬩欢
-                OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodes, bitmap));
+                OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodeInfos, bitmap));
             }
             catch (Exception ex)
             {

--
Gitblit v1.9.3