C3204
2025-12-25 32e832a898a6466369669717e7b11f7c957371bb
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using HalconDotNet;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
 
namespace LB_VisionProcesses.Cameras
{
    public abstract class BaseCamera : ICamera
    {
        protected BaseCamera() { }
 
        #region Parm
        public string SN { get; set; } = string.Empty;
        public CameraBrand Brand { get; set; } = CameraBrand.UNSUPPORTED;
 
        public bool isGrabbing = false;
 
        //public EventHandler<CameraEventArgs> ImageGrabbed = delegate { };
 
        private EventHandler<CameraEventArgs> _imageGrabbed;
        public EventHandler<CameraEventArgs> ImageGrabbed
        {
            get => _imageGrabbed;
            set
            {
                Debug.WriteLine($"ImageGrabbed 被设置,新值: {value?.Method}");
                Debug.WriteLine($"调用堆栈: {Environment.StackTrace}");
                _imageGrabbed = value;
            }
        }
 
        protected static Bitmap CallBackImg { get; set; }
 
        private Dictionary<string, List<Bitmap>> _collectedImages;
        public Dictionary<string, List<Bitmap>> CollectedImages
        {
            get => _collectedImages;
            set
            {
                _collectedImages = value;
            }
        }
 
        public Bitmap Bitmap;
        #endregion
 
        #region  operate
 
        public virtual void Dispose()
        {
            try
            {
                CloseDevice();
            }
            catch { }
        }
        public abstract bool CloseDevice();
 
        public abstract List<string> GetListEnum();
 
        public abstract bool InitDevice(string SN, object Handle = null);
 
        public bool StartWith_SoftTriggerModel()
        {
            SetTriggerMode(TriggerMode.Off, TriggerSource.Software);
            return StartGrabbing();
        }
 
        public bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
        {
            if (hardtriggeritem == TriggerSource.Software) hardtriggeritem = TriggerSource.Line0;
            SetTriggerMode(TriggerMode.On, hardtriggeritem);
            return StartGrabbing();
        }
 
        /// <summary>
        /// 等待硬触发获取图像
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="outtime"></param>
        /// <returns></returns>
        public bool GetImage(out Bitmap bitmap, int outtime = 3000)
        {
            bitmap = null;
 
            try
            {
                // 设置超时时间
                DateTime lastTime = DateTime.Now.AddMilliseconds(outtime);
                // 判断是否超时
                while (lastTime > DateTime.Now)// 设置超时时间为 3 秒
                {
                    if (CallBackImg != null)
                    {
                        lock (CallBackImg)
                        {
                            // 保存旧 Bitmap 并释放
                            bitmap = CallBackImg.Clone() as Bitmap; // 创建副本
                        }
 
                        // 释放旧资源
                        CallBackImg.Dispose();
                        CallBackImg = null;
                        return true;
                    }
                }
 
                return false;
            }
            catch { return bitmap == null ? false : true; }
        }
 
        /// <summary>
        /// 软触发获取图像
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="outtime"></param>
        /// <returns></returns>
        public bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
        {
            if (!isGrabbing)
                StartGrabbing();
 
            GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
 
            if (triggerMode != TriggerMode.On && triggerSource != TriggerSource.Software)
                SetTriggerMode(TriggerMode.On, TriggerSource.Software);
 
            bitmap = null;
            CallBackImg = null;
 
            if (!SoftTrigger())
                return false;
 
            // 开始时间
            DateTime startTime = DateTime.Now; // 当前时间
 
            // 判断是否超时
            while (DateTime.Now < startTime.AddMilliseconds(outtime))// 设置超时时间为 3 秒
            {
                GetImage(out bitmap, 50);
                if (bitmap != null)
                    break;
 
                Thread.Sleep(10);
            }
 
            if (triggerMode != TriggerMode.On)
                SetTriggerMode(TriggerMode.On, triggerSource);
 
            return (bitmap != null);
        }
 
        /// <summary>
        /// 软触发
        /// </summary>
        /// <returns></returns>
        public abstract bool SoftTrigger();
 
