提交 | 用户 | age
|
4b03ae
|
1 |
|
Q |
2 |
#include "stdafx.h"
|
|
3 |
#include "MVSCamera.h"
|
|
4 |
#include "../ImageFunc/ImageFunc.h"
|
|
5 |
volatile int MVSCamera::TotalCameraCount=0; //相机数量
|
|
6 |
|
|
7 |
#ifdef _WIN64
|
|
8 |
#pragma comment(lib,"../Mylib/camera/MVS_SDK/win64/MvCameraControl.lib")
|
|
9 |
#else
|
|
10 |
#pragma comment(lib,"../Mylib/camera/MVS_SDK/win32/MvCameraControl.lib")
|
|
11 |
#endif
|
|
12 |
|
|
13 |
#pragma comment(lib,"gdiplus.lib")
|
|
14 |
MVSCamera::MVSCamera(void)
|
|
15 |
{
|
|
16 |
Idx=0;
|
|
17 |
m_IsCameraOpened=0;
|
|
18 |
SnapCount=0;
|
|
19 |
TrigeredCount=0;
|
|
20 |
BackCallCount=0;
|
|
21 |
|
|
22 |
pImageBuf = NULL;
|
|
23 |
};
|
|
24 |
|
|
25 |
MVSCamera::~MVSCamera(void)
|
|
26 |
{
|
|
27 |
if (pImageBuf!=NULL)
|
|
28 |
{
|
|
29 |
delete[] pImageBuf;
|
|
30 |
pImageBuf = NULL;
|
|
31 |
}
|
|
32 |
int nRet;
|
|
33 |
//停止采集图像
|
|
34 |
nRet = MV_CC_StopGrabbing(m_hCam);
|
|
35 |
if (MV_OK != nRet)
|
|
36 |
{
|
|
37 |
return;
|
|
38 |
}
|
|
39 |
|
|
40 |
//关闭设备,释放资源
|
|
41 |
nRet = MV_CC_CloseDevice(m_hCam);
|
|
42 |
if (MV_OK != nRet)
|
|
43 |
{
|
|
44 |
return;
|
|
45 |
}
|
|
46 |
|
|
47 |
//销毁句柄,释放资源
|
|
48 |
nRet = MV_CC_DestroyHandle(m_hCam);
|
|
49 |
if (MV_OK != nRet)
|
|
50 |
{
|
|
51 |
return;
|
|
52 |
}
|
|
53 |
};
|
|
54 |
|
|
55 |
int MVSCamera::InitLib(void)
|
|
56 |
{
|
|
57 |
CString s1;
|
|
58 |
int nRet = -1;
|
|
59 |
//枚举子网内指定的传输协议对应的所有设备
|
|
60 |
MV_CC_DEVICE_INFO_LIST m_stDevList = {0};
|
|
61 |
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE, &m_stDevList);
|
|
62 |
if (MV_OK != nRet)
|
|
63 |
{
|
|
64 |
return 0;
|
|
65 |
}
|
|
66 |
|
|
67 |
TotalCameraCount=m_stDevList.nDeviceNum;
|
|
68 |
return TotalCameraCount;
|
|
69 |
}
|
|
70 |
int MVSCamera::GetCameraInfo(int index, MyCameraInfo * pMyCameraInfo)
|
|
71 |
{
|
|
72 |
CStringA s1A;
|
|
73 |
CString s1;
|
|
74 |
ResultStr.Empty();
|
|
75 |
|
|
76 |
MV_CC_DEVICE_INFO info1;
|
|
77 |
int nRet = MV_CC_GetDeviceInfo(m_hCam,&info1);
|
|
78 |
|
|
79 |
if (MV_OK != nRet)
|
|
80 |
{
|
|
81 |
ResultStr.Format(_T("获取相机信息失败: GetDeviceInfo [%x]"), nRet);
|
|
82 |
return CameraError;
|
|
83 |
}
|
|
84 |
|
|
85 |
memcpy(pMyCameraInfo->mMfgName,info1.SpecialInfo.stGigEInfo.chManufacturerName,32);
|
|
86 |
s1A=pMyCameraInfo->mMfgName;
|
|
87 |
AnsiToT(s1A,s1);
|
|
88 |
pMyCameraInfo->sMfgName=s1;
|
|
89 |
|
|
90 |
memcpy(pMyCameraInfo->mModelName,info1.SpecialInfo.stGigEInfo.chModelName,32);
|
|
91 |
s1A=pMyCameraInfo->mModelName;
|
|
92 |
AnsiToT(s1A,s1);
|
|
93 |
pMyCameraInfo->sModelName=s1;
|
|
94 |
|
|
95 |
memcpy(pMyCameraInfo->mSerialNumber,info1.SpecialInfo.stGigEInfo.chSerialNumber,16);
|
|
96 |
s1A=pMyCameraInfo->mSerialNumber;
|
|
97 |
AnsiToT(s1A,s1);
|
|
98 |
pMyCameraInfo->sSerialNumber=s1;
|
|
99 |
|
|
100 |
memcpy(pMyCameraInfo->mUserDefinedName,info1.SpecialInfo.stGigEInfo.chUserDefinedName,16);
|
|
101 |
s1A=pMyCameraInfo->mUserDefinedName;
|
|
102 |
AnsiToT(s1A,s1);
|
|
103 |
pMyCameraInfo->sUserDefinedName=s1;
|
|
104 |
|
|
105 |
pMyCameraInfo->camIp[0] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 3*8) & 0xff;
|
|
106 |
pMyCameraInfo->camIp[1] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 2*8) & 0xff;
|
|
107 |
pMyCameraInfo->camIp[2] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 1*8) & 0xff;
|
|
108 |
pMyCameraInfo->camIp[3] = (info1.SpecialInfo.stGigEInfo.nCurrentIp >> 0*8) & 0xff;
|
|
109 |
|
|
110 |
pMyCameraInfo->camMac[0] = (info1.nMacAddrHigh >> 1*8) & 0xff;
|
|
111 |
pMyCameraInfo->camMac[1] = (info1.nMacAddrHigh >> 0*8) & 0xff;
|
|
112 |
pMyCameraInfo->camMac[2] = (info1.nMacAddrLow >> 3*8) & 0xff;
|
|
113 |
pMyCameraInfo->camMac[3] = (info1.nMacAddrLow >> 2*8) & 0xff;
|
|
114 |
pMyCameraInfo->camMac[4] = (info1.nMacAddrLow >> 1*8) & 0xff;
|
|
115 |
pMyCameraInfo->camMac[5] = (info1.nMacAddrLow >> 0*8) & 0xff;
|
|
116 |
|
|
117 |
pMyCameraInfo->ifIp[0] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 3*8) & 0xff;
|
|
118 |
pMyCameraInfo->ifIp[1] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 2*8) & 0xff;
|
|
119 |
pMyCameraInfo->ifIp[2] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 1*8) & 0xff;
|
|
120 |
pMyCameraInfo->ifIp[3] = (info1.SpecialInfo.stGigEInfo.nNetExport >> 0*8) & 0xff;
|
|
121 |
|
|
122 |
return CameraOk;
|
|
123 |
}
|
|
124 |
int MVSCamera::OpenCamera(int Index)
|
|
125 |
{
|
|
126 |
ResultStr.Empty();
|
|
127 |
Idx=Index;
|
|
128 |
MV_CC_DEVICE_INFO_LIST m_stDevList = {0};
|
|
129 |
int nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE, &m_stDevList);
|
|
130 |
if (MV_OK != nRet)
|
|
131 |
{
|
|
132 |
ResultStr.Format(_T("查找相机失败: EnumDevices [%x]"),nRet);
|
|
133 |
return CameraError;
|
|
134 |
}
|
|
135 |
|
|
136 |
if (Idx>=m_stDevList.nDeviceNum)
|
|
137 |
{
|
|
138 |
ResultStr.Format(_T("打开相机失败: 相机 %d MVS相机总数 %d"), Idx, m_stDevList.nDeviceNum);
|
|
139 |
return CameraError;
|
|
140 |
}
|
|
141 |
|
|
142 |
MV_CC_DEVICE_INFO m_stDevInfo = {0};
|
|
143 |
memcpy(&m_stDevInfo, m_stDevList.pDeviceInfo[Idx], sizeof(MV_CC_DEVICE_INFO));
|
|
144 |
nRet = MV_CC_CreateHandleWithoutLog(&m_hCam, &m_stDevInfo);
|
|
145 |
|
|
146 |
if (MV_OK != nRet)
|
|
147 |
{
|
|
148 |
ResultStr.Format(_T("打开相机失败: CreateHandle [%x]"), nRet);
|
|
149 |
return CameraError;
|
|
150 |
}
|
|
151 |
//连接设备
|
|
152 |
nRet = MV_CC_OpenDevice(m_hCam);
|
|
153 |
if (MV_OK != nRet)
|
|
154 |
{
|
|
155 |
ResultStr.Format(_T("打开相机失败: OpenDevice [%x]"), nRet);
|
|
156 |
return CameraError;
|
|
157 |
}
|
|
158 |
|
|
159 |
m_IsCameraOpened=1;
|
|
160 |
|
|
161 |
MVCC_INTVALUE iValue;
|
|
162 |
MV_CC_GetWidth(m_hCam,&iValue);
|
|
163 |
m_nWidth = iValue.nCurValue;
|
|
164 |
|
|
165 |
MV_CC_GetHeight(m_hCam,&iValue);
|
|
166 |
m_nHeight = iValue.nCurValue;
|
|
167 |
|
|
168 |
nRet = MV_CC_SetPixelFormat(m_hCam,m_PixelFormat);
|
|
169 |
if (MV_OK != nRet)
|
|
170 |
{
|
|
171 |
ResultStr.Format(_T("打开相机失败: SetPixelFormat [%x]"), nRet);
|
|
172 |
return CameraError;
|
|
173 |
}
|
|
174 |
|
|
175 |
MV_CC_SetTriggerMode(m_hCam,MV_TRIGGER_MODE_ON);
|
|
176 |
MV_CC_SetTriggerSource(m_hCam,MV_TRIGGER_SOURCE_SOFTWARE);
|
|
177 |
|
|
178 |
UpdateFps();
|
|
179 |
|
|
180 |
nRet = MV_CC_RegisterImageCallBackEx(m_hCam, MVSCamera::ImageCallBackEx, this);
|
|
181 |
if (MV_OK != nRet)
|
|
182 |
{
|
|
183 |
ResultStr.Format(_T("打开相机失败: RegisterImageCallBackEx [%x]"), nRet);
|
|
184 |
return CameraError;
|
|
185 |
}
|
|
186 |
|
|
187 |
//开始采集图像
|
|
188 |
nRet = MV_CC_StartGrabbing(m_hCam);
|
|
189 |
if (MV_OK != nRet)
|
|
190 |
{
|
|
191 |
ResultStr.Format(_T("打开相机失败: StartGrabbing [%x]"), nRet);
|
|
192 |
return CameraError;
|
|
193 |
}
|
|
194 |
|
|
195 |
ResultStr.Format(_T("打开MVS相机%d[%p],成功"),Index,this);
|
|
196 |
return CameraOk;
|
|
197 |
}
|
|
198 |
|
|
199 |
int MVSCamera::CloseCamera()
|
|
200 |
{
|
|
201 |
return MV_CC_CloseDevice(m_hCam);
|
|
202 |
}
|
|
203 |
int MVSCamera::GetExposureTime(double *ExposureTime)
|
|
204 |
{
|
|
205 |
MVCC_FLOATVALUE fValue;
|
|
206 |
MV_CC_GetExposureTime(m_hCam, &fValue);
|
|
207 |
*ExposureTime = fValue.fCurValue;
|
|
208 |
return CameraOk;
|
|
209 |
}
|
|
210 |
int MVSCamera::SetExposureTime(double ExposeTime)
|
|
211 |
{
|
|
212 |
ResultStr.Empty();
|
|
213 |
m_fExpo=float(ExposeTime);
|
|
214 |
if (!m_IsCameraOpened)
|
|
215 |
{
|
|
216 |
ResultStr.Format(_T("相机未打开"));
|
|
217 |
return CameraError;
|
|
218 |
}
|
|
219 |
int nRet=MV_CC_SetExposureTime(m_hCam,m_fExpo*1000.0f);
|
|
220 |
if (MV_OK == nRet)
|
|
221 |
{
|
|
222 |
ResultStr.Format(_T("设置曝光时间%.1f成功"),m_fExpo);
|
|
223 |
return CameraOk;
|
|
224 |
}else
|
|
225 |
{
|
|
226 |
ResultStr.Format(_T("设置曝光时间%.1f失败 SetExposureTime [%x]"),m_fExpo,nRet);
|
|
227 |
return CameraError;
|
|
228 |
}
|
|
229 |
}
|
|
230 |
void __stdcall MVSCamera::ImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
|
|
231 |
{
|
|
232 |
MVSCamera *pCam = (MVSCamera *)(pUser);
|
|
233 |
pCam->OnImageCallBackEx(pData, pFrameInfo);
|
|
234 |
}
|
|
235 |
|
|
236 |
int MVSCamera::OnImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo)
|
|
237 |
{
|
|
238 |
BackCallCount++;
|
|
239 |
double time1 = GetTickCountmS();
|
|
240 |
int stride = pFrameInfo->nFrameLen / pFrameInfo->nHeight;
|
|
241 |
if (pFrameInfo->enPixelType==PixelType_Gvsp_YUV422_YUYV_Packed)
|
|
242 |
{
|
|
243 |
Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_YUV422_YUYV_Packed);
|
|
244 |
ResultStr.AppendFormat(_T("(YUV422_YUYV_Packed)"));
|
|
245 |
}else if (pFrameInfo->enPixelType==PixelType_Gvsp_Mono8)
|
|
246 |
{
|
|
247 |
Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_Mono8);
|
|
248 |
ResultStr.AppendFormat(_T("(Mono8)"));
|
|
249 |
}
|
|
250 |
else if (pFrameInfo->enPixelType==PixelType_Gvsp_BayerGR8)
|
|
251 |
{
|
|
252 |
Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_BayerGR8);
|
|
253 |
ResultStr.AppendFormat(_T("(BayerGR8)"));
|
|
254 |
}
|
|
255 |
else if (pFrameInfo->enPixelType==PixelType_Gvsp_BayerGB8)
|
|
256 |
{
|
|
257 |
Image1.CopyFromBuf(pData,pFrameInfo->nWidth,pFrameInfo->nHeight,stride,MyImage::MyPixelType_BayerGB8);
|
|
258 |
ResultStr.AppendFormat(_T("(BayerGB8)"));
|
|
259 |
}
|
|
260 |
double time2 = GetTickCountmS();
|
|
261 |
ResultStr.AppendFormat(_T(" copy %.1fmS "),time2-time1);
|
|
262 |
Grabok=1;
|
|
263 |
return 0;
|
|
264 |
}
|
|
265 |
int MVSCamera::UpdateFps()
|
|
266 |
{
|
|
267 |
int nRet = -1;
|
|
268 |
//图像宽高改变后,最大帧率会随之变化
|
|
269 |
float fFpsMin,fFpsMax;
|
|
270 |
|
|
271 |
MVCC_FLOATVALUE fValue;
|
|
272 |
nRet = MV_CC_GetFrameRate(m_hCam, &fValue);
|
|
273 |
fFpsMin = fValue.fMin;
|
|
274 |
fFpsMax = fValue.fMax;
|
|
275 |
|
|
276 |
//设置到最大帧率
|
|
277 |
nRet = MV_CC_SetFrameRate(m_hCam, fFpsMax);
|
|
278 |
|
|
279 |
return nRet;
|
|
280 |
}
|
|
281 |
|
|
282 |
|
|
283 |
int MVSCamera::StartGrab()
|
|
284 |
{
|
|
285 |
Grabok=0;
|
|
286 |
int nRet = -1;
|
|
287 |
if (m_hCam!=NULL)
|
|
288 |
{
|
|
289 |
nRet = MV_CC_SetCommandValue(m_hCam, "TriggerSoftware");
|
|
290 |
|
|
291 |
if (MV_OK != nRet)
|
|
292 |
{
|
|
293 |
return CameraError;
|
|
294 |
}
|
|
295 |
|
|
296 |
TrigeredCount++;
|
|
297 |
}
|
|
298 |
|
|
299 |
return CameraOk;
|
|
300 |
}
|
|
301 |
int MVSCamera::WaitForGrabFinish(int TimeOutmS)
|
|
302 |
{
|
|
303 |
int i;
|
|
304 |
|
|
305 |
for (i=0;i<TimeOutmS/10;i++)
|
|
306 |
{
|
|
307 |
if (Grabok==1)
|
|
308 |
{
|
|
309 |
break;
|
|
310 |
}
|
|
311 |
Sleep(10);
|
|
312 |
}
|
|
313 |
if (Grabok!=1)
|
|
314 |
{
|
|
315 |
ResultStr.Format(_T("拍照超时"));
|
|
316 |
return CameraError;
|
|
317 |
}
|
|
318 |
return CameraOk;
|
|
319 |
}
|
|
320 |
|
|
321 |
int MVSCamera::FetchImageData()
|
|
322 |
{
|
|
323 |
if (Grabok==1) return CameraOk;
|
|
324 |
else return CameraError;
|
|
325 |
}
|