alvin.chen
2019-11-29 edf127edc771f715c49f86e91e768633f0a0ed93
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using HalconDotNet;
using FlyCapture2Managed;
 
namespace PointGreyAndHalcon
{
    public delegate void GrabImageDoneDelegate(string recData,HImage recImage);
 
    public class PointGreyAndHalcon
    {
        #region 定义
        ManagedBusManager busMgr = new ManagedBusManager();
        //CameraInfo[] camInfos = ManagedBusManager.DiscoverGigECameras();
        int camNum;
        ManagedPGRGuid guid;
        ManagedGigECamera cam;
 
        public HImage image = new HImage();
        public event GrabImageDoneDelegate GrabImageDone;
        public string SerialNo;
 
        HImage tmpImage = new HImage();
        bool isCameraOpen = false;
        //bool isRightCameraOpen = false;
        #endregion
 
        public  PointGreyAndHalcon()
        {
            //camNum = camInfos.Length;
        }
 
        /// <summary>
        /// 打开相机
        /// </summary>
        /// <param name="index">编号</param>
        public void OpenCamera(int index)
        {
            try
            {
                guid = busMgr.GetCameraFromIndex((uint)index);
                cam = new ManagedGigECamera();
                cam.Connect(guid);
                cam.StartCapture();
                isCameraOpen = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("相机" + index.ToString() + "打开失败");
                
            }
        }
 
        /// <summary>
        /// 打开相机
        /// </summary>
        /// <param name="serialnumber">序列号</param>
        public bool OpenCamera(uint serialnumber)
        {
            try
            {
                guid = busMgr.GetCameraFromSerialNumber(serialnumber);
                cam = new ManagedGigECamera();
                cam.Connect(guid);
 
                cam.WriteRegister(0x610,0x00000000);//控制相机上电
                System.Threading.Thread.Sleep(100);
                cam.WriteRegister(0x610, 0x80000000);//控制相机下电
 
                cam.StartCapture();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("相机" + serialnumber.ToString() + "打开失败");
                return false;
            }
        }
 
        ///// <summary>
        ///// 打开相机
        ///// </summary>
        ///// <param name="serialnumber">序列号</param>
        //public void OpenCamera_test(uint serialnumber)
        //{
        //    try
        //    {
        //        guid = busMgr.GetCameraFromSerialNumber(serialnumber);
        //        cam = new ManagedGigECamera();
        //        cam.Connect(guid);
        //        cam.StartCapture(Test);
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("相机" + serialnumber.ToString() + "打开失败");
        //    }
        //}
 
        private void Test(ManagedImage image)
        {
            lock (GetMutexLock())
            {
                SerialNo = "";
                try
                {
                    //ManagedImage rawImage = new ManagedImage();
 
                    // Retrieve an image
                    //cam.RetrieveBuffer(rawImage);
                    
                    // Create a converted image
                    ManagedImage convertedImage = new ManagedImage();
 
                    // Convert the raw image
                    image.Convert(FlyCapture2Managed.PixelFormat.PixelFormatMono8, convertedImage);
 
                    //converterImage to HImage
                    Bitmap tmpBmp = convertedImage.bitmap;
                    BitmapData tmpBitmapData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
 
                    tmpImage.GenImage1("byte", tmpBmp.Width, tmpBmp.Height, tmpBitmapData.Scan0);
 
                    if (GrabImageDone != null)
                    {
                        GrabImageDone("GrabSuccess", tmpImage);
                    }
 
                    tmpBmp.UnlockBits(tmpBitmapData);
                    image.Dispose();
                    convertedImage.Dispose();
                    tmpBmp.Dispose();
                }
                catch (Exception ex)
                {
                    if (GrabImageDone != null)
                    {
                        GrabImageDone("GrabFail", tmpImage);
                    }
                }
            }
        }
 
        /// <summary>
        /// 关闭相机
        /// </summary>
        public void CloseCamera()
        {
            cam.StopCapture();
        }
 
        /// <summary>
        /// 采图
        /// </summary>
        public void GrabImage(string sn)
        {
            lock (GetMutexLock())
            {
                SerialNo = sn;
                try
                {
                    ManagedImage rawImage = new ManagedImage();
 
                    // Retrieve an image
                    cam.RetrieveBuffer(rawImage);
 
                    // Create a converted image
                    ManagedImage convertedImage = new ManagedImage();
 
                    // Convert the raw image
                    rawImage.Convert(FlyCapture2Managed.PixelFormat.PixelFormatMono8, convertedImage);
 
                    //converterImage to HImage
                    Bitmap tmpBmp = convertedImage.bitmap;
                    BitmapData tmpBitmapData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
 
                    tmpImage.GenImage1("byte", tmpBmp.Width, tmpBmp.Height, tmpBitmapData.Scan0);
                    image.Dispose();
                    image = tmpImage.CopyImage();
                    if (GrabImageDone != null)
                    {
                        GrabImageDone("GrabSuccess", tmpImage);
                    }
 
                    tmpBmp.UnlockBits(tmpBitmapData);
                    rawImage.Dispose();
                    convertedImage.Dispose();
                    tmpBmp.Dispose();
                }
                catch (Exception ex)
                {
                    if (GrabImageDone != null)
                    {
                        GrabImageDone("GrabFail", tmpImage);
                    }
                }
            }
        }
 
