C3032
2026-03-23 68a4b459eeb18effb8b3096add3d88c15629ab69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using EasyIDSDK_Net;
 
namespace LB_VisionProcesses.BarcodeReaders.Huayray
{
    /// <summary>
    /// 华睿读码器实现类
    /// </summary>
    public class HRBarcodeReader : BarcodeReaderBase
    {
        private EidCamera _camera;
        private EidCamera.EidFrameCallback _frameCallback;
        private GCHandle _callbackHandle;
 
        public override BarcodeReaderBrand Brand => BarcodeReaderBrand.Huayray;
 
        public HRBarcodeReader()
        {
            _camera = new EidCamera();
            _frameCallback = OnFrameReceived;
        }
 
        /// <summary>
        /// 获取在线的华睿读码器列表
        /// </summary>
        public override List<string> GetDeviceList()
        {
            List<string> snList = new List<string>();
            try
            {
                EidCamera.EidDeviceList devList = new EidCamera.EidDeviceList();
                int nRet = EidCamera.eidEnumDevices_Net(ref devList, 0);
                if (nRet == EidCamera.eidErrorOK && devList.num > 0)
                {
                    for (int i = 0; i < devList.num; i++)
                    {
                        EidCamera.EidDeviceInfo deviceInfo = (EidCamera.EidDeviceInfo)Marshal.PtrToStructure(
                            devList.infos + Marshal.SizeOf(typeof(EidCamera.EidDeviceInfo)) * i,
                            typeof(EidCamera.EidDeviceInfo));
                        snList.Add(deviceInfo.serialNumber);
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO: Log error
            }
            return snList;
        }
 
        public override bool Open(string sn)
        {
            if (IsConnected) Close();
 
            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;
                }
 
                // 注册回调
                nRet = _camera.eidRegisterFrameCallback_Net(_frameCallback, IntPtr.Zero);
                if (nRet != EidCamera.eidErrorOK)
                {
                    _camera.eidCloseDevice_Net();
                    _camera.eidReleaseHandle_Net();
                    if (_callbackHandle.IsAllocated) _callbackHandle.Free();
                    _camera = null;
                    return false;
                }
 
                this.SN = sn;
                this.IsConnected = true;
                return true;
            }
            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
            {
                if (IsConnected)
                {
                    StopGrabbing();
                _camera.eidCloseDevice_Net();
                _camera.eidReleaseHandle_Net();
                    _camera?.eidCloseDevice_Net();
                }
                // 无论是否连接,都尝试释放句柄
                _camera?.eidReleaseHandle_Net();
 
                // 释放回调句柄
                if (_callbackHandle.IsAllocated)
                {
                    _callbackHandle.Free();
                }
 
                // 释放相机对象引用,确保SDK资源完全释放
                _camera = null;
 
                this.IsConnected = false;
                this.SN = string.Empty;
                this.IsGrabbing = false;
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        public override bool StartGrabbing()
        {
            if (!IsConnected) return false;
            if (IsGrabbing) return true;
 
            int nRet = _camera.eidStartGrabbing_Net(0);
            if (nRet == EidCamera.eidErrorOK)
            {
                this.IsGrabbing = true;
                return true;
            }
            return false;
        }
 
        public override bool StopGrabbing()
        {
            if (!IsGrabbing) return true;
 
            int nRet = _camera.eidStopGrabbing_Net();
            this.IsGrabbing = false;
            return nRet == EidCamera.eidErrorOK;
        }
 
        public override bool SoftTrigger()
        {
            if (!IsConnected || !IsGrabbing) return false;
            int nRet = _camera.eidExecCommandFeature_Net("TriggerSoftware");
            return nRet == EidCamera.eidErrorOK;
        }
 
        public override bool SetTriggerMode(bool isSoftware)
        {
            if (!IsConnected) return false;
 
            int nRet;
            if (isSoftware)
            {
                // 软触发模式
                nRet = _camera.eidSetEnumFeatureSymbol_Net("TriggerType", "SingleFrame");
                if (nRet != EidCamera.eidErrorOK) return false;
                nRet = _camera.eidSetEnumFeatureSymbol_Net("TriggerSource", "Software");
            }
            else
            {
                // 自由运行或硬触发 (此处默认设为自由运行,可根据需要扩展)
                nRet = _camera.eidSetEnumFeatureSymbol_Net("TriggerType", "FreeRun");
            }
            return nRet == EidCamera.eidErrorOK;
        }
 
        /// <summary>
        /// SDK帧回调处理
        /// </summary>
        private void OnFrameReceived(ref EidCamera.EidFrameInfo frameInfo, IntPtr userData)
        {
            try
            {
                List<BarcodeInfo> barcodeInfos = new List<BarcodeInfo>();
                // 解析条码
                for (int i = 0; i < frameInfo.codeNum; i++)
                {
                    EidCamera.EidCodeInfo codeInfo = (EidCamera.EidCodeInfo)Marshal.PtrToStructure(
                        frameInfo.codeList + Marshal.SizeOf(typeof(EidCamera.EidCodeInfo)) * i,
                        typeof(EidCamera.EidCodeInfo));
                    
                    string data = Marshal.PtrToStringAnsi(codeInfo.data);
                    if (!string.IsNullOrEmpty(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)
                {
                    // 这里简化处理,如果是Jpeg则直接从内存加载,如果是Raw则需转换
                    // 实际项目中可根据 frameInfo.format 进行处理
                    if (frameInfo.isJpeg)
                    {
                        byte[] managedArray = new byte[frameInfo.imageDataLen];
                        Marshal.Copy(frameInfo.imageData, managedArray, 0, (int)frameInfo.imageDataLen);
                        using (var ms = new System.IO.MemoryStream(managedArray))
                        {
                            bitmap = new Bitmap(ms);
                        }
                    }
                }
 
                // 触发事件
                OnBarcodeRead(new BarcodeEventArgs(this.SN, barcodeInfos, bitmap));
            }
            catch (Exception ex)
            {
                // TODO: Log error
            }
        }
 
        public override void Dispose()
        {
            Close();
            base.Dispose();
        }
    }
}