        public void Bitmap2HObject(Bitmap bmp, out HObject image)
        {
            try
            {
                if (bmp == null)
                {
                    image = null;
                    return;
                }
                BitmapData srcBmpData;
                switch (bmp.PixelFormat)
                {
                    case PixelFormat.Format24bppRgb:
                        srcBmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                        HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                        bmp.UnlockBits(srcBmpData);
                        break;
                    default:
                        srcBmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
 
                        HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                        bmp.UnlockBits(srcBmpData);
                        break;
                }
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
 
        #endregion
 
        #region SettingConfig
        public void SetCamConfig(CameraConfig config)
        {
            if (Enum.TryParse(config.Params.Inputs["触发模式"].ToString(), out TriggerMode TriggerMode)
                && Enum.TryParse(config.Params.Inputs["触发方式"].ToString(), out TriggerSource TriggerSource)
                && Enum.TryParse(config.Params.Inputs["触发极性"].ToString(), out TriggerPolarity TriggerPolarity)
                )
            {
                SetTriggerMode(TriggerMode, TriggerSource);
                SetTriggerPolarity(TriggerPolarity);
                SetTriggerFliter(Convert.ToDouble(config.Params.Inputs["触发消抖"].ToString()));
                SetTriggerDelay(Convert.ToDouble(config.Params.Inputs["触发延时"].ToString()));
                SetExpouseTime(Convert.ToDouble(config.Params.Inputs["曝光时间"].ToString()));
                SetGain(Convert.ToDouble(config.Params.Inputs["增益"].ToString()));
            }
        }
 
        public void GetCamConfig(out CameraConfig config)
        {
            GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
            GetTriggerPolarity(out TriggerPolarity triggerPolarity);
            GetTriggerFliter(out double triggerfilter);
            GetTriggerDelay(out double triggerdelay);
            GetExpouseTime(out double expouseTime);
            GetGain(out double gain);
 
            config = new CameraConfig(null);
            config.Params.Inputs.Add("触发模式", triggerMode);
            config.Params.Inputs.Add("触发方式", triggerSource);
            config.Params.Inputs.Add("触发极性", triggerPolarity);
            config.Params.Inputs.Add("触发消抖", triggerfilter);
            config.Params.Inputs.Add("触发延时", triggerdelay);
            config.Params.Inputs.Add("曝光时间", expouseTime);
            config.Params.Inputs.Add("增益", gain);
        }
 
        /// <summary>
        /// 设置触发模式及触发源
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="triggerEnum"></param>
        /// <returns></returns>
        public abstract bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0);
 
        public abstract bool GetTriggerMode(out TriggerMode mode, out TriggerSource source);
 
        public abstract bool SetExpouseTime(double value);
 
        public abstract bool GetExpouseTime(out double value);
 
        public abstract bool SetTriggerPolarity(TriggerPolarity polarity);
 
        public abstract bool GetTriggerPolarity(out TriggerPolarity polarity);
 
        /// <summary>
        /// 设置触发滤波时间 (us)
        /// </summary>
        /// <param name="flitertime"></param>
        /// <returns></returns>
        public abstract bool SetTriggerFliter(double flitertime);
 
        /// <summary>
        /// 获取触发参数时间 (us)
        /// </summary>
        /// <param name="flitertime"></param>
        /// <returns></returns>
        public abstract bool GetTriggerFliter(out double flitertime);
 
        public abstract bool SetTriggerDelay(double delay);
 
        public abstract bool GetTriggerDelay(out double delay);
 
        public abstract bool SetGain(double gain);
 
        public abstract bool GetGain(out double gain);
 
        public abstract bool SetLineMode(IOLines line, LineMode mode);
 
        public abstract bool SetLineStatus(IOLines line, LineStatus linestatus);
 
        public abstract bool GetLineStatus(IOLines line, out LineStatus lineStatus);
 
        public abstract bool AutoBalanceWhite();
 
        #endregion
 
        #region  protected abstract
        /// <summary>
        /// 开始采图
        /// </summary>
        /// <returns></returns>
        public abstract bool StartGrabbing();
 
        /// <summary>
        /// 停止采图
        /// </summary>
        /// <returns></returns>
        public abstract bool StopGrabbing();
        #endregion
    }
}