Extracting USB camera images from v4l2 in Linux (1)

Source: Internet
Author: User

I used the z301b USB camera of xingwei and ran it on the embedded board. I searched the information on the network for a long time and did it myself, but I still cannot, I don't have an arm base. I have been in touch with Linux for more than a week or two. Now I have no clue. Please write it down. After calculating it, I will try it again ~~

The following program can collect images, but only one image can be collected and saved as JPG. Even if the selected video format is uyvy and saved as YUV, however, the final file is still in JPG format because the YUV decoder cannot be used to view the file, after you change the suffix to JPG, you can use the image viewer to view the image information, I switched to YUV format, but I haven't found any relevant materials for a long time.

The following is the source code of the Program for extracting an image, which has been implemented, but the program is owned by others. I want to modify it and save the image in YUV format, or the video can also be used, but I really don't know how to start... after two weeks of rolling, I still got nothing ~~~~ Tragedy ·························

 

/*arm-linux-gcc -o usbcamera usbcamera.c ; then download the file and put in the /sbin .*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <getopt.h> #include <fcntl.h> #include <unistd.h>#include <errno.h>#include <malloc.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/time.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <asm/types.h> #include <linux/videodev2.h>#define CAPTURE_FILE "/media/frame.yuv"#define CLEAR(x) memset (&(x), 0, sizeof (x))struct buffer {        void * start;        size_t length;};static char * dev_name = "/dev/video0";static int fd = -1;struct buffer * buffers = NULL;static unsigned int n_buffers = 0;FILE *file_fd;static unsigned long file_length;static unsigned char *file_name;static int read_frame (void){    struct v4l2_buffer buf;    unsigned int i;    CLEAR (buf);    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;    buf.memory = V4L2_MEMORY_MMAP;    int ff = ioctl (fd, VIDIOC_DQBUF, &buf);    if(ff<0)        printf("failture\n");     assert (buf.index < n_buffers);       printf ("buf.index dq is %d,\n",buf.index);    fwrite(buffers[buf.index].start, buffers[buf.index].length, 1, file_fd);///////////////////////    FILE *fp = fopen(CAPTURE_FILE, "w+");    if (fp < 0) {        printf("open frame data file failed\n");        return -1;    }    fwrite(buffers[buf.index].start, 1, buffers[buf.index].length, fp);
    fclose(fp);    printf("Capture one frame saved in %s\n", CAPTURE_FILE);////////////////////    ff=ioctl (fd, VIDIOC_QBUF, &buf);     if(ff<0)        printf("failture VIDIOC_QBUF\n");    return 1;}int main (int argc,char ** argv){    struct v4l2_capability cap;    struct v4l2_format fmt;    unsigned int i;    enum v4l2_buf_type type;file_fd = fopen("test-mmap.jpg", "w");    fd = open (dev_name, O_RDWR | O_NONBLOCK, 0);    int ff=ioctl (fd, VIDIOC_QUERYCAP, &cap);    if(ff<0)        printf("failture VIDIOC_QUERYCAP\n"); // Print capability infomations    printf("Capability Informations:\n");    printf(" driver: %s\n", cap.driver);    printf(" card: %s\n", cap.card);    printf(" bus_info: %s\n", cap.bus_info);    printf(" version: %08X\n", cap.version);    printf(" capabilities: %08X\n", cap.capabilities);        struct v4l2_fmtdesc fmt1;        int ret;        memset(&fmt1, 0, sizeof(fmt1));        fmt1.index = 0;        fmt1.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;    while ((ret = ioctl(fd, VIDIOC_ENUM_FMT, &fmt1)) == 0)        {         fmt1.index++;              printf("{ pixelformat = '%c%c%c%c', description = '%s' }\n",fmt1.pixelformat & 0xFF, (fmt1.pixelformat >> 8) & 0xFF,(fmt1.pixelformat >> 16) & 0xFF, (fmt1.pixelformat >> 24) & 0xFF,fmt1.description);        }    CLEAR (fmt);fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     fmt.fmt.pix.width = 640;    fmt.fmt.pix.height = 480;fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;//V4L2_PIX_FMT_JPEG;    fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;    ff = ioctl (fd, VIDIOC_S_FMT, &fmt);     if(ff<0)        printf("failture VIDIOC_S_FMT\n");    file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;     struct v4l2_requestbuffers req;    CLEAR (req);    req.count = 4;//1    req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;    req.memory = V4L2_MEMORY_MMAP;    ff = ioctl (fd, VIDIOC_REQBUFS, &req);  if(ff<0)        printf("failture VIDIOC_REQBUFS\n");   if (req.count < 1)        printf("Insufficient buffer memory\n");    buffers = (struct buffer*)calloc (req.count, sizeof (*buffers));    for (n_buffers = 0; n_buffers < req.count; ++n_buffers)    {         struct v4l2_buffer buf;         CLEAR (buf);           buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;           buf.memory = V4L2_MEMORY_MMAP;           buf.index = n_buffers;           if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf))                 printf ("VIDIOC_QUERYBUF error\n");           buffers[n_buffers].length = buf.length;           buffers[n_buffers].start = mmap (NULL ,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED ,fd, buf.m.offset);           if (MAP_FAILED == buffers[n_buffers].start)            printf ("mmap failed\n");     }    for (i = 0; i < n_buffers; ++i)    {           struct v4l2_buffer buf;           CLEAR (buf);           buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;         buf.memory = V4L2_MEMORY_MMAP;           buf.index = i;           if (-1 == ioctl (fd, VIDIOC_QBUF, &buf))                printf ("VIDIOC_QBUF failed\n");    }    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;    if (-1 == ioctl (fd, VIDIOC_STREAMON, &type))            printf ("VIDIOC_STREAMON failed\n");    for (;;)     {           fd_set fds;           struct timeval tv;           int r;           FD_ZERO (&fds);           FD_SET (fd, &fds);           tv.tv_sec = 2;           tv.tv_usec = 0;r = select (fd + 1, &fds, NULL, NULL, &tv);        if (-1 == r)         {              if (EINTR == errno)                     continue;                printf ("select err\n");         }       if (0 == r)         {                fprintf (stderr, "select timeout\n");                exit (EXIT_FAILURE);        }    if (read_frame ())               break;    }    unmap:    for (i = 0; i < n_buffers; ++i)       if (-1 == munmap (buffers->start, buffers->length))            printf ("munmap error");type = V4L2_BUF_TYPE_VIDEO_CAPTURE;        if (-1 == ioctl(fd, VIDIOC_STREAMOFF, &type))                 printf("VIDIOC_STREAMOFF");   close (fd);    fclose (file_fd);    exit (EXIT_SUCCESS);    return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.