linux下怎样进行摄像头编程

2020-04-22 社会 39阅读

在linux下所有设备都是文件。所以对摄像头的操作其实就是对文件的操作。USB摄像头的设备文件就是在/dev目录下的video0(假如只有一个摄像头)。在linux下操作摄像头就是使用v4l2对摄像头进行的操作,操作步骤如下

  1. 打开设备文件。

  2. int fd=open(”/dev/video0″,O_RDWR);

  3. 2. 取得设备的capability,看看设备具有什么功能,比如是否具有输入,或者音频输入输出等。VIDIOC_QUERYCAP,struct v4l2_capability

  4. v4l2_std_id std;

  5. do {

  6. ret= ioctl(fd, VIDIOC_QUERYSTD, std);

  7. } while (ret == -1  errno == EAGAIN);

  8. switch (std) {

  9. case V4L2_STD_NTSC:

  10. //……

  11. case V4L2_STD_PAL:

  12. //……

  13. }

  14. 3. 选择输入,一个设备可以有多个输入。VIDIOC_S_INPUT,struct v4l2_input(可不要)

  15. 4. 设置的制式和帧格式,制式包括PAL,NTSC,帧的格式个包括宽度和高度等。

  16. VIDIOC_S_STD,VIDIOC_S_FMT,struct v4l2_std_id,struct v4l2_format

  17. struct v4l2_format fmt;

  18. memset ( fmt, 0, sizeof(fmt) );

  19. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  20. fmt.fmt.pix.width = 320;

  21. fmt.fmt.pix.height = 240;

  22. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;

  23. if (ioctl(fd, VIDIOC_S_FMT, fmt) < 0)

  24. {

  25. printf("set format failed\n");

  26. //return 0;

  27. }

  28. 5. 向驱动申请帧缓冲,一般不超过5个。struct v4l2_requestbuffers

  29. struct v4l2_requestbuffers req;

  30. memset(req, 0, sizeof (req));

  31. req.count = 4;

  32. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  33. req.memory = V4L2_MEMORY_MMAP;

  34. if (ioctl(fd,VIDIOC_REQBUFS,req) == -1)

  35. {

  36. perror("VIDIOC_REQBUFS error \n");

  37. //return -1;

  38. }

  39. 6.申请物理内存

  40. 将申请到的帧缓冲映射到用户空间,这样就可以直接操作采集到的帧了,而不必去复制。将申请到的帧缓冲全部入队列,以便存放采集到的数据.VIDIOC_QBUF,struct v4l2_buffer

  41. VideoBuffer* buffers = calloc( req.count, sizeof(VideoBuffer) );

  42. printf("sizeof(VideoBuffer) is %d\n",sizeof(VideoBuffer));

  43. struct v4l2_buffer buf;

  44. for (numBufs = 0; numBufs < req.count; numBufs++)

  45. {

  46. memset( buf, 0, sizeof(buf) );

  47. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  48. buf.memory = V4L2_MEMORY_MMAP;

  49. buf.index = numBufs;

  50. if (ioctl(fd, VIDIOC_QUERYBUF, buf) < 0)

  51. {

  52. printf("VIDIOC_QUERYBUF error\n");

  53. //return -1;

  54. }

  55. printf("buf len is %d\n",sizeof(buf));

  56. //内存映射

  57. buffers[numBufs].length = buf.length;

  58. buffers[numBufs].offset = (size_t) buf.m.offset;

  59. buffers[numBufs].start = mmap (NULL, buf.length,PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);

  60. printf("buffers.length = %d,buffers.offset = %d ,buffers.start[0] = %d\n",buffers[numBufs].length,buffers[numBufs].offset,buffers[numBufs].start[0]);

  61. printf("buf2 len is %d\n",sizeof(buffers[numBufs].start));

  62. if (buffers[numBufs].start == MAP_FAILED)

  63. {

  64. perror("buffers error\n");

  65. //return -1;

  66. }

  67. if (ioctl (fd, VIDIOC_QBUF, buf) < 0)

  68. {

  69. printf("VIDIOC_QBUF error\n");

  70. //return -1;

  71. }

  72. }

  73. 7. 开始的采集。

  74. enum v4l2_buf_type type;

  75. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  76. if (ioctl (fd, VIDIOC_STREAMON, type) < 0)

  77. {

  78. printf("VIDIOC_STREAMON error\n");

  79. // return -1;

  80. }

  81. 8. 出队列以取得已采集数据的帧缓冲,取得原始采集数据。VIDIOC_DQBUF, 将缓冲重新入队列尾,这样可以循环采集。VIDIOC_QBUF

  82. if (ioctl(fd, VIDIOC_DQBUF, buf) < 0)

  83. {

  84. perror("VIDIOC_DQBUF failed.\n");

  85. //return -1;

  86. }

  87. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  88. buf.memory = V4L2_MEMORY_MMAP;

  89. unsigned char *ptcur = buffers[numBufs].start;

  90. DEBUG("buf.bytesused = %d \n",buf.bytesused);

  91. int i1;

  92. for(i1=0; i1

  93. {

  94. if((buffers[numBufs].start[i1] == 0xFF)  (buffers[numBufs].start[i1+1] == 0xC4))

  95. {

  96. DEBUG("huffman table finded! \nbuf.bytesused = %d\nFFC4 = %d \n",buf.bytesused,i1);

  97. break;

  98. }

  99. }

  100. if(i1 == buf.bytesused)printf("huffman table don't exist! \n");

  101. int i;

  102. for(i=0; i

  103. {

  104. if((buffers[numBufs].start[i] == 0xFF)  (buffers[numBufs].start[i+1] == 0xD8)) break;

  105. ptcur++;

  106. }

  107. DEBUG("i=%d,FF=%02x,D8=%02x\n",i,buffers[numBufs].start[i],buffers[numBufs].start[i+1]);

  108. int imagesize =buf.bytesused - i;

  109. DEBUG("buf.bytesused = %d \n",buf.bytesused);

  110. DEBUG ("imagesize = %d \n",imagesize);

  111. 9. 停止的采集。VIDIOC_STREAMOFF

  112. 10. 关闭设备。close(fd);

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com