forked from ZBar/ZBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_video.c
175 lines (152 loc) · 5.21 KB
/
test_video.c
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
/*------------------------------------------------------------------------
* Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
*
* This file is part of the ZBar Bar Code Reader.
*
* The ZBar Bar Code Reader is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* The ZBar Bar Code Reader is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with the ZBar Bar Code Reader; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* http://sourceforge.net/projects/zbar
*------------------------------------------------------------------------*/
#include <config.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <zbar.h>
#include "test_images.h"
#ifdef _WIN32
struct timespec {
time_t tv_sec;
long tv_nsec;
};
#endif
zbar_video_t *video;
int main (int argc, char *argv[])
{
zbar_set_verbosity(31);
const char *dev = "";
uint32_t vidfmt = 0;
if(argc > 1) {
dev = argv[1];
if(argc > 2) {
int n = strlen(argv[2]);
if(n > 4)
n = 4;
memcpy((char*)&vidfmt, argv[2], n);
}
}
if(!vidfmt)
vidfmt = fourcc('B','G','R','3');
video = zbar_video_create();
if(!video) {
fprintf(stderr, "unable to allocate memory?!\n");
return(1);
}
zbar_video_request_size(video, 640, 480);
if(zbar_video_open(video, dev)) {
zbar_video_error_spew(video, 0);
fprintf(stderr,
"ERROR: unable to access your video device\n"
"this program requires video capture support using"
" v4l version 1 or 2 or VfW\n"
" - is your video device located at \"%s\"?\n"
" - is your video driver installed? (check dmesg)\n"
" - make sure you have the latest drivers\n"
" - do you have read/write permission to access it?\n"
" - is another application is using it?\n"
" - does the device support video capture?\n"
" - does the device work with other programs?\n",
dev);
return(1);
}
fprintf(stderr, "opened video device: %s (fd=%d)\n",
dev, zbar_video_get_fd(video));
fflush(stderr);
if(zbar_video_init(video, vidfmt)) {
fprintf(stderr, "ERROR: failed to set format: %.4s(%08x)\n",
(char*)&vidfmt, vidfmt);
return(zbar_video_error_spew(video, 0));
}
if(zbar_video_enable(video, 1)) {
fprintf(stderr, "ERROR: starting video stream\n");
return(zbar_video_error_spew(video, 0));
}
fprintf(stderr, "started video stream...\n");
fflush(stderr);
zbar_image_t *image = zbar_video_next_image(video);
if(!image) {
fprintf(stderr, "ERROR: unable to capture image\n");
return(zbar_video_error_spew(video, 0));
}
uint32_t format = zbar_image_get_format(image);
unsigned width = zbar_image_get_width(image);
unsigned height = zbar_image_get_height(image);
const uint8_t *data = zbar_image_get_data(image);
fprintf(stderr, "captured image: %d x %d %.4s @%p\n",
width, height, (char*)&format, data);
fflush(stderr);
zbar_image_destroy(image);
fprintf(stderr, "\nstreaming 100 frames...\n");
fflush(stderr);
struct timespec start, end;
#if _POSIX_TIMERS > 0
clock_gettime(CLOCK_REALTIME, &start);
#else
struct timeval ustime;
gettimeofday(&ustime, NULL);
start.tv_nsec = ustime.tv_usec * 1000;
start.tv_sec = ustime.tv_sec;
#endif
int i;
for(i = 0; i < 100; i++) {
zbar_image_t *image = zbar_video_next_image(video);
if(!image) {
fprintf(stderr, "ERROR: unable to capture image\n");
return(zbar_video_error_spew(video, 0));
}
zbar_image_destroy(image);
}
#if _POSIX_TIMERS > 0
clock_gettime(CLOCK_REALTIME, &end);
#else
gettimeofday(&ustime, NULL);
end.tv_nsec = ustime.tv_usec * 1000;
end.tv_sec = ustime.tv_sec;
#endif
double ms = (end.tv_sec - start.tv_sec +
(end.tv_nsec - start.tv_nsec) / 1000000000.);
double fps = i / ms;
fprintf(stderr, "\nprocessed %d images in %gs @%gfps\n", i, ms, fps);
fflush(stderr);
if(zbar_video_enable(video, 0)) {
fprintf(stderr, "ERROR: while stopping video stream\n");
return(zbar_video_error_spew(video, 0));
}
fprintf(stderr, "\ncleaning up...\n");
fflush(stderr);
zbar_video_destroy(video);
if(test_image_check_cleanup())
return(32);
fprintf(stderr, "\nvideo support verified\n\n");
return(0);
}