描述了摄像头的图像采集能力——能输出哪些像素格式、每种格式下能达到哪些分辨率、当前配置是什么,以及如何试探和更改这些参数。

支持的像素格式(枚举)

  • 通过 VIDIOC_ENUM_FMT 列出所有可用的像素格式(如 YUYV、MJPEG、H264 等),并打印 FourCC 码和描述信息。
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *dev = "/dev/video0";
static void fmtdesc(int fd)
{
    struct v4l2_fmtdesc fmt;
    printf("\n====== 枚举支持的像素格式 ======\n");
	memset(&fmt, 0, sizeof(fmt)); // 用 memset 将所有字段清零,避免残留数据影响 ioctl。
	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; // 设置 fmt.type 为 V4L2_BUF_TYPE_VIDEO_CAPTURE (摄像头设备)

	for (fmt.index = 0; ; fmt.index++) { 
		if (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) < 0)
			break;
		printf("  [%d] 0x%08x  %s\n",
		       fmt.index,
		       fmt.pixelformat,
		       fmt.description);
	}
	printf("\n");
}
int main(void)
{
    int fd = open(dev, O_RDWR);
    if (fd < 0)
    {
        printf("open %s failed\n", dev);
        return -1;
    }
	fmtdesc(fd);
    close(fd);
    return 0;
}
  • 结果

    root@wyl:~/c-std/for-linux/099_other/v4l2# ./002_v4l2_fmt_type 
    ====== 枚举支持的像素格式 ======
      [0] 0x56595559  YUYV 4:2:2