一 获取摄像头的能力

1 代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>

#define CLEAR(x) memset(&(x), 0, sizeof(x))

void print_capability(struct v4l2_capability cap) {
    printf("Driver: \"%s\"\n", cap.driver);
    printf("Card: \"%s\"\n", cap.card);
    printf("Bus info: \"%s\"\n", cap.bus_info);
    printf("Version: %d.%d\n", (cap.version >> 16) & 0xff, (cap.version >> 24) & 0xff);
    printf("Capabilities: %08x\n", cap.capabilities);
}

void print_formats(int fd) {
    struct v4l2_fmtdesc fmt;
    CLEAR(fmt);
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    printf("Supported formats:\n");
    while (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) == 0) {
        printf("  Index: %d\n", fmt.index);
        printf("  Type: %s\n", (fmt.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ? "Video Capture" : "Other");
        printf("  Description: '%s'\n", fmt.description);
        printf("  Pixelformat: %c%c%c%c\n",
               (fmt.pixelformat >>  0) & 0xff,
               (fmt.pixelformat >>  8) & 0xff,
               (fmt.pixelformat >> 16) & 0xff,
               (fmt.pixelformat >> 24) & 0xff);
        fmt.index++;
    }
}

void print_controls(int fd) {
    struct v4l2_queryctrl qc;
    CLEAR(qc);
    qc.id = V4L2_CID_BASE;
    while (ioctl(fd, VIDIOC_QUERYCTRL, &qc) == 0) {
        printf("Control: %s (%s)\n", qc.name, (qc.flags & V4L2_CTRL_FLAG_DISABLED) ? "disabled" : "enabled");
        printf("  Minimum: %d\n", qc.minimum);
        printf("  Maximum: %d\n", qc.maximum);
        printf("  Step: %d\n", qc.step);
        printf("  Default: %d\n", qc.default_value);
        printf("  Type: %s\n", (qc.type == V4L2_CTRL_TYPE_INTEGER) ? "Integer" :
               (qc.type == V4L2_CTRL_TYPE_BOOLEAN) ? "Boolean" :
               (qc.type == V4L2_CTRL_TYPE_MENU) ? "Menu" :
               (qc.type == V4L2_CTRL_TYPE_BUTTON) ? "Button" : "Unknown");
        qc.id++;
    }
}

int main() {
    int fd = open("/dev/video0", O_RDWR);
    if (fd == -1) {
        perror("Cannot open device");
        return 1;
    }

    struct v4l2_capability cap;
    if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
        perror("VIDIOC_QUERYCAP");
        close(fd);
        return 1;
    }

    print_capability(cap);
    print_formats(fd);
    print_controls(fd);

    close(fd);
    return 0;
}

2 输出log

Driver: "uvcvideo"
Card: "Integrated_Webcam_HD: Integrate"
Bus info: "usb-0000:00:14.0-5"
Version: 5.0
Capabilities: 84a00001
Supported formats:
  Index: 0
  Type: Video Capture
  Description: 'Motion-JPEG'
  Pixelformat: MJPG
  Index: 1
  Type: Video Capture
  Description: 'YUYV 4:2:2'
  Pixelformat: YUYV
Control: Brightness (enabled)
  Minimum: -64
  Maximum: 64
  Step: 1
  Default: 0
  Type: Integer
Control: Contrast (enabled)
  Minimum: 0
  Maximum: 95
  Step: 1
  Default: 0
  Type: Integer
Control: Saturation (enabled)
  Minimum: 0
  Maximum: 100
  Step: 1
  Default: 64
  Type: Integer
Control: Hue (enabled)
  Minimum: -2000
  Maximum: 2000
  Step: 1
  Default: 0
  Type: Integer

3 参数分析

Capabilities: 84a00001
每个内核版本都有自己的掩码

  1. 亮度(Brightness):调整图像的整体亮度水平。
  2. 对比度(Contrast):调整图像中亮部和暗部之间的差异程度。
  3. 饱和度(Saturation):调整图像中颜色的鲜艳程度。
  4. 色调(Hue):调整图像中颜色的整体偏向,如红色调、绿色调等。
  5. 锐度(Sharpness):调整图像边缘的清晰度,使图像看起来更加锐利。
  6. 曝光(Exposure):调整图像传感器的曝光时间,以控制图像的亮度。
  7. 白平衡(White Balance):调整图像中红、绿、蓝三原色的比例,以达到正确的色彩平衡。
  8. 增益(Gain):调整图像传感器的增益,以改变图像的亮度。
  9. 缩放(Zoom):调整图像的放大比例,以改变图像的视野范围。
  10. 旋转(Rotation):将图像旋转一定的角度,以改变图像的显示方向。