QuakeGod
2021-07-29 3b04f942bd51c0453cbb64167cbdb7de69159bd5
提交 | 用户 | age
4b03ae 1 #include "StdAfx.h"
Q 2 #include "BaslerCamera.h"
3 #include "../ImageFunc/ImageFunc.h"
4
5 #pragma comment(lib,"PylonGigE_MD_VC100_TL.lib")
6
7 volatile int BaslerCamera::Inited=0;        //是否已经初始化
8 int BaslerCamera::TotalCameraCount=0;        //相机数量
9
10 using namespace std;
11
12 BaslerCamera::BaslerCamera(void)
13 {
14     m_IsCameraOpened=0;
15     ExposeTimemS=35;
16 }
17
18 BaslerCamera::~BaslerCamera(void)
19 {
20     if (m_IsCameraOpened)
21     {
22         this->CloseCamera();
23     }
24 }
25
26 int BaslerCamera::InitLib()
27 {
28     if (0==Inited++)
29     {
30         Pylon::DeviceInfoList list;
31         Pylon::DeviceInfoList filter;
32         TotalCameraCount=Pylon::CTlFactory::GetInstance().EnumerateDevices(list,filter);
33     }
34     return TotalCameraCount;
35 }
36 int BaslerCamera::GetCameraInfo(int index,    MyCameraInfo * pMyCameraInfo)
37 {
38     CString s1;
39     CString results[10];
40     ResultStr.Empty();
41
42     Pylon::CBaslerGigEDeviceInfo info = camera.GetDeviceInfo();
43
44     if (info.IsVendorNameAvailable())
45     {
46         pMyCameraInfo->sMfgName = info.GetVendorName();
47     }
48     if (info.IsModelNameAvailable())
49     {
50         pMyCameraInfo->sModelName = info.GetModelName();
51     }
52     if (info.IsSerialNumberAvailable())
53     {
54         pMyCameraInfo->sSerialNumber = info.GetSerialNumber();
55     }
56     if (info.IsUserDefinedNameAvailable())
57     {
58         pMyCameraInfo->sUserDefinedName = info.GetUserDefinedName();
59     }
60
61     if (info.IsIpAddressAvailable())
62     {
63         s1 = info.GetIpAddress();
64         Split(s1,_T("."),results);
65         pMyCameraInfo->camIp[0] = _tstoi(results[0]);
66         pMyCameraInfo->camIp[1] = _tstoi(results[1]);
67         pMyCameraInfo->camIp[2] = _tstoi(results[2]);
68         pMyCameraInfo->camIp[3] = _tstoi(results[3]);
69     }
70     
71     if (info.IsMacAddressAvailable())
72     {
73         s1 = info.GetMacAddress();
74         Split(s1,_T("-"),results);
75         pMyCameraInfo->camMac[0] = _tstoi(results[0]);
76         pMyCameraInfo->camMac[1] = _tstoi(results[1]);
77         pMyCameraInfo->camMac[2] = _tstoi(results[2]);
78         pMyCameraInfo->camMac[3] = _tstoi(results[3]);
79         pMyCameraInfo->camMac[4] = _tstoi(results[4]);
80         pMyCameraInfo->camMac[5] = _tstoi(results[5]);
81     }
82
83     if (info.IsInterfaceAvailable())
84     {
85         s1 = info.GetInterface();
86         Split(s1,_T("."),results);
87         pMyCameraInfo->ifIp[0] = _tstoi(results[0]);
88         pMyCameraInfo->ifIp[1] = _tstoi(results[1]);
89         pMyCameraInfo->ifIp[2] = _tstoi(results[2]);
90         pMyCameraInfo->ifIp[3] = _tstoi(results[3]);
91     }
92
93     return CameraOk;
94 }
95 int BaslerCamera::OpenCamera(int Index)
96 {
97     CString s1;
98     s1.Format(_T("开始初始化GigeVision相机 ....\r\n"));
99
100     if (m_IsCameraOpened) {this->CloseCamera();}
101     try
102     {
103         Pylon::IPylonDevice *pdevice1;
104         Pylon::DeviceInfoList list;
105         Pylon::DeviceInfoList filter;
106         Pylon::CDeviceInfo di;
107
108         int j;
109         j=Pylon::CTlFactory::GetInstance().EnumerateDevices(list,filter);
110         if (j<=Index) {return -1;}
111         di=list.at(Index);
112         pdevice1=Pylon::CTlFactory::GetInstance().CreateDevice(di);
113
114         camera.Attach( pdevice1);
115         camera.Open();
116         camera.PixelFormat.SetValue(Basler_GigECamera::PixelFormat_BayerGB8);
117         camera.TriggerSelector.SetValue(Basler_GigECamera::TriggerSelector_FrameStart,true);
118         camera.TriggerMode.SetValue(Basler_GigECamera::TriggerMode_On,true);
119         camera.TriggerSource.SetValue(Basler_GigECamera::TriggerSource_Software,true);
120
121         m_IsCameraOpened=1;
122         s1.Format(_T("打开Basler相机%d成功 \r\n"),Index);
123         ResultStr=s1;
124     }
125     catch (GenICam::GenericException &e)
126     {
127         // Error handling
128         cerr << "An exception occurred." << endl << e.GetDescription() << endl;
129
130         CStringA s1A;
131         CString s1T;
132         s1A=e.GetDescription();
133         AnsiToT(s1A,s1T);
134         s1.Format(_T("相机初始化失败 具体信息为 %s \r\n"),s1T);
135         ResultStr=s1;
136
137         m_IsCameraOpened=0;
138         return CameraError;
139     }
140     catch (...)
141     {
142         return CameraError;
143     }
144     return CameraOk;
145 }
146
147 int BaslerCamera::CloseCamera()
148 {
149     camera.Close();
150     camera.DetachDevice();
151     this->m_IsCameraOpened=0;
152     return CameraOk;
153 }
154
155 int BaslerCamera::SetExposureTime(double ExposeTime)
156 {
157     try
158     {
159         ExposeTimemS=ExposeTime;
160         if (m_IsCameraOpened)
161         camera.ExposureTimeAbs.SetValue(ExposeTimemS*1000.0,true);
162     }
163     catch (GenICam::GenericException &e)
164     {
165         // Error handling
166         CString s1;
167         cerr << "An exception occurred." << endl << e.GetDescription() << endl;
168         CStringA s1A;
169         CString s1T;
170         s1A=e.GetDescription();
171         AnsiToT(s1A,s1T);
172         s1.Format(_T("设置相机参数失败 具体信息为 %s \r\n"),s1T);
173         ResultStr=s1;
174
175         return CameraError;
176     }
177     return CameraOk;
178 }
179 int BaslerCamera::StartGrab()
180 {
181     CString s1;
182     try
183     {
184         camera.ExposureTimeAbs.SetValue(ExposeTimemS*1000.0,true);
185
186         camera.StartGrabbing(1,Pylon::GrabStrategy_OneByOne,Pylon::GrabLoop_ProvidedByUser);
187
188         if (camera.WaitForFrameTriggerReady(TimeOutmS))
189         {
190             camera.ExecuteSoftwareTrigger();
191         }else
192         {
193             return CameraError;
194         }
195     }
196     catch (GenICam::GenericException &e)
197     {
198         //Error handling
199         cerr << "An exception occurred." << endl << e.GetDescription() << endl;
200         CStringA s1A;
201         CString s1T;
202         s1A=e.GetDescription();
203         AnsiToT(s1A,s1T);
204         s1.Format(_T(" %s \r\n"),s1T);
205         ResultStr=s1;
206
207         camera.StopGrabbing();
208
209         return CameraError;
210
211     }
212     return CameraOk;
213 }
214 int BaslerCamera::WaitForGrabFinish(int TimeOutmS)
215 {
216     CString s1;
217     try
218     {
219         if (camera.RetrieveResult(TimeOutmS,ptrGrabResult,Pylon::TimeoutHandling_ThrowException))
220             return CameraOk;
221         else return CameraError;
222     }
223     catch (GenICam::GenericException &e)
224     {
225         // Error handling
226         cerr << "An exception occurred." << endl << e.GetDescription() << endl;
227
228         CStringA s1A;
229         CString s1T;
230         s1A=e.GetDescription();
231         AnsiToT(s1A,s1T);
232         s1.Format(_T(" %s \r\n"),s1T);
233         ResultStr=s1;
234
235         camera.StopGrabbing();
236
237         return CameraError;
238
239     }
240     return CameraOk;
241 }
242 int BaslerCamera::FetchImageData()
243 {
244     CString s1;
245
246     if (ptrGrabResult==NULL)
247     {
248         return CameraError;
249     }
250     if (ptrGrabResult->GrabSucceeded())
251     {
252         int w,h;
253         size_t stride;
254
255         Pylon::PixelType pixeltype;
256         pixeltype=ptrGrabResult->GetPixelType();
257         ptrGrabResult->GetStride(stride);
258         w=ptrGrabResult->GetWidth();
259         h=ptrGrabResult->GetHeight();
260         UCHAR *srcpixel=(UCHAR *)(ptrGrabResult->GetBuffer());
261         if (pixeltype==Pylon::PixelType_BayerGB8)
262         {
263             Image1.CopyFromBuf(srcpixel,w,h,stride,MyImage::MyPixelType_BayerGB8);
264         }
265
266         s1.Format(_T("拍照成功 pixeltype %X w %d h %d stride %d  \r\n"),pixeltype,w,h,stride);
267         ResultStr=s1;
268     }
269     else
270     {
271         CStringA s1A;
272         s1A=ptrGrabResult->GetErrorDescription().c_str();
273         CString s1T;
274         AnsiToT(s1A,s1T);
275         s1.Format(_T("拍照失败 %0X %s\r\n"),ptrGrabResult->GetErrorCode(),s1T);
276         ResultStr=s1;
277
278         return CameraError;
279     }
280     return CameraOk;
281 }
282
283 int BaslerCamera::SetPixelFormat(Basler_GigECamera::PixelFormatEnums newPixelFormat)
284 {
285     try
286     {
287         camera.PixelFormat.SetValue(newPixelFormat);
288     }
289     catch (GenICam::GenericException &e)
290     {
291         // Error handling
292         CString s1;
293         cerr << "An exception occurred." << endl << e.GetDescription() << endl;
294         CStringA s1A;
295         CString s1T;
296         s1A=e.GetDescription();
297         AnsiToT(s1A,s1T);
298         s1.Format(_T("设置相机参数失败 具体信息为 %s \r\n"),s1T);
299
300         ResultStr=s1;
301         return CameraError;
302     }
303     return CameraOk;
304 }