patrick.xu
2021-05-24 f13235bee2e25d091ba13dc3732b83f905565f80
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
 
using System.Drawing;
using FlyCapture2Managed;
using System.Threading;
 
namespace M423project
{
    /// <summary>
    /// 利用Flycapture库连接的相机
    /// </summary>
    public class FlyCaptureCamera : CameraBase
    {
        // 摄像机序列号
        public string       SerialNum {get;set;}
 
        // 抓拍或扫描到的图片
        public ManagedImage   Image {
            get { return (ManagedImage)base.ImageObj; }
            set { base.ImageObj = value; }
        }
 
        // 图片像素格式
        public PixelFormat PixFormat { get; set; }
 
        // 取景相机
        protected ManagedCamera cam = null;
 
        public double ExposureValue { get; set; }
        public double GainValue { get; set; }
 
 
        public FlyCaptureCamera(double _exposureValue, double _gainValue)
        {
            this.SerialNum = "";
            this.Image = null;
            ExposureValue = _exposureValue;// 4.97;
            GainValue = _gainValue;// 1.9;
            this.PixFormat = PixelFormat.PixelFormatMono8;
            this.cam = new ManagedCamera();
        }
 
        /// <summary>
        /// 相机初始化
        /// </summary>
        /// <returns></returns>
        public override bool Init()
        {
            if (base.IsInit)
            {
                return true;
            }
 
            try
            {
                //ManagedBusManager.ForceAllIPAddressesAutomatically(Convert.ToUInt32(SerialNum));
 
                base.Init();
            }
            catch (Exception e)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("{0} Init fail, serialNum:{1} ex:{2}", Desp, SerialNum, e.Message));
                return false;
            }
 
            return true;
        }
 
        /// <summary>
        /// 打开
        /// </summary>
        /// <returns></returns>
        public override bool Open()
        {
            if (base.IsOpen)
            {
                return true;
            }
            
            try
            {
                var busManager  = new ManagedBusManager();
                var guid = busManager.GetCameraFromSerialNumber(Convert.ToUInt32(SerialNum));
 
                cam.Connect(guid);
 
                InitParams();
                SetShutter(ExposureValue);
                SetGain(GainValue);
 
                base.Open();
 
                return true;
            }
            catch(Exception ex)
            {
                cam = null;
                CommonUtil.WriteLog(LogType.Exc, string.Format("FlayCaptureCamera serialNum{0} open fail, ex:{1}", SerialNum, ex.Message));
            }
 
            return false;
        }
 
 
        public void SetShutter(double ShutterVal)
        {
            try
            {
                CameraProperty campara = cam.GetProperty(PropertyType.Shutter);
                campara.absControl = true;
                campara.autoManualMode = false;
                campara.absValue = (float)ShutterVal;
                cam.SetProperty(campara);
            }
            catch(Exception )
            {
 
            }
 
        }
 
        public void SetGain(double gainVal)
        {
            try
            {
                CameraProperty campara = cam.GetProperty(PropertyType.Gain);
                campara.absControl = true;
                campara.autoManualMode = false;
                campara.absValue = (float)gainVal;
                cam.SetProperty(campara);
            }
            catch(Exception )
            {
            }
 
        }
 
        /// <summary>
        /// 关闭
        /// </summary>
        public override void Close()
        {
            if (!base.IsOpen)
            {
                return;
            }
 
            if ( null == cam )
            {
                return;
            }
 
            try
            {
                cam.Disconnect();
                cam = null;
 
                base.Close();
            }
            catch(Exception ex)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("FlayCaptureCamera serialNum:{0} Close fail, ex:{1}", SerialNum, ex.Message));
            }
        }
 
        /// <summary>
        /// 开始抓拍
        /// </summary>
        public override bool Snapshot()
        {
            if (!base.IsOpen)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("FlayCaptureCamera serialNum:{0} Snapshot fail, not open", SerialNum));
                return false;
            }
 
            try
            {
                cam.StartCapture();
                System.Threading.Thread.Sleep(20);
                GrabImage();
 
                cam.StopCapture();
 
                return true;
            }
            catch(Exception ex)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("FlayCaptureCamera serialNum:{0} StartCapture fail, ex:{1}", SerialNum, ex.Message));
            }
            
            return false;
        }
 
        /// <summary>
        /// 从摄像机抓取图像
        ///// </summary>
        /// <returns></returns>
        public void GrabImage()
        {
            try
            {
                var curImage = new ManagedImage();
                var rawImage = new ManagedImage();
 
                this.FreeImage();
 
                // Retrieve an image
                cam.RetrieveBuffer(rawImage);
                // Convert the raw image
                rawImage.Convert(this.PixFormat, curImage);
                this.Image = curImage;
 
                this.NotifyImageEvent();
 
                rawImage.Dispose();
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Exc, string.Format("FlayCaptureCamera serialNum:{0} GrabImage fail, ex:{1}", SerialNum, ex.Message));
 
            }
        }
 
        /// <summary>
        /// 释放图片数据
        /// </summary>
        protected override void FreeImage()
        {
            if (null != this.Image)
            {
                this.Image.Dispose();
                this.Image = null;
            }
        }
 
        /// <summary>
        /// 把抓拍图片转成bitmap图片
        /// </summary>
        /// <returns></returns>
        public override Bitmap GetBitmap()
        {
            if (null == this.Image)
            {
                return null;
            }
 
            return this.Image.bitmap;
        }
    }
}