基於Linux的網路攝影機採集v4l2__Linux

來源:互聯網
上載者:User
集體的源碼如下:
#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>  struct buffer {        void *                  start;        size_t                  length;}; struct buffer *buffers;unsigned long  n_buffers;unsigned long file_length;int file_fd;char *dev_name = "/dev/video3";int fd;static int read_frame (void){     struct v4l2_buffer buf;          /*幀出列*/     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     buf.memory = V4L2_MEMORY_MMAP;     ioctl (fd, VIDIOC_DQBUF, &buf);     write(file_fd,buffers[buf.index].start,buffers[buf.index].length);          /*buf入列*/     ioctl(fd, VIDIOC_QBUF, &buf);     return 1;} int main (int argc,char ** argv){     struct v4l2_capability cap;     struct v4l2_format fmt;     struct v4l2_requestbuffers req;     struct v4l2_buffer buf;      unsigned int i;     enum v4l2_buf_type type;               file_fd = open("test.jpg", O_RDWR | O_CREAT, 0777);         fd = open (dev_name, O_RDWR | O_NONBLOCK, 0);     /*擷取驅動資訊*/      ioctl (fd, VIDIOC_QUERYCAP, &cap);      printf("Driver Name:%s\n Card Name:%s\n Bus info:%s\n\n",cap.driver,cap.card,cap.bus_info);               /*設定映像格式*/     fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;     fmt.fmt.pix.width       = 320;     fmt.fmt.pix.height      = 240;     fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;     fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;     ioctl (fd, VIDIOC_S_FMT, &fmt) ;           /*申請映像緩衝區*/     req.count               = 4;     req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;     req.memory              = V4L2_MEMORY_MMAP;     ioctl (fd, VIDIOC_REQBUFS, &req);             buffers = calloc (req.count, sizeof (*buffers));           for (n_buffers = 0; n_buffers < req.count; ++n_buffers)     {            /*擷取映像緩衝區的資訊*/           buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;           buf.memory      = V4L2_MEMORY_MMAP;           buf.index       = n_buffers;            ioctl (fd, VIDIOC_QUERYBUF, &buf);                         buffers[n_buffers].length = buf.length;                       // 把核心空間中的映像緩衝區映射到使用者空間          buffers[n_buffers].start = mmap (NULL ,    //通過mmap建立映射關係                                        buf.length,                                        PROT_READ | PROT_WRITE ,                                        MAP_SHARED ,                                        fd,                                        buf.m.offset);     }             /*映像緩衝入隊*/               for (i = 0; i < n_buffers; ++i)       {               buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;               buf.memory      = V4L2_MEMORY_MMAP;               buf.index       = i;                ioctl (fd, VIDIOC_QBUF, &buf);                      }        //開始捕捉映像資料      type = V4L2_BUF_TYPE_VIDEO_CAPTURE;    ioctl (fd, VIDIOC_STREAMON, &type);   fd_set fds;   FD_ZERO (&fds);   FD_SET (fd, &fds);   select(fd + 1, &fds, NULL, NULL, NULL);      /*讀取一幅映像*/   read_frame();   for (i = 0; i < n_buffers; ++i)      munmap (buffers[i].start, buffers[i].length);       close (fd);   close (file_fd);   printf("Camera Done.\n");   return 0;}

主要的架構上面的東西。

這隻是一個比較簡單的流程,對於實際的操作我們或許可以將網路攝影機的資料通過主控制器進行處理,然後在通過Framebuffer進行渲染。具體的框圖如下:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.