1. 程式人生 > >Linux攝像頭V4L2程式設計

Linux攝像頭V4L2程式設計

一、摘要

    在Linux作業系統下,攝像頭的工作都要遵循V4L2的機制,下面介紹如何從無到有,編寫程式碼去使能攝像頭拍照。

使用V4L2基本上就是使用核心提供的函式介面,填充相應的函式,主要依靠ioctl()函式填充函式域,然後按照一定操作順序(工作流程),就能拍照了。

二、操作順序(工作流程)

   前奏:

    1、開啟USB攝像頭裝置檔案

    2、獲取驅動資訊(VIDIOC_QUERYCAP)

    3、設定影象格式(VIDIOC_S_FMT)

   幀緩衝:

    4、申請幀緩衝(VIDIOC_REQBUFS)

    5、獲取幀緩衝地址長度資訊(VIDIOC_QUERYBUF)

    6、使用mmap吧核心空間的幀緩衝對映到使用者空間

    7、幀緩衝入佇列

    8、開始採集影象(VIDIOC_STREAMON)

    9、取出幀緩衝(VIDIOC_DQBUF)

    10、訪問幀緩衝(可以讀寫操作等等)

    11、幀緩衝入佇列,重新入隊(VIDIOC_QBUF)

寫程式碼的時候按照這個編寫就行了。


下面的程式碼,直接複製過去,然後gcc編譯就能用了,如果是用在嵌入式板子上,只用交叉編譯以後就能用了

#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);   //驅動資訊儲存在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;   //影象格式,此處是jpg

     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;
}


上面程式碼可直接使用。