forked from openpnp/openpnp-capture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
332 lines (292 loc) · 9.01 KB
/
main.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
/*
openpnp test application
Niels Moseley
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include "openpnp-capture.h"
#include "../common/context.h"
#include "../uvcctrl.h"
bool writeBufferAsPPM(uint32_t frameNum, uint32_t width, uint32_t height, const uint8_t *bufferPtr, size_t bytes)
{
char fname[100];
sprintf(fname, "frame_%d.ppm",frameNum);
FILE *fout = fopen(fname, "wb");
if (fout == 0)
{
fprintf(stderr, "Cannot open %s for writing\n", fname);
return false;
}
fprintf(fout, "P6 %d %d 255\n", width, height); // PGM header
fwrite(bufferPtr, 1, bytes, fout);
fclose(fout);
return true;
}
int main(int argc, char*argv[])
{
uint32_t deviceFormatID = 0;
uint32_t deviceID = 0;
printf("==============================\n");
printf(" OpenPNP Capture Test Program\n");
printf(" %s\n", Cap_getLibraryVersion());
printf("==============================\n");
Cap_setLogLevel(8);
if (argc == 1)
{
printf("Usage: openpnp-capture-test <camera ID> <frame format ID>\n");
printf("\n..continuing with default camera parameters.\n\n");
}
if (argc >= 2)
{
deviceID = atoi(argv[1]);
}
if (argc >= 3)
{
deviceFormatID = atoi(argv[2]);
}
CapContext ctx = Cap_createContext();
#if 0
uint32_t deviceCount = Cap_getDeviceCount(ctx);
printf("Number of devices: %d\n", deviceCount);
for(uint32_t deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++)
{
printf("Device [%d/%d] Name: %s\n", deviceIndex, deviceCount, Cap_getDeviceName(ctx, deviceIndex));
printf("Unique ID: %s\n", Cap_getDeviceUniqueID(ctx, deviceIndex));
uint32_t formatCount = Cap_getNumFormats(ctx, deviceIndex);
printf(" Number of formats: %d\n", formatCount);
for (uint32_t formatIndex = 0; formatIndex < formatCount; formatIndex++) {
CapFormatInfo format;
Cap_getFormatInfo(ctx, deviceIndex, formatIndex, &format);
std::string fourcc;
for (int i = 3; i >= 0; i--) {
fourcc += (char) ((format.fourcc >> (8 * i)) & 0xff);
}
printf(" Format [%d/%d] %s %dx%d @ %d FPS\n", formatIndex, formatCount, fourcc.c_str(), format.width, format.height, format.fps);
}
}
#else
uint32_t deviceCount = Cap_getDeviceCount(ctx);
printf("Number of devices: %d\n", deviceCount);
for(uint32_t i=0; i<deviceCount; i++)
{
printf("ID %d -> %s\n", i, Cap_getDeviceName(ctx,i));
printf("Unique: %s\n", Cap_getDeviceUniqueID(ctx,i));
// show all supported frame buffer formats
int32_t nFormats = Cap_getNumFormats(ctx, i);
printf(" Number of formats: %d\n", nFormats);
std::string fourccString;
for(int32_t j=0; j<nFormats; j++)
{
CapFormatInfo finfo;
Cap_getFormatInfo(ctx, i, j, &finfo);
//fourccString = FourCCToString(finfo.fourcc);
std::string fourcc;
for (int i = 3; i >= 0; i--) {
fourcc += (char) ((finfo.fourcc >> (8 * i)) & 0xff);
}
printf(" Format ID %d: %d x %d pixels %d FPS(max) FOURCC=%s\n",
j, finfo.width, finfo.height, finfo.fps, fourcc.c_str());
}
}
#endif
// get current stream parameters
CapFormatInfo finfo;
Cap_getFormatInfo(ctx, deviceID, deviceFormatID, &finfo);
#if 0
UVCCtrl *ctrl = UVCCtrl::create(0x05AC, 0x8502);
if (ctrl != nullptr)
{
printf("Created UVC control interface!\n");
bool state;
if (ctrl->getAutoProperty(CAPPROPID_WHITEBALANCE, state))
{
printf("Auto white balance is: %s\n", state ? "ON" : " OFF");
}
else
{
printf("Cannot get white balance state..\n");
}
int32_t value, emin, emax;
if (ctrl->getProperty(CAPPROPID_EXPOSURE, &value))
{
printf("Exposure is: %d\n", value);
}
else
{
printf("Cannot get exposure..\n");
}
if (ctrl->getPropertyLimits(CAPPROPID_EXPOSURE, &emin, &emax))
{
printf("Exposure limits are: %d .. %d\n", emin, emax);
}
else
{
printf("Cannot get exposure limits..\n");
}
delete ctrl;
}
else
{
printf("Failed to create UVC control interface\n");
}
#endif
int32_t streamID = Cap_openStream(ctx, deviceID, deviceFormatID);
printf("Stream ID = %d\n", streamID);
if (Cap_isOpenStream(ctx, streamID) == 1)
{
printf("Stream is open\n");
}
else
{
printf("Stream is closed (?)\n");
return 1;
}
int32_t emin,emax,edefault;
if (Cap_getPropertyLimits(ctx, streamID, CAPPROPID_EXPOSURE, &emin, &emax, &edefault) == CAPRESULT_OK)
{
printf("Exposure limits: %d .. %d (default=%d)\n", emin, emax, edefault);
}
else
{
printf("Failed to get exposure limits!\n");
}
if (Cap_getProperty(ctx, streamID, CAPPROPID_EXPOSURE, &emin) == CAPRESULT_OK)
{
printf("Exposure: %d\n", emin);
}
else
{
printf("Failed to get exposure!\n");
}
uint32_t vvv;
if (Cap_getAutoProperty(ctx, streamID, CAPPROPID_EXPOSURE, &vvv) == CAPRESULT_OK)
{
printf("Auto exposure: %d\n", vvv);
}
else
{
printf("Failed to get auto exposure!\n");
}
//disable auto exposure, focus and white balance
//Cap_setAutoProperty(ctx, streamID, CAPPROPID_EXPOSURE, 0);
//Cap_setAutoProperty(ctx, streamID, CAPPROPID_FOCUS, 0);
//Cap_setAutoProperty(ctx, streamID, CAPPROPID_WHITEBALANCE, 0);
//Cap_setAutoProperty(ctx, streamID, CAPPROPID_GAIN, 0);
std::vector<uint8_t> m_buffer;
m_buffer.resize(finfo.width*finfo.height*3);
uint32_t counter = 0;
while(counter < 30)
{
if (Cap_hasNewFrame(ctx, streamID) == 1)
{
Cap_captureFrame(ctx, streamID, &m_buffer[0], m_buffer.size());
writeBufferAsPPM(counter, finfo.width, finfo.height, &m_buffer[0], m_buffer.size());
counter++;
printf("Captured frames: %d\r", counter);
fflush(stdout);
}
};
printf("\n\n");
Cap_closeStream(ctx, streamID);
//
// printf("Press Q to exit..\n");
//
// std::vector<uint8_t> m_buffer;
// m_buffer.resize(640*480*3);
//
// Cap_setAutoExposure(ctx, streamID, 1);
//
//#if 1
// uint32_t counter = 0;
// uint32_t tries = 0;
// while(counter < 30)
// {
// usleep(50000);
// printf("%d", Cap_getStreamFrameCount(ctx, streamID));
// if (Cap_hasNewFrame(ctx, streamID) == 1)
// {
// Cap_captureFrame(ctx, streamID, &m_buffer[0], m_buffer.size());
// counter++;
// }
// tries++;
// if (tries == 1000)
// {
// break;
// }
// };
//#endif
//
// Cap_setAutoExposure(ctx, streamID, 0);
//
// // wait for a new frame ..
// //while (Cap_hasNewFrame(ctx, streamID) == 0) {};
//
// if (Cap_captureFrame(ctx, streamID, &m_buffer[0], m_buffer.size()) == CAPRESULT_OK)
// {
// printf("Buffer captured!\n");
//
// FILE *fout = fopen("image.ppm", "wb");
// fprintf(fout, "P6 640 480 255\n"); // PGM header
//
// const uint32_t height = 480;
// const uint32_t width = 640;
//
// // exchange BGR to RGB
// uint32_t idx = 0;
// for(uint32_t i=0; i<width*height; i++)
// {
// uint8_t b = m_buffer[idx];
// uint8_t g = m_buffer[idx+1];
// uint8_t r = m_buffer[idx+2];
// m_buffer[idx++] = r;
// m_buffer[idx++] = g;
// m_buffer[idx++] = b;
// }
//
// // and upside-down :)
// const uint32_t stride = 3;
// const size_t lineBytes = width * stride;
// uint8_t *row = new uint8_t[lineBytes];
// uint8_t *low = &m_buffer[0];
// uint8_t *high = &m_buffer[(height - 1) * lineBytes];
//
// for (; low < high; low += lineBytes, high -= lineBytes) {
// memcpy(row, low, lineBytes);
// memcpy(low, high, lineBytes);
// memcpy(high, row, lineBytes);
// }
// delete[] row;
//
// fwrite(&m_buffer[0], 1, m_buffer.size(), fout);
// fclose(fout);
// }
//
// char c = 0;
// int32_t v = 0;
// while((c != 'q') && (c != 'Q'))
// {
// c = getchar();
// switch(c)
// {
// case '+':
// printf("+");
// Cap_setExposure(ctx, streamID, ++v);
// break;
// case '-':
// printf("-");
// Cap_setExposure(ctx, streamID, --v);
// break;
// case '0':
// printf("0");
// v = 0;
// Cap_setExposure(ctx, streamID, v);
// break;
// }
// }
//
// Cap_closeStream(ctx, streamID);
CapResult result = Cap_releaseContext(ctx);
return 0;
}