我一直在努力编写 C++ 代码来从网络摄像头捕获图片。我成功地做到了,但我想对我所采取的过程进行一些澄清。
所以我的代码可以用6个步骤来描述,我将它们和我的问题一起写在这里: 一、步骤:初始化设备并设置图像格式。
const char* dev_name = "/dev/video0";
int width=320;
int height=240;
int fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
struct v4l2_format format = {0};
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.fmt.pix.width = width;
format.fmt.pix.height = height;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;//V4L2_PIX_FMT_YUYV //V4L2_PIX_FMT_RGB24
format.fmt.pix.field = V4L2_FIELD_NONE; //V4L2_FIELD_NONE
xioctl(fd, VIDIOC_S_FMT, &format);
Run Code Online (Sandbox Code Playgroud)
二. 步骤:请求缓冲区。
struct v4l2_requestbuffers req = {0};
req.count = 2;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
xioctl(fd, VIDIOC_REQBUFS, &req);
Run Code Online (Sandbox Code Playgroud)
三.步骤:查询缓冲区和映射。
struct v4l2_buffer buf;
buffer* buffers;
unsigned int i;
buffers = (buffer*) …Run Code Online (Sandbox Code Playgroud)