-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
497 lines (375 loc) · 11.8 KB
/
Camera.cpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
Camera.cpp
Purpose: Login to camera and get stream data from HIKVision Camera
@author Gulraiz Iqbal
@version 1.0 22/10/2015
*/
#include "Camera.h"
uchar* pixels =NULL;
LONG h,w;
Mat result(1080, 1920, CV_8UC3);
LONG lPort;
HWND hWnd=GetConsoleWindow();
//Set Camera Login details
void Camera::setLoginDetails(char * ip, WORD port, char * username, char * password){
this->ip= ip;
this->port=port;
this->username=username;
this->password=password;
}
//Login into camera
void Camera::login(){
//Initialize SDK
NET_DVR_Init();
//Set connection time and reconnection time
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
//Login on given location
lUserID = NET_DVR_Login_V30(this->ip, this->port, this->username, this->password, &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
}
//---------------------------------------
//Set exception callback function
NET_DVR_SetExceptionCallBack_V30(0, NULL,&g_ExceptionCallBack, NULL);
//---------------------------------------
//Start preview and set to callback stream data
NET_DVR_PREVIEWINFO struPlayInfo = {0};
struPlayInfo .hPlayWnd = NULL; //If need to decode, please set it valid. If want to get stream data only, it can be set to NULL
struPlayInfo .lChannel = 1; //Preview channel NO.
struPlayInfo .dwStreamType = 0; //0-main stream, 1-sub stream, 2-stream3, 3-stream4.
struPlayInfo.dwLinkMode = 4; //0-TCP mode, 1-UDP mode, 2-Multi-play mode, 3-RTP mode, 4-RTP/RTSP, 5-RTSP/HTTP
// PlayM4_SetDisplayBuf(lPort,10);
this->lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, &g_RealDataCallBack_V30, 0);
}
//GetRealPlayHandle value return by NET_DVR_RealPlay_V40
LONG Camera::getlRealPlayHandle(){
return lRealPlayHandle;
}
//prepare and start preview of stream data
void Camera::GetPreview(){
while(waitKey(1) != 27){
Convertyv12toBGR();
imshow("video capture",result);
}
}
int Camera::WriteAVIvideo(){
if(!result.empty()){
VideoWriter oVideoWriter ("MyVideo.avi",CV_FOURCC('D','I','V','X'),10,Size(1920,1080),true);
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
int count=0;
while(waitKey(1) != 27){
if(Convertyv12toBGR()){
imshow("video capture",result);
if(count>14)
oVideoWriter.write(result); //writer the frame into the file
}
count++;
}
oVideoWriter.release();
}
return -1;
}
//Get Mat object
int Camera::Convertyv12toBGR(){
if(sizeof(pixels)==4){
yv12ToRgb(pixels,w,h);
return 1;
}
return 0;
}
//Logout from camera
void Camera::logout(){
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V30 error\n");
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
}
//---------------------------------------
//Close preview
NET_DVR_StopRealPlay(lRealPlayHandle);
//Logout
NET_DVR_Logout_V30(lUserID);
NET_DVR_Cleanup();
}
//Convert yv12 colorspace to BGR colorspace
void Camera::yv12ToRgb( uchar *pBuffer, const int w, const int h )
{
#define clip_8_bit(val) \
{ \
if( val < 0 ) \
val = 0; \
else if( val > 255 ) \
val = 255; \
}
long ySize=w*h;
long uSize;
uSize=ySize>>2;
uchar *output=result.data;
uchar *pY=pBuffer;
uchar *pU=pY+ySize;
uchar *pV=pU+uSize;
int y, u, v;
int r, g, b;
int sub_i_uv;
int sub_j_uv;
const int uv_width = w / 2;
const int uv_height = h / 2;
for( int i = 0; i < h; ++i ) {
// calculate u & v rows
sub_i_uv = ((i * uv_height) / h);
for( int j = 0; j < w; ++j ) {
// calculate u & v columns
sub_j_uv = (j * uv_width) / w;
y = pY[(i * w) + j] - 16;
u = pU[(sub_i_uv * uv_width) + sub_j_uv] - 128;
v = pV[(sub_i_uv * uv_width) + sub_j_uv] - 128;
r = (int)((1.1644 * y) + (1.5960 * v));
g = (int)((1.1644 * y) - (0.3918 * u) - (0.8130 * v));
b = (int)((1.1644 * y) + (2.0172 * u));
clip_8_bit( b );
clip_8_bit( g );
clip_8_bit( r );
*output++=r;
*output++=g;
*output++=b;
}
}
}
//Decode compressed data to YV12 format
void CALLBACK Camera::g_DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2)
{
if ( pFrameInfo->nType == T_YV12 )
{
pixels = (uchar*)pBuf;
h=pFrameInfo->nHeight;
w= pFrameInfo->nWidth;
/*
printf("nPort=%d,nSize=%d,pFrameInfo.nWidth=%ld,pFrameInfo.nHeight=%ld,\n pFrameInfo.nStamp=%ld,pFrameInfo.nType=%ld,pFrameInfo.nFrameRate=%ld.\n",
nPort,nSize,pFrameInfo->nWidth,pFrameInfo->nHeight, pFrameInfo->nStamp,pFrameInfo->nType,pFrameInfo->nFrameRate);
*/
}
}
//Get real stream data
void CALLBACK Camera::g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer,DWORD dwBufSize,void* dwUser)
{
switch (dwDataType)
{
case NET_DVR_SYSHEAD: //System head
if (!PlayM4_GetPort(&lPort)) //Get unused port
{
break;
}
//m_iPort = lPort; //The data called back at the first time is system header. Please assign this port to global port, and it will be used to play in next callback
if (dwBufSize > 0)
{
if (!PlayM4_SetStreamOpenMode(lPort, STREAME_REALTIME)) //Set real-time stream playing mode
{
break;
}
if (!PlayM4_OpenStream(lPort, pBuffer, dwBufSize, 1024*1024)) //Open stream
{
break;
}
if(!PlayM4_SetDecCallBack(lPort,g_DecCBFun))
{
break;
}
if (!PlayM4_Play(lPort, hWnd)) //Start play
{
break;
}
}
case NET_DVR_STREAMDATA: //Stream data
if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
}
}
//Callback function to handl exception
void CALLBACK Camera::g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
char tempbuf[256] = {0};
switch(dwType)
{
case EXCEPTION_RECONNECT: //reconnect when preview
printf("----------reconnect--------%d\n", time(NULL));
break;
default:
break;
}
}
bool Camera::Ptz_ZoomIn(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_ZoomIn,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_ZoomIn,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_ZoomIn,0))
{
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_ZoomIn,1);
}
return false;
}
bool Camera::Ptz_ZoomOut(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_ZoomOut,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_ZoomOut,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_ZoomOut,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_ZoomOut,1);
}
return false;
}
bool Camera::Ptz_FOCUS_NEAR(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_FOCUS_NEAR,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_FOCUS_NEAR,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_FOCUS_NEAR,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_FOCUS_NEAR,1);
}
return false;
}
bool Camera::Ptz_FOCUS_FAR(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_FOCUS_FAR,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_FOCUS_FAR,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_FOCUS_FAR,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_FOCUS_FAR,1);
}
return false;
}
bool Camera::Ptz_TILT_UP(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_TILT_UP,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_TILT_UP,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_TILT_UP,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_TILT_UP,1);
}
return false;
}
bool Camera::Ptz_TILT_DOWN(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_TILT_DOWN,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_TILT_DOWN,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_TILT_DOWN,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_TILT_DOWN,1);
}
return false;
}
bool Camera::Ptz_PAN_LEFT(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_PAN_LEFT,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_PAN_LEFT,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_PAN_LEFT,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_PAN_LEFT,1);
}
return false;
}
bool Camera::Ptz_PAN_RIGHT(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_PAN_RIGHT,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_PAN_RIGHT,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_PAN_RIGHT,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_PAN_RIGHT,1);
}
return false;
}
bool Camera::Ptz_UP_LEFT(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_UP_LEFT,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_UP_LEFT,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_UP_LEFT,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_UP_LEFT,1);
}
return false;
}
bool Camera::Ptz_UP_RIGHT(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_UP_RIGHT,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_UP_RIGHT,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_UP_RIGHT,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_UP_RIGHT,1);
}
return false;
}
bool Camera::Ptz_DOWN_LEFT(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_DOWN_LEFT,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_DOWN_LEFT,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_DOWN_LEFT,0)){
Sleep(time);
NET_DVR_PTZControl(this->lRealPlayHandle,this->v_DOWN_LEFT,1);
}
return false;
}
bool Camera::Ptz_DOWN_RIGHT(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_DOWN_RIGHT,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_DOWN_RIGHT,1,7);
}
else if (!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_DOWN_RIGHT,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_DOWN_RIGHT,1);
}
return false;
}
bool Camera::Ptz_PAN_AUTO(int time, bool isFast){
if(isFast && NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_PAN_AUTO,0,7)){
Sleep(time);
return NET_DVR_PTZControlWithSpeed(this->lRealPlayHandle,this->v_PAN_AUTO,1,7);
}
else if(!isFast && NET_DVR_PTZControl(this->lRealPlayHandle,this->v_PAN_AUTO,0)){
Sleep(time);
return NET_DVR_PTZControl(this->lRealPlayHandle,this->v_PAN_AUTO,1);
}
return false;
}
Camera::Camera(void){
//Set PTz commands
this->v_ZoomIn=11;
this->v_ZoomOut=12;
this->v_FOCUS_NEAR=13;
this->v_FOCUS_FAR=14;
this->v_TILT_UP=21;
this->v_TILT_DOWN=22;
this->v_PAN_LEFT=23;
this->v_PAN_RIGHT=24;
this->v_UP_LEFT= 25;
this->v_UP_RIGHT= 26;
this->v_DOWN_LEFT=27;
this->v_DOWN_RIGHT=28;
this->v_PAN_AUTO=29;
}
Camera::~Camera(void)
{
}