#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#define VIDEO_DEV "/dev/video0"
int main()
{
int fd;
struct v4l2_capability cap;
struct v4l2_format fmt;
struct v4l2_requestbuffers req;
struct v4l2_buffer buf;
void *buffer_start;
FILE *file;
// 打开视频设备
fd = open(VIDEO_DEV, O_RDWR);
if (fd == -1) {
perror("Opening video device");
return 1;
}
// 查询设备能力
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
perror("Querying Capabilities");
return 1;
}
// 设置视频捕捉格式
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 1920;
fmt.fmt.pix.height = 1080;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field = V4L2_FIELD_NONE;
if (ioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
perror("Setting Pixel Format");
return 1;
}
// 请求缓冲区
req.count = 1;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
perror("Requesting Buffer");
return 1;
}
// 查询缓冲区
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = 0;
if (ioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
perror("Querying Buffer");
return 1;
}
// 映射缓冲区
buffer_start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
if (buffer_start == MAP_FAILED) {
perror("Mapping Buffer");
return 1;
}
// 将缓冲区加入队列
if (ioctl(fd, VIDIOC_QBUF, &buf) == -1) {
perror("Queue Buffer");
return 1;
}
// 开始捕捉
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(fd, VIDIOC_STREAMON, &type) == -1) {
perror("Start Capture");
return 1;
}
// 从队列中取缓冲区
if (ioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
perror("Retrieving Frame");
return 1;
}
// 保存图像到文件
file = fopen("frame.jpg", "wb");
if (file == NULL) {
perror("Cannot open image file");
return 1;
}
fwrite(buffer_start, buf.bytesused, 1, file);
fclose(file);
// 结束捕捉
if (ioctl(fd, VIDIOC_STREAMOFF, &type) == -1) {
perror("Stop Capture");
return 1;
}
// 解除映射
munmap(buffer_start, buf.length);
// 关闭设备
close(fd);
printf("Image captured and saved to frame.jpg\n");
return 0;
}
运行代码,运行结束后会保存图像至当前运行目录下。