免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2142 | 回复: 1
打印 上一主题 下一主题

那些年啊,那些事---V4L2采集图片源码分享 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-25 10:39 |只看该作者 |倒序浏览
那些年啊,那些事---V4L2采集图片源码分享


把v4l2采集图片测试程序,贴在这和大家共享,在此基础上可以使实现视频的采集,只不过在pc机上写个上位机进行显示或者直接写个Qt在开发板上显示即可,


源码如下:
  1. [cpp] view plaincopyprint?
  2. #include <stdio.h>   
  3. #include <stdlib.h>   
  4. #include <string.h>   
  5. #include <assert.h>   
  6.   
  7. #include <getopt.h>   
  8.   
  9. #include <fcntl.h>   
  10. #include <unistd.h>   
  11. #include <errno.h>   
  12. #include <malloc.h>   
  13. #include <sys/stat.h>   
  14. #include <sys/types.h>   
  15. #include <sys/time.h>   
  16. #include <sys/mman.h>   
  17. #include <sys/ioctl.h>   
  18.   
  19. #include <asm/types.h>   
  20. #include <linux/videodev2.h>   
  21.   
  22. #define CLEAR(x) memset (&(x), 0, sizeof (x))   
  23.   
  24. struct buffer {  
  25. void * start;  
  26. size_t length;  
  27. };  
  28.   
  29. static char * dev_name = "/dev/video0";  
  30. static int fd = -1;  
  31. struct buffer * buffers = NULL;  
  32.   
  33. FILE *file_fd;  
  34. static unsigned long file_length;  
  35. static unsigned char *file_name;  
  36.   
  37. int main (int argc,char ** argv)  
  38. {  
  39. struct v4l2_capability cap;  
  40. struct v4l2_format fmt;  
  41.   
  42. file_fd = fopen("test.jpg", "w");  
  43.   
  44. fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);  
  45.   
  46. ioctl (fd, VIDIOC_QUERYCAP, &cap);  
  47.   
  48. CLEAR (fmt);  
  49. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  50. fmt.fmt.pix.width = 640;  
  51. fmt.fmt.pix.height = 480;  
  52. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;  
  53. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;  
  54. ioctl (fd, VIDIOC_S_FMT, &fmt);  
  55.   
  56. file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;  
  57.   
  58. buffers = calloc (1, sizeof (*buffers));  
  59.   
  60. buffers[0].length = file_length;  
  61. buffers[0].start = malloc (file_length);  
  62.   
  63. for (;;)  
  64. {  
  65. fd_set fds;  
  66. struct timeval tv;  
  67. int r;  
  68.   
  69. FD_ZERO (&fds);  
  70. FD_SET (fd, &fds);  
  71.   
  72. /* Timeout. */  
  73. tv.tv_sec = 3;  
  74. tv.tv_usec = 0;  
  75.   
  76. r = select (fd + 1, &fds, NULL, NULL, &tv);  
  77.   
  78. if (-1 == r) {  
  79. if (EINTR == errno)  
  80. continue;  
  81. printf ("select");  
  82. }  
  83.   
  84. if (0 == r) {  
  85. fprintf (stderr, "select timeout\n");  
  86. exit (EXIT_FAILURE);  
  87. }  
  88.   
  89. if (read (fd, buffers[0].start, buffers[0].length))  
  90. break;  
  91. }  
  92.   
  93. fwrite(buffers[0].start, buffers[0].length, 1, file_fd);  
  94.   
  95. free (buffers[0].start);  
  96. close (fd);  
  97. fclose (file_fd);  
  98. exit (EXIT_SUCCESS);  
  99. return 0;  
  100. }  
  101. #include <stdio.h>
  102. #include <stdlib.h>
  103. #include <string.h>
  104. #include <assert.h>

  105. #include <getopt.h>

  106. #include <fcntl.h>
  107. #include <unistd.h>
  108. #include <errno.h>
  109. #include <malloc.h>
  110. #include <sys/stat.h>
  111. #include <sys/types.h>
  112. #include <sys/time.h>
  113. #include <sys/mman.h>
  114. #include <sys/ioctl.h>

  115. #include <asm/types.h>
  116. #include <linux/videodev2.h>

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

  118. struct buffer {
  119. void * start;
  120. size_t length;
  121. };

  122. static char * dev_name = "/dev/video0";
  123. static int fd = -1;
  124. struct buffer * buffers = NULL;

  125. FILE *file_fd;
  126. static unsigned long file_length;
  127. static unsigned char *file_name;

  128. int main (int argc,char ** argv)
  129. {
  130. struct v4l2_capability cap;
  131. struct v4l2_format fmt;

  132. file_fd = fopen("test.jpg", "w");

  133. fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);

  134. ioctl (fd, VIDIOC_QUERYCAP, &cap);

  135. CLEAR (fmt);
  136. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  137. fmt.fmt.pix.width = 640;
  138. fmt.fmt.pix.height = 480;
  139. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  140. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  141. ioctl (fd, VIDIOC_S_FMT, &fmt);

  142. file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;

  143. buffers = calloc (1, sizeof (*buffers));

  144. buffers[0].length = file_length;
  145. buffers[0].start = malloc (file_length);

  146. for (;;)
  147. {
  148. fd_set fds;
  149. struct timeval tv;
  150. int r;

  151. FD_ZERO (&fds);
  152. FD_SET (fd, &fds);

  153. /* Timeout. */
  154. tv.tv_sec = 3;
  155. tv.tv_usec = 0;

  156. r = select (fd + 1, &fds, NULL, NULL, &tv);

  157. if (-1 == r) {
  158. if (EINTR == errno)
  159. continue;
  160. printf ("select");
  161. }

  162. if (0 == r) {
  163. fprintf (stderr, "select timeout\n");
  164. exit (EXIT_FAILURE);
  165. }

  166. if (read (fd, buffers[0].start, buffers[0].length))
  167. break;
  168. }

  169. fwrite(buffers[0].start, buffers[0].length, 1, file_fd);

  170. free (buffers[0].start);
  171. close (fd);
  172. fclose (file_fd);
  173. exit (EXIT_SUCCESS);
  174. return 0;
  175. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-02-25 10:39 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP