Navigation C API Pages Python bindings Applications

Grabbers (v4l2) Examples

Simple grabber example that saves images taken from v4l2 device.

/*****************************************************************************
 * This file is part of gfxprim library.                                     *
 *                                                                           *
 * Gfxprim is free software; you can redistribute it and/or                  *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 2.1 of the License, or (at your option) any later version.        *
 *                                                                           *
 * Gfxprim 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 General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with gfxprim; if not, write to the Free Software            *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,                        *
 * Boston, MA  02110-1301  USA                                               *
 *                                                                           *
 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz>                       *
 *                                                                           *
 *****************************************************************************/

 /*

   Gets an image from v4l2 device.

  */

#include <string.h>
#include <errno.h>
#include <stdio.h>

#include <GP.h>

static int get_image(const char *filename, GP_Grabber *grabber)
{
        /* turn on grabber */
        if (GP_GrabberStart(grabber)) {
                fprintf(stderr, "Failed to start grabber\n");
                return 1;
        }

        /* throw away first frame, it's usually wrong */
        while (!GP_GrabberPoll(grabber))
                usleep(100000);

        while (!GP_GrabberPoll(grabber))
                usleep(100000);

        /* save image */
        if (GP_SaveJPG(grabber->frame, filename, NULL)) {
                fprintf(stderr, "Failed to save image '%s': %s",
                        filename, strerror(errno));
                return 1;
        }

        /* turn off grabber */
        if (GP_GrabberStop(grabber)) {
                fprintf(stderr, "Failed to start grabber\n");
                return 1;
        }

        return 0;
}

int main(int argc, char *argv[])
{
        const char *v4l2_device = "/dev/video0";
        const char *image_filename = "frame.jpg";
        unsigned int w = 640, h = 480;
        int secs = 0;
        int opt;

        while ((opt = getopt(argc, argv, "d:hH:o:W:l:s:")) != -1) {
                switch (opt) {
                case 'o':
                        image_filename = optarg;
                break;
                case 'd':
                        v4l2_device = optarg;
                break;
                case 'W':
                        w = atoi(optarg);
                break;
                case 'H':
                        h = atoi(optarg);
                break;
                case 's':
                        secs = atoi(optarg);
                break;
                case 'l':
                        GP_SetDebugLevel(atoi(optarg));
                break;
                case 'h':
                        printf("Usage; %s opts\n", argv[0]);
                        printf("-o  output image file, default is 'frame.jpg'\n"
                               "-d  v4l2 device name (default is '/dev/video0'\n"
                               "-W  output image width, default is 640\n"
                               "-H  output image height, default is 480\n"
                               "-l  sets GFXprim debug level (default is 0)\n"
                               "-s  take image every s seconds (the images are stored as frameX.jpg)\n"
                               "-h  prints this help\n");
                        return 0;
                break;
                default:
                        fprintf(stderr, "Invalid paramter '%c'\n", opt);
                        return 1;
                }
        }

        GP_Grabber *grabber = GP_GrabberV4L2Init(v4l2_device, w, h);

        if (grabber == NULL) {
                fprintf(stderr, "Failed to initalize grabber '%s': %s\n",
                        v4l2_device, strerror(errno));
                return 1;
        }

        if (secs == 0) {
                get_image(image_filename, grabber);
                GP_GrabberExit(grabber);
                return 0;
        }

        int i = 0;

        for (;;) {
                char buf[128];

                snprintf(buf, sizeof(buf), "frame%03i.jpg", i++);

                if (get_image(buf, grabber)) {
                        fprintf(stderr, "Failed to get image, exitting...\n");
                        return 1;
                }

                sleep(secs);
        }


        return 0;
}

More sophisticated example utilizing v4l2 device and backends.

/*****************************************************************************
 * This file is part of gfxprim library.                                     *
 *                                                                           *
 * Gfxprim is free software; you can redistribute it and/or                  *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 2.1 of the License, or (at your option) any later version.        *
 *                                                                           *
 * Gfxprim 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 General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with gfxprim; if not, write to the Free Software            *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,                        *
 * Boston, MA  02110-1301  USA                                               *
 *                                                                           *
 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz>                       *
 *                                                                           *
 *****************************************************************************/

 /*

   Simple V4L2 grabber interactive example.

  */

#include <errno.h>
#include <string.h>
#include <stdio.h>

#include <GP.h>

int main(int argc, char *argv[])
{
        GP_Backend *backend;
        GP_Grabber *grabber;
        const char *v4l2_device = "/dev/video0";
        unsigned int w = 320, h = 240;
        int mode = 0;
        int opt;

        while ((opt = getopt(argc, argv, "d:hH:W:l:")) != -1) {
                switch (opt) {
                case 'd':
                        v4l2_device = optarg;
                break;
                case 'W':
                        w = atoi(optarg);
                break;
                case 'H':
                        h = atoi(optarg);
                break;
                case 'l':
                        GP_SetDebugLevel(atoi(optarg));
                break;
                case 'h':
                        printf("Usage; %s opts\n", argv[0]);
                        printf("-d  v4l2 device name (default is '/dev/video0'\n"
                               "-W  output image width, default is 640\n"
                               "-H  output image height, default is 480\n"
                               "-l  sets GFXprim debug level (default is 0)\n"
                               "-h  prints this help\n");
                        return 0;
                break;
                default:
                        fprintf(stderr, "Invalid paramter '%c'\n", opt);
                        return 1;
                }
        }

        grabber = GP_GrabberV4L2Init(v4l2_device, w, h);

        if (grabber == NULL) {
                fprintf(stderr, "Failed to initalize grabber '%s': %s\n",
                        v4l2_device, strerror(errno));
                return 1;
        }

        backend = GP_BackendX11Init(NULL, 0, 0, grabber->frame->w,
                                    grabber->frame->h, "V4L2", 0);

        if (backend == NULL) {
                GP_GrabberExit(grabber);
                return 1;
        }

        if (GP_GrabberStart(grabber)) {
                fprintf(stderr, "Failed to start grabber\n");
                GP_BackendExit(backend);
                GP_GrabberExit(grabber);
                return 1;
        }

        printf("Press SPACE to change mode and Q to exit.\n");

        for (;;) {
                if (GP_GrabberPoll(grabber) > 0) {
                        GP_Context *res, *img = grabber->frame;

                        switch (mode) {
                        case 0:
                                res = img;
                        break;
                        case 1:
                        //      GP_FilterEdgePrewitt(img, &res, NULL, NULL);
                                GP_FilterEdgeSobel(img, &res, NULL, NULL);
                        break;
                        case 2:
                                GP_FilterGaussianBlur(img, img, 1, 1, NULL);
                                res = GP_FilterFloydSteinbergAlloc(img, GP_PIXEL_G2, NULL);
                        break;
                        }

                        GP_Blit_Clipped(res, 0, 0, res->w, res->h, backend->context, 0, 0);
                        GP_BackendFlip(backend);

                        if (mode)
                                GP_ContextFree(res);
                }

                usleep(1000);

                GP_BackendPoll(backend);

                /* Read and parse events */
                GP_Event ev;

                while (GP_BackendGetEvent(backend, &ev)) {
                        switch (ev.type) {
                        case GP_EV_KEY:

                                /* ignore key up events */
                                if (!ev.code)
                                        continue;

                                switch (ev.val.key.key) {
                                case GP_KEY_ESC:
                                case GP_KEY_Q:
                                        GP_BackendExit(backend);
                                        GP_GrabberExit(grabber);
                                        return 0;
                                break;
                                case GP_KEY_SPACE:

                                        mode++;

                                        if (mode > 2)
                                                mode = 0;
                                break;
                                }
                        }
                }
        }

        GP_BackendExit(backend);

        return 0;
}