1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 Gets an image from v4l2 device.
35 static int get_image(const char *filename
, gp_grabber
*grabber
)
38 if (gp_grabber_start(grabber
)) {
39 fprintf(stderr
, "Failed to start grabber\n");
43 /* throw away first frame, it's usually wrong */
44 while (!gp_grabber_poll(grabber
))
47 while (!gp_grabber_poll(grabber
))
51 if (gp_save_jpg(grabber
->frame
, filename
, NULL
)) {
52 fprintf(stderr
, "Failed to save image '%s': %s",
53 filename
, strerror(errno
));
57 /* turn off grabber */
58 if (gp_grabber_stop(grabber
)) {
59 fprintf(stderr
, "Failed to start grabber\n");
66 int main(int argc
, char *argv
[])
68 const char *v4l2_device
= "/dev/video0";
69 const char *image_filename
= "frame.jpg";
70 unsigned int w
= 640, h
= 480;
74 while ((opt
= getopt(argc
, argv
, "d:hH:o:W:l:s:")) != -1) {
77 image_filename
= optarg
;
92 gp_set_debug_level(atoi(optarg
));
95 printf("Usage; %s opts\n", argv
[0]);
96 printf("-o output image file, default is 'frame.jpg'\n"
97 "-d v4l2 device name (default is '/dev/video0'\n"
98 "-W output image width, default is 640\n"
99 "-H output image height, default is 480\n"
100 "-l sets GFXprim debug level (default is 0)\n"
101 "-s take image every s seconds (the images are stored as frameX.jpg)\n"
102 "-h prints this help\n");
106 fprintf(stderr
, "Invalid paramter '%c'\n", opt
);
111 gp_grabber
*grabber
= gp_grabber_v4l2_init(v4l2_device
, w
, h
);
113 if (grabber
== NULL
) {
114 fprintf(stderr
, "Failed to initalize grabber '%s': %s\n",
115 v4l2_device
, strerror(errno
));
120 get_image(image_filename
, grabber
);
121 gp_grabber_exit(grabber
);
130 snprintf(buf
, sizeof(buf
), "frame%03i.jpg", i
++);
132 if (get_image(buf
, grabber
)) {
133 fprintf(stderr
, "Failed to get image, exitting...\n");