Chinaunix首页 | 论坛 | 博客
  • 博客访问: 560254
  • 博文数量: 252
  • 博客积分: 1068
  • 博客等级: 少尉
  • 技术积分: 1775
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-05 21:33
文章分类

全部博文(252)

文章存档

2013年(21)

2012年(231)

分类: C/C++

2013-05-24 10:44:04

  1. 终于把mini2440 usb摄像头程序的搞懂了,附上程序及我的一些注释:
    程序名:usbcamera.cpp
    Makefile
    usbcamera: usbcamera.cpp
    arm-linux-g++ usbcamera.cpp -Wall -O2 -o usbcamera
    arm-linux-strip -s usbcamera

    clean:
    rm usbcamera
    可执行文件:usbcamera
    使用方法:
    将可执行文件下载到开发板,执行,会在当前文件夹生成一张图片。

  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. /*8.出队列以取得已采集数据的帧缓冲,取得原始采集数据。VIDIOC_DQBUF*/
  41.     int ff = ioctl (fd, VIDIOC_DQBUF, &buf);
  42.     if(ff<0)
  43.         printf("failture\n"); //出列采集的帧缓冲

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

  46.     fwrite(buffers[buf.index].start, buffers[buf.index].length, 1, file_fd); //将其写入文件中
  47. /*9.将缓冲重新入队列尾,这样可以循环采集。VIDIOC_QBUF*/
  48.     ff=ioctl (fd, VIDIOC_QBUF, &buf); //再将其入列
  49.     if(ff<0)//把数据从缓存中读取出来
  50.         printf("failture VIDIOC_QBUF\n");
  51.     return 1;
  52. }

  53. int main (int argc,char ** argv)
  54. {
  55.     struct v4l2_capability cap;
  56.     struct v4l2_format fmt;
  57.     unsigned int i;
  58.     enum v4l2_buf_type type;

  59.     file_fd = fopen("test-mmap.jpg", "w");//图片文件名
  60. /*1.打开设备文件。 int fd=open(/dev/video0″,O_RDWR);*********/
  61.     fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);//打开设备

  62. /*2.取得设备的capability,看看设备具有什么功能,比如是否具有视频输入,或者音频输入输出等。VIDIOC_QUERYCAP,struct v4l2_capability*/
  63.     int ff=ioctl (fd, VIDIOC_QUERYCAP, &cap);//获取摄像头参数
  64.     if(ff<0)
  65.         printf("failture VIDIOC_QUERYCAP\n");

  66. /*3.设置视频的制式和帧格式,制式包括PAL,NTSC,帧的格式个包括宽度和高度等。*/
  67.         struct v4l2_fmtdesc fmt1;
  68.         int ret;
  69.         memset(&fmt1, 0, sizeof(fmt1));
  70.         fmt1.index = 0;
  71.         fmt1.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  72.     //获取当前驱动支持的视频格式
  73.         while ((ret = ioctl(fd, VIDIOC_ENUM_FMT, &fmt1)) == 0)
  74.         {
  75.               fmt1.index++;
  76.               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);
  77.         }
  78.     //帧的格式,比如宽度,高度等
  79.     CLEAR (fmt);
  80.     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; //数据流类型,必须永远是V4L2_BUF_TYPE_VIDEO_CAPTURE
  81.     fmt.fmt.pix.width = 640;//宽,必须是16的倍数
  82.     fmt.fmt.pix.height = 480;////高,必须是16的倍数
  83.     fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;//视频数据存储类型//V4L2_PIX_FMT_YUYV;//V4L2_PIX_FMT_YVU420;//V4L2_PIX_FMT_YUYV;
  84.     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  85.     //设置当前驱动的频捕获格式
  86.     ff = ioctl (fd, VIDIOC_S_FMT, &fmt);
  87.     if(ff<0)
  88.         printf("failture VIDIOC_S_FMT\n");
  89.     //计算图片大小
  90.     file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;

  91. /*4.向驱动申请帧缓冲,一般不超过5个。struct v4l2_requestbuffers*/
  92.     struct v4l2_requestbuffers req;
  93.     CLEAR (req);
  94.     req.count = 1;//缓存数量,也就是说在缓存队列里保持多少张照片
  95.     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  96.     req.memory = V4L2_MEMORY_MMAP;//或V4L2_MEMORY_USERPTR

  97.     ff = ioctl (fd, VIDIOC_REQBUFS, &req); //申请缓冲,count是申请的数量
  98.     if(ff<0)
  99.         printf("failture VIDIOC_REQBUFS\n");
  100.     if (req.count < 1)
  101.         printf("Insufficient buffer memory\n");

  102.     buffers = (struct buffer*)calloc (req.count, sizeof (*buffers));//内存中建立对应空间
  103. /*5.将申请到的帧缓冲映射到用户空间,这样就可以直接操作采集到的帧了,而不必去复制。mmap*/
  104.     for (n_buffers = 0; n_buffers < req.count; ++n_buffers)
  105.     {
  106.         struct v4l2_buffer buf; //驱动中的一帧
  107.         CLEAR (buf);
  108.            buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  109.            buf.memory = V4L2_MEMORY_MMAP;
  110.            buf.index = n_buffers;
  111.         //把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址
  112.            if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buf)) //映射用户空间
  113.                 printf ("VIDIOC_QUERYBUF error\n");

  114.            buffers[n_buffers].length = buf.length;
  115.            buffers[n_buffers].start = mmap (NULL /* start anywhere */,buf.length,PROT_READ | PROT_WRITE /* required */,MAP_SHARED /* recommended */,fd, buf.m.offset);//通过mmap建立映射关系,返回映射区的起始地址

  116.            if (MAP_FAILED == buffers[n_buffers].start)
  117.             printf ("mmap failed\n");
  118.         }
  119. /*6.将申请到的帧缓冲全部入队列,以便存放采集到的数据.VIDIOC_QBUF,struct v4l2_buffer*/
  120.     for (i = 0; i < n_buffers; ++i)
  121.     {
  122.            struct v4l2_buffer buf;
  123.            CLEAR (buf);

  124.            buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  125.          buf.memory = V4L2_MEMORY_MMAP;
  126.            buf.index = i;
  127.             //把数据从缓存中读取出来
  128.            if (-1 == ioctl (fd, VIDIOC_QBUF, &buf))//申请到的缓冲进入列队
  129.                 printf ("VIDIOC_QBUF failed\n");
  130.     }
  131.                 
  132.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  133. /*7.开始视频的采集。VIDIOC_STREAMON*/
  134.     if (-1 == ioctl (fd, VIDIOC_STREAMON, &type)) //开始捕捉图像数据
  135.            printf ("VIDIOC_STREAMON failed\n");

  136.     for (;;) //这一段涉及到异步IO
  137.     {
  138.            fd_set fds;
  139.            struct timeval tv;
  140.            int r;

  141.            FD_ZERO (&fds);//将指定的?件描述符集清空
  142.            FD_SET (fd, &fds);//在文件描述符集合中增鍔????个新的文件描述符

  143.            /* Timeout. */
  144.            tv.tv_sec = 2;
  145.            tv.tv_usec = 0;

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

  147.            if (-1 == r)
  148.         {
  149.                 if (EINTR == errno)
  150.                      continue;
  151.                 printf ("select err\n");
  152.                 }
  153.            if (0 == r) {
  154.                 fprintf (stderr, "select timeout\n");
  155.                 exit (EXIT_FAILURE);
  156.                 }

  157.            if (read_frame ())//如果可读,执行read_frame ()函数,并跳出循环
  158.                break;
  159.     }

  160.     unmap:
  161.     for (i = 0; i < n_buffers; ++i)
  162.        if (-1 == munmap (buffers->start, buffers->length))
  163.             printf ("munmap error");
  164.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  165. /*10.停止视频的采集。VIDIOC_STREAMOFF*/
  166.         if (-1 == ioctl(fd, VIDIOC_STREAMOFF, &type))
  167.                 printf("VIDIOC_STREAMOFF");
  168. /*11.关闭视频设备。close(fd);*/
  169.     close (fd);
  170.     fclose (file_fd);
  171.     exit (EXIT_SUCCESS);
  172.     return 0;
  173. }



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