        /// <summary>
        /// 采彩色图
        /// </summary>
        public void GrabColorImage(string sn)
        {
            SerialNo = sn;
            try
            {
                ManagedImage rawImage = new ManagedImage();
 
                // Retrieve an image
                cam.RetrieveBuffer(rawImage);
 
                // Create a converted image
                ManagedImage convertedImage = new ManagedImage();
 
                // Convert the raw image
                rawImage.Convert(FlyCapture2Managed.PixelFormat.PixelFormatBgr, convertedImage);
 
                //converterImage to HImage
                Bitmap tmpBmp = convertedImage.bitmap;
                Rectangle rect = new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height);
                BitmapData tmpBitmapData = tmpBmp.LockBits(rect, ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb);
 
                #region 将RGB分离开来
                //创建新的彩色的bitmap
                Bitmap redBitmap = new Bitmap(tmpBmp.Width, tmpBmp.Height);
                Bitmap greenBitmap = new Bitmap(tmpBmp.Width, tmpBmp.Height);
                Bitmap blueBitmap = new Bitmap(tmpBmp.Width, tmpBmp.Height);
                //创建新的彩色的数据容器
                BitmapData redBmData = redBitmap.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                BitmapData greenBmData = greenBitmap.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                BitmapData blueBmData = blueBitmap.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                //将Bitmap对象的信息存放到byte数组中
                int tmp_bytes = tmpBitmapData.Stride * tmpBmp.Height;
                int red_bytes = redBmData.Stride * tmpBmp.Height;
                int green_bytes = greenBmData.Stride * tmpBmp.Height;
                int blue_bytes = blueBmData.Stride * tmpBmp.Height;
                byte[] tmpValues = new byte[tmp_bytes];
                byte[] redValues = new byte[red_bytes];
                byte[] greenValues = new byte[green_bytes];
                byte[] blueValues = new byte[blue_bytes];
                //复制GRB信息到byte数组
                System.Runtime.InteropServices.Marshal.Copy(tmpBitmapData.Scan0, tmpValues, 0, tmp_bytes);
                System.Runtime.InteropServices.Marshal.Copy(redBmData.Scan0, redValues, 0, red_bytes);
                System.Runtime.InteropServices.Marshal.Copy(greenBmData.Scan0, greenValues, 0, green_bytes);
                System.Runtime.InteropServices.Marshal.Copy(blueBmData.Scan0, blueValues, 0, blue_bytes);
 
                //将彩色信息分别存储到RGB数组中
                for (int i = 0; i < tmpBmp.Height; i++)
                {
                    for (int j = 0; j < tmpBmp.Width; j++)
                    {
                        int k = 3 * j;
                        redValues[i * redBmData.Stride + j] = tmpValues[i * tmpBitmapData.Stride + k + 2];
                        greenValues[i * blueBmData.Stride + j] = tmpValues[i * tmpBitmapData.Stride + k + 1];
                        blueValues[i * greenBmData.Stride + j] = tmpValues[i * tmpBitmapData.Stride + k];
                    }
                }
 
                //再将数组信息复制到RGB
                System.Runtime.InteropServices.Marshal.Copy(redValues, 0, redBmData.Scan0, red_bytes);
                System.Runtime.InteropServices.Marshal.Copy(greenValues, 0, greenBmData.Scan0, green_bytes);
                System.Runtime.InteropServices.Marshal.Copy(blueValues, 0, blueBmData.Scan0, blue_bytes);
 
                #endregion
 
                tmpImage.GenImage3("byte", tmpBmp.Width, tmpBmp.Height, redBmData.Scan0, greenBmData.Scan0, blueBmData.Scan0);
 
                if (GrabImageDone != null)
                {
                    GrabImageDone("GrabSuccess", tmpImage);
                }
 
                tmpBmp.UnlockBits(tmpBitmapData);
                redBitmap.UnlockBits(redBmData);
                greenBitmap.UnlockBits(greenBmData);
                blueBitmap.UnlockBits(blueBmData);
                rawImage.Dispose();
                convertedImage.Dispose();
                tmpBmp.Dispose();
                redBitmap.Dispose();
                greenBitmap.Dispose();
                blueBitmap.Dispose();
            }
            catch (Exception ex)
            {
                if (GrabImageDone != null)
                {
                    GrabImageDone("GrabFail", tmpImage);
                }
            }
        }
 
        /// <summary>
        /// 将图片显示到图相框中
        /// </summary>
        public void UpdateImage(HWindowControl viewPort)
        {
            HWndCtrl viewController = new HWndCtrl(viewPort);
            ROIController roiController = new ROIController();
            viewController.resetAll();
            viewController.useROIController(roiController);
            viewController.setViewState(HWndCtrl.MODE_VIEW_NONE);
 
            viewController.addIconicVar(image);
            viewController.repaint();
            //image.Dispose();
        }
 
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        public void SaveImage(string filePath, string fileName)
        {
            image.WriteImage("tiff", 0, filePath + "\\" + fileName);
        }
        
        public void SetExposure(float exposure)
        {
            CameraProperty cp = cam.GetProperty(PropertyType.Shutter);
            cp.absValue = exposure;
            cp.autoManualMode = false;
            cam.SetProperty(cp);
        }
 
        private static System.Threading.Mutex _mutex;
 
        private System.Threading.Mutex GetMutexLock()
        {
            System.Threading.Interlocked.CompareExchange(ref _mutex, new System.Threading.Mutex(), null);
            return _mutex;
        }
    }
}