LInux使用V4L2打开UVC摄像头

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

直接贴代码,编译工具用了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);</
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值