QuakeGod
2021-07-29 3b04f942bd51c0453cbb64167cbdb7de69159bd5
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
 
#include "stdafx.h"
#include "MVCamera2.h"
#include "../ImageFunc/ImageFunc.h"
volatile int MVCamera2::LabInited=0; //ÊÇ·ñÒѾ­³õʼ»¯
volatile int MVCamera2::TotalCameraCount=0;  //Ïà»úÊýÁ¿
#ifdef _WIN64
#pragma comment(lib,"../Mylib/camera/MV_SDK64/mvGige.lib")
#else
#pragma comment(lib,"../Mylib/camera/MV_SDK/mvGige.lib")
#endif
#pragma comment(lib,"gdiplus.lib")
MVCamera2::MVCamera2(void)
{
    if (0==LabInited++)
    {
        MVInitLib();
    }
    m_IsCameraOpened=0;
    Idx=0;
    TrigeredCount=0;
    BackCallCount=0;
};
 
MVCamera2::~MVCamera2(void)
{
    MVStopGrab(m_hCam);
    if( m_hCam != NULL )
    {
        MVCloseCam(m_hCam);
        m_hCam = NULL;
    }
    if (!m_Raw.IsNull()) {m_Raw.Destroy();}
    if (!m_image.IsNull()) {m_image.Destroy();}
 
    if (0==--LabInited)
    {
        MVTerminateLib();
    }
};
 
int MVCamera2::InitLib(void)
{
    CString s1;
 
    int nCams = 0;
    MVGetNumOfCameras(&nCams);
    TotalCameraCount=nCams;
    if( nCams == 0 )
    {
        return 0;
    }
    return nCams;
}
int MVCamera2::GetCameraInfo(int index,    MyCameraInfo * pMyCameraInfo)
{
    MVCamInfo info1;
    MVGetCameraInfo(index,&info1);
    memcpy(pMyCameraInfo->camIp,info1.mIpAddr,4);
    memcpy(pMyCameraInfo->camMac,info1.mEthernetAddr,6);
    memcpy(pMyCameraInfo->mMfgName,info1.mMfgName,32);
    memcpy(pMyCameraInfo->mModelName,info1.mModelName,32);
    memcpy(pMyCameraInfo->mSerialNumber,info1.mSerialNumber,16);
    memcpy(pMyCameraInfo->mUserDefinedName,info1.mUserDefinedName,4);
    memcpy(pMyCameraInfo->ifIp,info1.m_IfIp,4);
    memcpy(pMyCameraInfo->ifMac,info1.m_IfMAC,4);
    CStringA s1A;
    CString s1;
    s1A=pMyCameraInfo->mMfgName;
    AnsiToT(s1A,s1);
    pMyCameraInfo->sMfgName=s1;
    s1A=pMyCameraInfo->mModelName;
    AnsiToT(s1A,s1);
    pMyCameraInfo->sModelName=s1;
    s1A=pMyCameraInfo->mSerialNumber;
    AnsiToT(s1A,s1);
    pMyCameraInfo->sSerialNumber=s1;
    s1A=pMyCameraInfo->mUserDefinedName;
    AnsiToT(s1A,s1);
    pMyCameraInfo->sUserDefinedName=s1;
 
    return CameraOk;
}
int MVCamera2::OpenCamera(int Index)
{
    Idx=Index;
    MVSTATUS_CODES r = MVOpenCamByIndex(Idx,&m_hCam);
    if( m_hCam == NULL )
    {
        if( r == MVST_ACCESS_DENIED )
        {
            ResultStr=_T("ÎÞ·¨´ò¿ªMVÏà»ú£¬¿ÉÄÜÕý±»±ðµÄÈí¼þ¿ØÖÆ");
            return CameraError;
        }
        else
        {
            ResultStr.Format(_T("ÎÞ·¨´ò¿ªMVÏà»ú%p£¬·¢Éú´íÎó %d"),this,r);
            return CameraError;
        }
    }
    m_IsCameraOpened=1;
    MVGetWidth(m_hCam,&m_nWidth);
    MVGetHeight(m_hCam,&m_nHeight);
 
    MV_PixelFormatEnums format;
    MVGetPixelFormat(m_hCam,&format);
    m_PixelFormat = format;
 
    MVSetTriggerMode(m_hCam, ::TriggerMode_On);
    MVSetTriggerSource(m_hCam, ::TriggerSource_Software);
    MVSetStrobeSource(m_hCam,::LineSource_ExposureActive);
    MVSetStrobeInvert(m_hCam,TRUE);
    UpdateFps();
    UpdateImage();
#ifdef _WIN64
    MVStartGrab(m_hCam,StreamCB,(long long)this);        //ÉèÖÃÅÄÕջص÷º¯Êý
#else    
    MVStartGrab(m_hCam,StreamCB,(long)this);        //ÉèÖÃÅÄÕջص÷º¯Êý
#endif
    ResultStr.Format(_T("´ò¿ªMVÏà»ú%d[%p]³É¹¦"),Index,this);
    return CameraOk;
}
 
