直接贴代码,编译工具用了qt
#include <errno.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <stdbool.h>
#include <math.h>
#include "thread"
#define TRUE 1
#define FALSE 0
#define MMAP_SIZE 4
class CameraPrivate;
class Camera : public QObject
{
Q_OBJECT
public:
explicit Camera(QObject *parent = nullptr);
~Camera();
void searchDevice();
bool running();
int open(const char *device_dir);
void close();
private:
friend class CameraPrivate;
QScopedPointer<CameraPrivate> p;
Q_SIGNALS:
void frame(QPixmap);
};
class CameraPrivate
{
public:
explicit CameraPrivate();
~CameraPrivate();
int fd; //设备描述符
struct v4l2_streamparm stream_para; //结构体v4l2_streamparm来描述视频流的属性
struct v4l2_capability cap; //取得设备的capability,看看设备具有什么功能,比如是否具有视频输入,或者音频输入输出等
struct v4l2_fmtdesc fmtdesc; //枚举设备所支持的image format: VIDIOC_ENUM_FMT
struct v4l2_format fmt; //子结构体struct v4l2_pix_format设置摄像头采集视频的宽高和类型:V4L2_PIX_FMT_YYUV V4L2_PIX_FMT_YUYV
struct v4l2_requestbuffers req; //向驱动申请帧缓冲的请求,里面包含申请的个数
struct v4l2_buffer buf; //代表驱动中的一帧
struct v4l2_control ctrl;
uint32_t width;
uint32_t height;
bool thread_exit;
std::thread camera_thread;
struct buffer //从相机获得的数据缓存
{
void * start;
unsigned int length;
long long int timestamp;
} *buffers;
void searchDevice();
int v4l2_init(const char *device_dir);
int v4l2_grab();
int v4l2_running();
int v4l2_control(const __u32 &id, const __s32 &value);</

该博客详细介绍了如何在Linux环境下使用V4L2 API与摄像头设备进行交互,包括打开设备、查询设备能力、设置图像格式、控制帧率、内存映射及帧捕获等操作。同时,它展示了如何将这些操作集成到一个Qt应用程序中,实现实时视频流的捕获和显示。
最低0.47元/天 解锁文章
4158

被折叠的 条评论
为什么被折叠?



