歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux下攝像頭圖像數據的獲取

Linux 2.6.32內核,V4L2架構

親自測試可用

在友善之臂MINI2440開發板上測試可用

源程序如下

#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 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); //將其寫入文件中
 
 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.jpg", "wb");                //圖片文件名

 fd = open (dev_name, O_RDWR /* required */ |O_NONBLOCK);//打開設備
 if(fd<0)
 { 
  perror("open faiure!");
  exit(1);
 }
 int ff=ioctl (fd, VIDIOC_QUERYCAP, &cap);              //獲取攝像頭參數
 if(ff<0)
  printf("failture VIDIOC_QUERYCAP\n");

 struct v4l2_fmtdesc fmt1;
    int ret;
 memset(&fmt1, 0, sizeof(fmt1));
 fmt1.index = 0;            //初始化為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.width      = 320;
 fmt.fmt.pix.height      = 240;
 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;//V4L2_PIX_FMT_YUYV;//V4L2_PIX_FMT_YVU420;//V4L2_PIX_FMT_YUYV;
 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              = 1;
 req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 req.memory              = V4L2_MEMORY_MMAP;

 ff=ioctl (fd, VIDIOC_REQBUFS, &req); //申請緩沖,count是申請的數量
 if(ff<0)
  printf("failture VIDIOC_REQBUFS\n");
 if (req.count < 1)
  printf("Insufficient buffer memory\n");

 buffers = 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 /* start anywhere */,    //通過mmap建立映射關系
   buf.length,
   PROT_READ | PROT_WRITE /* required */,
   MAP_SHARED /* recommended */,
   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 (;;) //這一段涉及到異步IO
 {
  fd_set fds;
  struct timeval tv;
  int r;

  FD_ZERO (&fds);//將指定的文件描述符集清空
  FD_SET (fd, &fds);//在文件描述符集合中增加一個新的文件描述符

    /* Timeout. */
  tv.tv_sec = 2;
  tv.tv_usec = 0;

  r = select (fd + 1, &fds, NULL, NULL, &tv);//判斷是否可讀(即攝像頭是否准備好),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 ())//如果可讀,執行read_frame ()函數,並跳出循環
   break;
 }

unmap:
 for (i = 0; i < n_buffers; ++i)
  if (-1 == munmap (buffers[i].start, buffers[i].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);
return 0;
}

Copyright © Linux教程網 All Rights Reserved