/
LiveFrameSample.cpp
220 lines (197 loc) · 5.13 KB
/
LiveFrameSample.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libqhy/qhyccd.h>
#include <sys/time.h>
//#define OPENCV_SUPPORT
#ifdef OPENCV_SUPPORT
#include <opencv/cv.h>
#include <opencv/highgui.h>
#endif
int main(int argc,char *argv[])
{
int num = 0;
qhyccd_handle *camhandle;
int ret;
char id[32];
char camtype[16];
int found = 0;
unsigned int w,h,bpp,channels;
unsigned char *ImgData;
int camtime = 10000,camgain = 0,camspeed = 2,cambinx = 1,cambiny = 1;
ret = InitQHYCCDResource();
if(ret == QHYCCD_SUCCESS)
{
printf("Init SDK success!\n");
}
else
{
goto failure;
}
num = ScanQHYCCD();
if(num > 0)
{
printf("Yes!Found QHYCCD,the num is %d \n",num);
}
else
{
printf("Not Found QHYCCD,please check the usblink or the power\n");
goto failure;
}
for(int i = 0;i < num;i++)
{
ret = GetQHYCCDId(i,id);
if(ret == QHYCCD_SUCCESS)
{
printf("connected to the first camera from the list,id is %s\n",id);
found = 1;
break;
}
}
if(found == 1)
{
camhandle = OpenQHYCCD(id);
if(camhandle != NULL)
{
printf("Open QHYCCD success!\n");
}
else
{
printf("Open QHYCCD failed \n");
goto failure;
}
ret = SetQHYCCDStreamMode(camhandle,1);
ret = InitQHYCCD(camhandle);
if(ret == QHYCCD_SUCCESS)
{
printf("Init QHYCCD success!\n");
}
else
{
printf("Init QHYCCD fail code:%d\n",ret);
goto failure;
}
double chipw,chiph,pixelw,pixelh;
ret = GetQHYCCDChipInfo(camhandle,&chipw,&chiph,&w,&h,&pixelw,&pixelh,&bpp);
if(ret == QHYCCD_SUCCESS)
{
printf("GetQHYCCDChipInfo success!\n");
printf("CCD/CMOS chip information:\n");
printf("Chip width %3f mm,Chip height %3f mm\n",chipw,chiph);
printf("Chip pixel width %3f um,Chip pixel height %3f um\n",pixelw,pixelh);
printf("Chip Max Resolution is %d x %d,depth is %d\n",w,h,bpp);
}
else
{
printf("GetQHYCCDChipInfo fail\n");
goto failure;
}
ret = IsQHYCCDControlAvailable(camhandle,CONTROL_TRANSFERBIT);
if(ret == QHYCCD_SUCCESS)
{
ret = SetQHYCCDBitsMode(camhandle,8);
if(ret != QHYCCD_SUCCESS)
{
printf("SetQHYCCDParam CONTROL_GAIN failed\n");
getchar();
return 1;
}
}
ret = SetQHYCCDResolution(camhandle,0,0,w,h);
if(ret == QHYCCD_SUCCESS)
{
printf("SetQHYCCDResolution success!\n");
}
else
{
printf("SetQHYCCDResolution fail\n");
goto failure;
}
ret = BeginQHYCCDLive(camhandle);
if(ret == QHYCCD_SUCCESS)
{
printf("BeginQHYCCDLive success!\n");
}
else
{
printf("BeginQHYCCDLive failed\n");
goto failure;
}
int length = GetQHYCCDMemLength(camhandle);
if(length > 0)
{
ImgData = (unsigned char *)malloc(length);
memset(ImgData,0,length);
}
else
{
printf("Get the min memory space length failure \n");
goto failure;
}
int t_start,t_end;
t_start = time(NULL);
int fps = 0;
#ifdef OPENCV_SUPPORT
IplImage *img = NULL;
cvNamedWindow("show",0);
#endif
ret = QHYCCD_ERROR;
while(ret != QHYCCD_SUCCESS)
{
ret = GetQHYCCDLiveFrame(camhandle,&w,&h,&bpp,&channels,ImgData);
if(ret == QHYCCD_SUCCESS)
{
#ifdef OPENCV_SUPPORT
if(img == NULL)
{
img = cvCreateImageHeader(cvSize(w,h),bpp,1);
img->imageData = (char *)ImgData;
}
cvShowImage("show",img);
cvWaitKey(30);
#endif
fps++;
t_end = time(NULL);
if(t_end - t_start >= 5)
{
printf("fps = %d\n",fps / 5);
fps = 0;
t_start = time(NULL);
}
}
}
delete(ImgData);
}
else
{
printf("The camera is not QHYCCD or other error \n");
goto failure;
}
if(camhandle)
{
StopQHYCCDLive(camhandle);
ret = CloseQHYCCD(camhandle);
if(ret == QHYCCD_SUCCESS)
{
printf("Close QHYCCD success!\n");
}
else
{
goto failure;
}
}
ret = ReleaseQHYCCDResource();
if(ret == QHYCCD_SUCCESS)
{
printf("Rlease SDK Resource success!\n");
}
else
{
goto failure;
}
return 0;
failure:
printf("some fatal error happened\n");
return 1;
}