--- ./v4l2grab.c.orig 2012-01-01 20:08:30 +0100 +++ ./v4l2grab.c 2012-01-01 20:20:17 +0100 @@ -67,6 +67,7 @@ static int fd = -1; struct buffer * buffers = NULL; static unsigned int n_buffers = 0; +static unsigned int imageSeq = 0; // global settings static unsigned int width = 640; @@ -74,6 +75,7 @@ static unsigned char jpegQuality = 70; static char* jpegFilename = NULL; static char* deviceName = "/dev/video0"; +static unsigned int imageCount = 1; /** Convert from YUV422 format to RGB888. Formulae are described on http://en.wikipedia.org/wiki/YUV @@ -153,7 +155,17 @@ struct jpeg_error_mgr jerr; JSAMPROW row_pointer[1]; - FILE *outfile = fopen( jpegFilename, "wb" ); + FILE *outfile; + + if (imageCount > 1) { + char *name = malloc(strlen(jpegFilename) + 6); + sprintf(name, "%s-%04d", jpegFilename, imageSeq); + outfile = fopen( name, "wb" ); + free(name); + } + else { + outfile = fopen( jpegFilename, "wb" ); + } // try to open file for saving if (!outfile) { @@ -349,8 +361,10 @@ exit(EXIT_FAILURE); } - if (frameRead()) - break; + if (frameRead()) { + if (++imageSeq >= imageCount) + break; + } /* EAGAIN - continue select loop. */ } @@ -776,17 +790,18 @@ "-d | --device name Video device name [/dev/video0]\n" "-h | --help Print this message\n" "-o | --output JPEG output filename\n" - "-q | --quality JPEG quality (0-100)\n" + "-q | --quality JPEG quality (0-100) [70]\n" + "-c | --count Number of images to capture [1]\n" "-m | --mmap Use memory mapped buffers\n" "-r | --read Use read() calls\n" "-u | --userptr Use application allocated buffers\n" - "-W | --width width\n" - "-H | --height height\n" + "-W | --width width [640]\n" + "-H | --height height [480]\n" "", argv[0]); } -static const char short_options [] = "d:ho:q:mruW:H:"; +static const char short_options [] = "d:ho:q:c:mruW:H:"; static const struct option long_options [] = { @@ -794,6 +809,7 @@ { "help", no_argument, NULL, 'h' }, { "output", required_argument, NULL, 'o' }, { "quality", required_argument, NULL, 'q' }, + { "count", required_argument, NULL, 'c' }, { "mmap", no_argument, NULL, 'm' }, { "read", no_argument, NULL, 'r' }, { "userptr", no_argument, NULL, 'u' }, @@ -836,6 +852,11 @@ jpegQuality = atoi(optarg); break; + case 'c': + // set image count + imageCount = atoi(optarg); + break; + case 'm': #ifdef IO_MMAP io = IO_METHOD_MMAP;