提交 | 用户 | age
|
4b03ae
|
1 |
|
Q |
2 |
#include "stdafx.h"
|
|
3 |
#include "MVCamera2.h"
|
|
4 |
#include "../ImageFunc/ImageFunc.h"
|
|
5 |
volatile int MVCamera2::LabInited=0; //是否已经初始化
|
|
6 |
volatile int MVCamera2::TotalCameraCount=0; //相机数量
|
|
7 |
#ifdef _WIN64
|
|
8 |
#pragma comment(lib,"../Mylib/camera/MV_SDK64/mvGige.lib")
|
|
9 |
#else
|
|
10 |
#pragma comment(lib,"../Mylib/camera/MV_SDK/mvGige.lib")
|
|
11 |
#endif
|
|
12 |
#pragma comment(lib,"gdiplus.lib")
|
|
13 |
MVCamera2::MVCamera2(void)
|
|
14 |
{
|
|
15 |
if (0==LabInited++)
|
|
16 |
{
|
|
17 |
MVInitLib();
|
|
18 |
}
|
|
19 |
m_IsCameraOpened=0;
|
|
20 |
Idx=0;
|
|
21 |
TrigeredCount=0;
|
|
22 |
BackCallCount=0;
|
|
23 |
};
|
|
24 |
|
|
25 |
MVCamera2::~MVCamera2(void)
|
|
26 |
{
|
|
27 |
MVStopGrab(m_hCam);
|
|
28 |
if( m_hCam != NULL )
|
|
29 |
{
|
|
30 |
MVCloseCam(m_hCam);
|
|
31 |
m_hCam = NULL;
|
|
32 |
}
|
|
33 |
if (!m_Raw.IsNull()) {m_Raw.Destroy();}
|
|
34 |
if (!m_image.IsNull()) {m_image.Destroy();}
|
|
35 |
|
|
36 |
if (0==--LabInited)
|
|
37 |
{
|
|
38 |
MVTerminateLib();
|
|
39 |
}
|
|
40 |
};
|
|
41 |
|
|
42 |
int MVCamera2::InitLib(void)
|
|
43 |
{
|
|
44 |
CString s1;
|
|
45 |
|
|
46 |
int nCams = 0;
|
|
47 |
MVGetNumOfCameras(&nCams);
|
|
48 |
TotalCameraCount=nCams;
|
|
49 |
if( nCams == 0 )
|
|
50 |
{
|
|
51 |
return 0;
|
|
52 |
}
|
|
53 |
return nCams;
|
|
54 |
}
|
|
55 |
int MVCamera2::GetCameraInfo(int index, MyCameraInfo * pMyCameraInfo)
|
|
56 |
{
|
|
57 |
MVCamInfo info1;
|
|
58 |
MVGetCameraInfo(index,&info1);
|
|
59 |
memcpy(pMyCameraInfo->camIp,info1.mIpAddr,4);
|
|
60 |
memcpy(pMyCameraInfo->camMac,info1.mEthernetAddr,6);
|
|
61 |
memcpy(pMyCameraInfo->mMfgName,info1.mMfgName,32);
|
|
62 |
memcpy(pMyCameraInfo->mModelName,info1.mModelName,32);
|
|
63 |
memcpy(pMyCameraInfo->mSerialNumber,info1.mSerialNumber,16);
|
|
64 |
memcpy(pMyCameraInfo->mUserDefinedName,info1.mUserDefinedName,4);
|
|
65 |
memcpy(pMyCameraInfo->ifIp,info1.m_IfIp,4);
|
|
66 |
memcpy(pMyCameraInfo->ifMac,info1.m_IfMAC,4);
|
|
67 |
CStringA s1A;
|
|
68 |
CString s1;
|
|
69 |
s1A=pMyCameraInfo->mMfgName;
|
|
70 |
AnsiToT(s1A,s1);
|
|
71 |
pMyCameraInfo->sMfgName=s1;
|
|
72 |
s1A=pMyCameraInfo->mModelName;
|
|
73 |
AnsiToT(s1A,s1);
|
|
74 |
pMyCameraInfo->sModelName=s1;
|
|
75 |
s1A=pMyCameraInfo->mSerialNumber;
|
|
76 |
AnsiToT(s1A,s1);
|
|
77 |
pMyCameraInfo->sSerialNumber=s1;
|
|
78 |
s1A=pMyCameraInfo->mUserDefinedName;
|
|
79 |
AnsiToT(s1A,s1);
|
|
80 |
pMyCameraInfo->sUserDefinedName=s1;
|
|
81 |
|
|
82 |
return CameraOk;
|
|
83 |
}
|
|
84 |
int MVCamera2::OpenCamera(int Index)
|
|
85 |
{
|
|
86 |
Idx=Index;
|
|
87 |
MVSTATUS_CODES r = MVOpenCamByIndex(Idx,&m_hCam);
|
|
88 |
if( m_hCam == NULL )
|
|
89 |
{
|
|
90 |
if( r == MVST_ACCESS_DENIED )
|
|
91 |
{
|
|
92 |
ResultStr=_T("无法打开MV相机,可能正被别的软件控制");
|
|
93 |
return CameraError;
|
|
94 |
}
|
|
95 |
else
|
|
96 |
{
|
|
97 |
ResultStr.Format(_T("无法打开MV相机%p,发生错误 %d"),this,r);
|
|
98 |
return CameraError;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
m_IsCameraOpened=1;
|
|
102 |
MVGetWidth(m_hCam,&m_nWidth);
|
|
103 |
MVGetHeight(m_hCam,&m_nHeight);
|
|
104 |
|
|
105 |
MV_PixelFormatEnums format;
|
|
106 |
MVGetPixelFormat(m_hCam,&format);
|
|
107 |
m_PixelFormat = format;
|
|
108 |
|
|
109 |
MVSetTriggerMode(m_hCam, ::TriggerMode_On);
|
|
110 |
MVSetTriggerSource(m_hCam, ::TriggerSource_Software);
|
|
111 |
MVSetStrobeSource(m_hCam,::LineSource_ExposureActive);
|
|
112 |
MVSetStrobeInvert(m_hCam,TRUE);
|
|
113 |
UpdateFps();
|
|
114 |
UpdateImage();
|
|
115 |
#ifdef _WIN64
|
|
116 |
MVStartGrab(m_hCam,StreamCB,(long long)this); //设置拍照回调函数
|
|
117 |
#else
|
|
118 |
MVStartGrab(m_hCam,StreamCB,(long)this); //设置拍照回调函数
|
|
119 |
#endif
|
|
120 |
ResultStr.Format(_T("打开MV相机%d[%p]成功"),Index,this);
|
|
121 |
return CameraOk;
|
|
122 |
}
|
|
123 |
|
|
124 |
int MVCamera2::CloseCamera()
|
|
125 |
{
|
|
126 |
return MVCloseCam(m_hCam);
|
|
127 |
}
|
|
128 |
int MVCamera2::GetExposureTime(double *ExposureTime)
|
|
129 |
{
|
|
130 |
MVGetExposureTime(m_hCam,ExposureTime);
|
|
131 |
return CameraOk;
|
|
132 |
}
|
|
133 |
int MVCamera2::SetExposureTime(double ExposeTime)
|
|
134 |
{
|
|
135 |
m_fExpo=ExposeTime;
|
|
136 |
if (!m_IsCameraOpened)
|
|
137 |
{
|
|
138 |
ResultStr.Format(_T("相机未打开"));
|
|
139 |
return CameraError;
|
|
140 |
}
|
|
141 |
int Res=MVSetExposureTime(m_hCam,m_fExpo*1000.0);
|
|
142 |
if (Res==MVSTATUS_CODES::MVST_SUCCESS)
|
|
143 |
{
|
|
144 |
ResultStr.Format(_T("设置曝光时间%.1f成功"),m_fExpo);
|
|
145 |
return CameraOk;
|
|
146 |
}else
|
|
147 |
{
|
|
148 |
ResultStr.Format(_T("设置曝光时间%.1f失败 Err %d "),m_fExpo,Res);
|
|
149 |
return CameraError;
|
|
150 |
}
|
|
151 |
}
|
|
152 |
#ifdef _WIN64
|
|
153 |
int MVCamera2::StreamCB(MV_IMAGE_INFO *pInfo,long long nUserVal)
|
|
154 |
{
|
|
155 |
MVCamera2 *pDlg = (MVCamera2 *)(nUserVal|0x100000000);
|
|
156 |
return ( pDlg->OnStreamCB(pInfo) );
|
|
157 |
}
|
|
158 |
#else
|
|
159 |
int MVCamera2::StreamCB(MV_IMAGE_INFO *pInfo,long nUserVal)
|
|
160 |
{
|
|
161 |
MVCamera2 *pDlg = (MVCamera2 *)(nUserVal);
|
|
162 |
return ( pDlg->OnStreamCB(pInfo) );
|
|
163 |
}
|
|
164 |
#endif
|
|
165 |
|
|
166 |
int MVCamera2::OnStreamCB( MV_IMAGE_INFO *pInfo)
|
|
167 |
{
|
|
168 |
CString str;
|
|
169 |
BackCallCount++;
|
|
170 |
|
|
171 |
int w = pInfo->nSizeX;
|
|
172 |
int h = pInfo->nSizeY;
|
|
173 |
int pixeltype=pInfo->nPixelType;
|
|
174 |
ResultStr.AppendFormat(_T("X %d Y %d Type %X P %p "),w,h,pixeltype,pInfo->pImageBuffer);
|
|
175 |
|
|
176 |
if( m_PixelFormat == ::PixelFormat_Mono8 )
|
|
177 |
{
|
|
178 |
Image1.CopyFromBuf(pInfo->pImageBuffer,w,h,w,MyImage::MyPixelType_Mono8);
|
|
179 |
ResultStr.AppendFormat(_T("Mono8 \r\n"));
|
|
180 |
}
|
|
181 |
else
|
|
182 |
{
|
|
183 |
Image1.CopyFromBuf(pInfo->pImageBuffer,w,h,w,MyImage::MyPixelType_BayerGR8);
|
|
184 |
ResultStr.AppendFormat(_T("BayerGR8 \r\n"));
|
|
185 |
}
|
|
186 |
Grabok=1;
|
|
187 |
return 0;
|
|
188 |
}
|
|
189 |
int MVCamera2::UpdateFps()
|
|
190 |
{
|
|
191 |
//图像宽高改变后,最大帧率会随之变化
|
|
192 |
double fFpsMin,fFpsMax;
|
|
193 |
MVGetFrameRateRange(m_hCam,&fFpsMin,&fFpsMax);
|
|
194 |
|
|
195 |
//设置到最大帧率
|
|
196 |
return MVSetFrameRate(m_hCam,fFpsMax);
|
|
197 |
}
|
|
198 |
|
|
199 |
int MVCamera2::UpdateImage()
|
|
200 |
{
|
|
201 |
if (!m_Raw.IsNull()) {m_Raw.Destroy();}
|
|
202 |
if (!m_image.IsNull()) {m_image.Destroy(); }
|
|
203 |
|
|
204 |
if( m_PixelFormat == ::PixelFormat_Mono8 )
|
|
205 |
{
|
|
206 |
m_Raw.Create(m_nWidth,m_nHeight,8);
|
|
207 |
m_image.Create(m_nWidth,m_nHeight,8);
|
|
208 |
}
|
|
209 |
else
|
|
210 |
{
|
|
211 |
m_Raw.Create(m_nWidth,m_nHeight,8);
|
|
212 |
m_image.Create(m_nWidth,m_nHeight,24);
|
|
213 |
}
|
|
214 |
return 0;
|
|
215 |
}
|
|
216 |
|
|
217 |
int MVCamera2::StartGrab()
|
|
218 |
{
|
|
219 |
Grabok=0;
|
|
220 |
int j;
|
|
221 |
j=-1;
|
|
222 |
if (m_hCam!=NULL)
|
|
223 |
{
|
|
224 |
j=MVTriggerSoftware(m_hCam);
|
|
225 |
|
|
226 |
TrigeredCount++;
|
|
227 |
}
|
|
228 |
if (j!=MVST_SUCCESS) {return CameraError;}
|
|
229 |
return CameraOk;
|
|
230 |
}
|
|
231 |
int MVCamera2::WaitForGrabFinish(int TimeOutmS)
|
|
232 |
{
|
|
233 |
int i;
|
|
234 |
|
|
235 |
for (i=0;i<TimeOutmS/10;i++)
|
|
236 |
{
|
|
237 |
if (Grabok==1)
|
|
238 |
{
|
|
239 |
break;
|
|
240 |
}
|
|
241 |
Sleep(10);
|
|
242 |
}
|
|
243 |
if (Grabok!=1)
|
|
244 |
{
|
|
245 |
ResultStr.Format(_T("拍照超时"));
|
|
246 |
return CameraError;
|
|
247 |
}
|
|
248 |
return CameraOk;
|
|
249 |
}
|
|
250 |
|
|
251 |
int MVCamera2::FetchImageData()
|
|
252 |
{
|
|
253 |
if (Grabok==1) return CameraOk;
|
|
254 |
else return CameraError;
|
|
255 |
} |