int MVCamera2::CloseCamera()
{
    return MVCloseCam(m_hCam);
}
int MVCamera2::GetExposureTime(double *ExposureTime)
{
    MVGetExposureTime(m_hCam,ExposureTime);
    return CameraOk;
}
int MVCamera2::SetExposureTime(double ExposeTime)
{
    m_fExpo=ExposeTime;
    if (!m_IsCameraOpened) 
    {
        ResultStr.Format(_T("Ïà»úδ´ò¿ª"));
        return CameraError;
    }
    int Res=MVSetExposureTime(m_hCam,m_fExpo*1000.0);
    if (Res==MVSTATUS_CODES::MVST_SUCCESS)
    {
        ResultStr.Format(_T("ÉèÖÃÆعâʱ¼ä%.1f³É¹¦"),m_fExpo);
        return CameraOk;
    }else
    {
        ResultStr.Format(_T("ÉèÖÃÆعâʱ¼ä%.1fʧ°Ü Err %d "),m_fExpo,Res);
        return CameraError;
    }
}
#ifdef _WIN64
int MVCamera2::StreamCB(MV_IMAGE_INFO *pInfo,long long nUserVal)
{
    MVCamera2 *pDlg = (MVCamera2 *)(nUserVal|0x100000000);
    return ( pDlg->OnStreamCB(pInfo) );
}
#else
int MVCamera2::StreamCB(MV_IMAGE_INFO *pInfo,long nUserVal)
{
    MVCamera2 *pDlg = (MVCamera2 *)(nUserVal);
    return ( pDlg->OnStreamCB(pInfo) );
}
#endif
 
int MVCamera2::OnStreamCB( MV_IMAGE_INFO *pInfo)
{
    CString str;
    BackCallCount++;
 
    int w = pInfo->nSizeX;
    int h = pInfo->nSizeY;
    int pixeltype=pInfo->nPixelType;
    ResultStr.AppendFormat(_T("X %d Y %d Type %X P %p "),w,h,pixeltype,pInfo->pImageBuffer);
 
    if( m_PixelFormat == ::PixelFormat_Mono8 )
    {
        Image1.CopyFromBuf(pInfo->pImageBuffer,w,h,w,MyImage::MyPixelType_Mono8);
        ResultStr.AppendFormat(_T("Mono8 \r\n"));
    }
    else
    {
        Image1.CopyFromBuf(pInfo->pImageBuffer,w,h,w,MyImage::MyPixelType_BayerGR8);
        ResultStr.AppendFormat(_T("BayerGR8 \r\n"));
    }
    Grabok=1;
    return 0;
}
int MVCamera2::UpdateFps()
{
    //ͼÏñ¿í¸ß¸Ä±äºó£¬×î´óÖ¡ÂÊ»áËæÖ®±ä»¯
    double fFpsMin,fFpsMax;
    MVGetFrameRateRange(m_hCam,&fFpsMin,&fFpsMax);
 
    //ÉèÖõ½×î´óÖ¡ÂÊ
    return MVSetFrameRate(m_hCam,fFpsMax);
}
 
int MVCamera2::UpdateImage()
{
    if (!m_Raw.IsNull()) {m_Raw.Destroy();}
    if (!m_image.IsNull())    {m_image.Destroy();    }
 
    if( m_PixelFormat == ::PixelFormat_Mono8 )
    {
        m_Raw.Create(m_nWidth,m_nHeight,8);
        m_image.Create(m_nWidth,m_nHeight,8);
    }
    else
    {
        m_Raw.Create(m_nWidth,m_nHeight,8);
        m_image.Create(m_nWidth,m_nHeight,24);
    }
    return 0;
}
 
int MVCamera2::StartGrab()
{
    Grabok=0;
    int j;
    j=-1;
    if (m_hCam!=NULL)
    {
        j=MVTriggerSoftware(m_hCam);
 
        TrigeredCount++;
    }
    if (j!=MVST_SUCCESS) {return CameraError;}
    return CameraOk;
}
int MVCamera2::WaitForGrabFinish(int TimeOutmS)
{
    int i;
 
    for (i=0;i<TimeOutmS/10;i++)
    {
        if (Grabok==1)
        {
            break;
        }
        Sleep(10);
    }
    if (Grabok!=1)
    {
        ResultStr.Format(_T("ÅÄÕÕ³¬Ê±"));
        return CameraError;
    }
    return CameraOk;
}
 
int MVCamera2::FetchImageData()
{
    if (Grabok==1)    return CameraOk;
    else return CameraError;
}