举例说明如何在linux下检测摄像头设备具备的功能

假设摄像头设备文件为/dev/video1 ,下面是一个专门用于检测 /dev/video1 设备能力的简化程序。这个程序将打印出设备的所有能力、格式和其他相关信息,以帮助你了解设备支持的功能。

检测 /dev/video1 设备能力的程序

复制代码
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <android/log.h>

#define LOG_TAG "DeviceCapabilityCheck"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

// 定义一个宏来检查特定的能力是否被设置
#define CHECK_CAPABILITY(capability) \
    if (cap.capabilities & capability) { \
        LOGD(" - " #capability); \
    }

int xioctl(int fh, int request, void *arg) {
    int r;
    do {
        r = ioctl(fh, request, arg);
    } while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
    if (r == -1) {
        perror("ioctl failed");
        exit(EXIT_FAILURE);
    }
    return r;
}

void print_supported_pixel_formats(int fd) {
    struct v4l2_fmtdesc fmtdesc;
    memset(&fmtdesc, 0, sizeof(fmtdesc));
    fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; // 使用多平面类型

    LOGD("Supported pixel formats:");
    int index = 0;
    while (true) {
        fmtdesc.index = index++;
        if (ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == -1) {
            if (errno == EINVAL) break; // 没有更多的格式了
            perror("VIDIOC_ENUM_FMT failed");
            return;
        }
        LOGD(" - %s (0x%08x)", fmtdesc.description, fmtdesc.pixelformat);
    }
}

int main() {
    const char *dev_name = "/dev/video1";
    LOGD("Checking capabilities for device %s", dev_name);

    int fd = open(dev_name, O_RDWR);
    if (fd == -1) {
        perror("Cannot open device");
        return EXIT_FAILURE;
    }

    // 查询设备能力
    struct v4l2_capability cap;
    if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
        perror("Querying capabilities failed");
        close(fd);
        return EXIT_FAILURE;
    }

    LOGD("Device capabilities: 0x%08x", cap.capabilities);
    LOGD("Device bus info: %s", cap.bus_info);
    LOGD("Device driver: %s", cap.driver);
    LOGD("Device card: %s", cap.card);

    LOGD("Device capabilities detailed:");
    CHECK_CAPABILITY(V4L2_CAP_VIDEO_CAPTURE);
    CHECK_CAPABILITY(V4L2_CAP_VIDEO_CAPTURE_MPLANE); // 添加对多平面的支持检查
    CHECK_CAPABILITY(V4L2_CAP_VIDEO_OUTPUT);
    CHECK_CAPABILITY(V4L2_CAP_VIDEO_OVERLAY);
    CHECK_CAPABILITY(V4L2_CAP_VBI_CAPTURE);
    CHECK_CAPABILITY(V4L2_CAP_VBI_OUTPUT);
    CHECK_CAPABILITY(V4L2_CAP_SLICED_VBI_CAPTURE);
    CHECK_CAPABILITY(V4L2_CAP_SLICED_VBI_OUTPUT);
    CHECK_CAPABILITY(V4L2_CAP_RDS_CAPTURE);
    CHECK_CAPABILITY(V4L2_CAP_VIDEO_OUTPUT_OVERLAY);
    CHECK_CAPABILITY(V4L2_CAP_HW_FREQ_SEEK);
    CHECK_CAPABILITY(V4L2_CAP_RDS_OUTPUT);
    CHECK_CAPABILITY(V4L2_CAP_READWRITE);
    CHECK_CAPABILITY(V4L2_CAP_ASYNCIO);
    CHECK_CAPABILITY(V4L2_CAP_STREAMING);

    // 打印支持的像素格式
    print_supported_pixel_formats(fd);

    close(fd);
    return 0;
}

关键更改点

  1. 多平面类型

    • print_supported_pixel_formats 函数中,我们将 fmtdesc.type 设置为 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE 以匹配设备的能力。
  2. 错误处理

    • 在枚举格式时添加了更详细的错误处理,以便在遇到无效索引时停止枚举。
  3. 能力检查

    • 添加了对 V4L2_CAP_VIDEO_CAPTURE_MPLANE 的检查,以确认设备支持多平面视频捕获。
相关推荐
天朝八阿哥1 小时前
关于xfce4-pulseaudio-plugin中文翻译的bug
linux·debian
z202305081 小时前
linux之 remoteproc 内核实现源码分析
linux·运维·服务器
阿方索1 小时前
shell脚本
linux·运维
努力努力再努力wz2 小时前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
QMCY_jason2 小时前
ubuntu 24.04 FFmpeg编译 带Nvidia 加速记录
linux·ubuntu·ffmpeg
matlab的学徒2 小时前
Kubernetes(K8S)全面解析:核心概念、架构与实践指南
linux·容器·架构·kubernetes
Fcy6482 小时前
初识Linux和Linux基础指令详细解析及shell的运行原理
linux·服务器·ubuntu·centos
gb42152873 小时前
linux系统中如何在root用户中将某个文件夹目录的权限赋值给其它用户(主要说的是 方法 1)
linux
qq_339554823 小时前
linux串口驱动学习
linux
拾光Ծ3 小时前
【Linux】入门指南:基础指令详解Part Two
linux·运维·服务器