Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4470351
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-04-30 15:19:32

     我这里将 阿虚的电子小屋 中的 关于 V4L2摄像头获取图片 稍微改了下,在自己的开发板上就可以使用了,采集到了单一的图片,小高兴了会。。。

  1. //#Rockie Cheng


  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>

  6. #include <getopt.h>

  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <malloc.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <sys/time.h>
  14. #include <sys/mman.h>
  15. #include <sys/ioctl.h>

  16. #include <asm/types.h>
  17. #include <linux/videodev2.h>

  18. #define CLEAR(x) memset (&(x), 0, sizeof (x))

  19. struct buffer {
  20.         void * start;
  21.         size_t length;
  22. };

  23. static char * dev_name = "/dev/video0";//摄像头设备名
  24. static int fd = -1;
  25. struct buffer * buffers = NULL;
  26. static unsigned int n_buffers = 0;

  27. FILE *file_fd;
  28. static unsigned long file_length;
  29. static unsigned char *file_name;
  30. //////////////////////////////////////////////////////
  31. //获取一帧数据
  32. //////////////////////////////////////////////////////
  33. static int read_frame (void)
  34. {
  35. struct v4l2_buffer buf;
  36. unsigned int i;

  37. CLEAR (buf);
  38. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  39. buf.memory = V4L2_MEMORY_MMAP;

  40. ioctl (fd, VIDIOC_DQBUF, &buf); //出列采集的帧缓冲

  41. assert (buf.index < n_buffers);
  42.    printf ("buf.index dq is %d,\n",buf.index);

  43. fwrite(buffers[buf.index].start, buffers[buf.index].length, 1, file_fd); //将其写入文件中
  44.   
  45. ioctl (fd, VIDIOC_QBUF, &buf); //再将其入列

  46. return 1;
  47. }

  48. int main (int argc,char ** argv)
  49. {
  50. struct v4l2_capability cap;
  51. struct v4l2_format fmt;
  52. unsigned int i;
  53. enum v4l2_buf_type type;

  54. file_fd = fopen("test-mmap.jpg", "w");//图片文件名

  55. fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);//打开设备

  56. ioctl (fd, VIDIOC_QUERYCAP, &cap);//获取摄像头参数

  57. CLEAR (fmt);
  58. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  59. fmt.fmt.pix.width = 320;
  60. fmt.fmt.pix.height = 240;
  61. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  62. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  63. ioctl (fd, VIDIOC_S_FMT, &fmt); //设置图像格式

  64. file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; //计算图片大小

  65. struct v4l2_requestbuffers req;
  66. CLEAR (req);
  67. req.count = 4;
  68. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  69. req.memory = V4L2_MEMORY_MMAP;

  70. ioctl (fd, VIDIOC_REQBUFS, &req); //申请缓冲,count是申请的数量

  71. if (req.count < 2)
  72.    printf("Insufficient buffer memory\n");

  73. buffers = calloc (req.count, sizeof (*buffers));//内存中建立对应空间

  74. for (n_buffers = 0; n_buffers < req.count; ++n_buffers)
  75. {
  76.    struct v4l2_buffer buf; //驱动中的一帧
  77.    CLEAR (buf);
  78.    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  79.    buf.memory = V4L2_MEMORY_MMAP;
  80.    buf.index = n_buffers;

  81.    if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf)) //映射用户空间
  82.     printf ("VIDIOC_QUERYBUF error\n");

  83.    buffers[n_buffers].length = buf.length;
  84.    buffers[n_buffers].start =
  85.    mmap (NULL /* start anywhere */, //通过mmap建立映射关系
  86.     buf.length,
  87.     PROT_READ | PROT_WRITE /* required */,
  88.     MAP_SHARED /* recommended */,
  89.     fd, buf.m.offset);

  90.    if (MAP_FAILED == buffers[n_buffers].start)
  91.     printf ("mmap failed\n");
  92.         }

  93. for (i = 0; i < n_buffers; ++i)
  94. {
  95.    struct v4l2_buffer buf;
  96.    CLEAR (buf);

  97.    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  98.    buf.memory = V4L2_MEMORY_MMAP;
  99.    buf.index = i;

  100.    if (-1 == ioctl (fd, VIDIOC_QBUF, &buf))//申请到的缓冲进入列队
  101.     printf ("VIDIOC_QBUF failed\n");
  102. }
  103.                
  104. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  105. if (-1 == ioctl (fd, VIDIOC_STREAMON, &type)) //开始捕捉图像数据
  106.    printf ("VIDIOC_STREAMON failed\n");

  107. for (;;) //这一段涉及到异步IO
  108. {
  109.    fd_set fds;
  110.    struct timeval tv;
  111.    int r;

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

  114.    /* Timeout. */
  115.    tv.tv_sec = 2;
  116.    tv.tv_usec = 0;

  117.    r = select (fd + 1, &fds, NULL, NULL, &tv);//判断是否可读(即摄像头是否准备好),tv是定时

  118.    if (-1 == r) {
  119.     if (EINTR == errno)
  120.      continue;
  121.     printf ("select err\n");
  122.                         }
  123.    if (0 == r) {
  124.     fprintf (stderr, "select timeout\n");
  125.     exit (EXIT_FAILURE);
  126.                         }

  127.    if (read_frame ())//如果可读,执行read_frame ()函数,并跳出循环
  128.    break;
  129. }

  130. unmap:
  131. for (i = 0; i < n_buffers; ++i)
  132.    if (-1 == munmap (buffers[i].start, buffers[i].length))
  133.     printf ("munmap error");
  134. close (fd);
  135. fclose (file_fd);
  136. exit (EXIT_SUCCESS);
  137. return 0;
  138. }

1.编译程序
  1. yxx@yuweixian:~/yu/v4l2$ arm-linux-gcc -o axu axu.c


2.将 aux 复制到 开发板上

   1. 建立测试图片,在程序中写道
  1. file_fd = fopen("test-mmap.jpg", "w");//图片文件名
  
  1. [root@yuweixian yu]# touch test-mmap.jpg
 
   2. 测试 aux 程序
  1. [root@yuweixian yu]# ./axu
  2. zc3xx: probe 2wr ov vga 0x0000
  3. buf.index dq is 0,
  4. [root@yuweixian yu]#

  3. 复制 test-mmap.jpg 文件 到 xp下,查看




阅读(2740) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

tamtian20082012-09-18 15:43:27

为什么我的test-mmap.jpg图片什么都没有?