1. 程式人生 > >V4L2 獲取和配置攝像頭程式示例(使用v4l2_queryctrl )

V4L2 獲取和配置攝像頭程式示例(使用v4l2_queryctrl )

V4L2 獲取和配置攝像頭程式示例:

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

int fd;
const char *input_dev = "/dev/video0";
const char *qctrl_name = NULL;
int qctrl_value = 0;

struct v4l2_capability cap;
struct v4l2_queryctrl qctrl;

static void print_qctrl(struct v4l2_queryctrl *qctrl)
{
  struct v4l2_control ctrl;

  ctrl.id = qctrl->id;
  if (ioctl(fd, VIDIOC_G_CTRL, &ctrl) < 0) {
    perror("get ctrl failed");
    ctrl.value = -999;
  }

  printf("%-14s : id=%08x, type=%d, minimum=%d, maximum=%d\n"
         "\t\t value = %d, step=%d, default_value=%d\n", 
         qctrl->name, qctrl->id, qctrl->type, qctrl->minimum, qctrl->maximum,
         ctrl.value, qctrl->step, qctrl->default_value);
}
static void print_menu(struct v4l2_querymenu *menu)
{
  printf("\t %d : %s\n", menu->index, menu->name);
}
static int set_qctrl(struct v4l2_queryctrl *qctrl)
{
  struct v4l2_control ctrl;

  printf("set %s = %d\n", qctrl_name, qctrl_value);

  ctrl.id = qctrl->id;
  ctrl.value = qctrl_value;
  return ioctl(fd, VIDIOC_S_CTRL, &ctrl);
}
static void deal_qctrl(struct v4l2_queryctrl *qctrl)
{
  print_qctrl(qctrl);
  if (qctrl_name && !strcmp(qctrl_name, qctrl->name))
    set_qctrl(qctrl);
}

static void qctrl_get(int id)
{
    qctrl.id = id;
    if (ioctl(fd, VIDIOC_QUERYCTRL, &qctrl) == 0) {
      deal_qctrl(&qctrl);
      if (qctrl.type == V4L2_CTRL_TYPE_MENU) {
        int idx;
        struct v4l2_querymenu menu;
        for (idx = qctrl.minimum; idx <= qctrl.maximum; idx++) {
          menu.id = qctrl.id;
          menu.index = idx;
          if (ioctl(fd, VIDIOC_QUERYMENU, &menu)==0) {
            print_menu(&menu);
          }
        }
      }
    }
}

int main(int argc, char **argv)
{
  int ret, i;

  if (argc == 3) {
    qctrl_name = argv[1];
    qctrl_value = atoi(argv[2]);
  }

  fd = open(input_dev, O_RDWR);
  if (fd < 0) {
    perror("open video failed");
    return -1;
  }
  printf("open video '%s' success\n", input_dev);

  ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
  if (ret < 0) {
    perror("ioctl querycap");
    return -1;
  }
  
  if ((cap.capabilities &  V4L2_CAP_VIDEO_CAPTURE) == 0) {
    printf("video device donot support capture\n");
    return -1;
  }

  for (i = V4L2_CID_BASE; i < V4L2_CID_LASTP1; i++) {
    qctrl_get(i);
  }

  for (i = V4L2_CID_PRIVATE_BASE; i < V4L2_CID_PRIVATE_BASE+25; i++) {
    qctrl_get(i);
  }
 

  printf("close video\n");
  close(fd);
}

程式執行說明:
本程式是攝像頭引數獲取和配置的示例程式,所以系統必須帶有攝像頭驅動程式及攝像頭感測器
程式引數說明:
不帶任何引數,則顯示攝像頭可配置的引數,即其當前配置值

www.linuxidc.com @linuxidc$ ./a.out 
open video '/dev/video0' success
Brightness     : id=00980900, type=1, minimum=0, maximum=255
         value = 10, step=1, default_value=128
Contrast       : id=00980901, type=1, minimum=0, maximum=256
         value = 128, step=1, default_value=128
Gamma          : id=00980910, type=1, minimum=1, maximum=6
         value = 4, step=1, default_value=4
Auto Gain      : id=00980912, type=2, minimum=0, maximum=1
         value = 1, step=1, default_value=1
Light frequency filter : id=00980918, type=3, minimum=0, maximum=2
         value = 1, step=1, default_value=1
     0 : NoFliker
     1 : 50 Hz
     2 : 60 Hz
close video

帶兩個引數,第一個是要配置選項的名稱,第二項為其對應的值。

www.linuxidc.com @linuxidc$ ./a.out Brightness 50
open video '/dev/video0' success
Brightness     : id=00980900, type=1, minimum=0, maximum=255
         value = 10, step=1, default_value=128
set Brightness = 50
Contrast       : id=00980901, type=1, minimum=0, maximum=256
         value = 128, step=1, default_value=128
Gamma          : id=00980910, type=1, minimum=1, maximum=6
         value = 4, step=1, default_value=4
Auto Gain      : id=00980912, type=2, minimum=0, maximum=1
         value = 1, step=1, default_value=1
Light frequency filter : id=00980918, type=3, minimum=0, maximum=2
         value = 1, step=1, default_value=1
     0 : NoFliker
     1 : 50 Hz
     2 : 60 Hz
close video

程式原理說明:

本程式可簡要使用如下序列說明其執行過程:

1.   fd = open(input_dev, O_RDWR);    開啟 /dev/video0 裝置

2.  ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);  獲取攝像頭裝置的能力,例如照相、輸出、VBI、調頻什麼的。這裡只需照相能力

3. ioctl(fd, VIDIOC_QUERYCTRL, &qctrl);   通過id來枚舉出取攝像頭的queryctrl,這個結構體用來描述攝像頭的某個具體引數選項的(亮度、色度、曝光度什麼的), 包括id, 名稱、型別、極值和預設值。

4. ioctl(fd, VIDIOC_QUERYMENU, &menu);  如果上面獲取的queryctrl型別是選單型別的,可使用該步驟來列出所有的選單項名稱。 這一步不是必須的,只是為了讓人更清除的知道queryctrl中每個值代表著什麼含義。例如 "Light frequency filter" 這個選項的取值範圍是 0~2,   3個選單項說明其3個值的用途。

5. ioctl(fd, VIDIOC_S_CTRL, &ctrl) 和 ioctl(fd, VIDIOC_G_CTRL, &ctrl) 分別是設定和獲取引數項當前值的介面,只需指出要配置引數項的id即可。

-------------------------------------------------------------------------


以上幾個所涉及的介面體可在  /usr/include/linux/videodev2.h 檔案中找到。

/*
 *    C O N T R O L S
 */
struct v4l2_control {
    __u32             id;
    __s32             value;
};


/*  Used in the VIDIOC_QUERYCTRL ioctl for querying controls */
struct v4l2_queryctrl {
    __u32             id;
    enum v4l2_ctrl_type  type;
    __u8             name[32];    /* Whatever */
    __s32             minimum;    /* Note signedness */
    __s32             maximum;
    __s32             step;
    __s32             default_value;
    __u32                flags;
    __u32             reserved[2];
};

/*  Used in the VIDIOC_QUERYMENU ioctl for querying menu items */
struct v4l2_querymenu {
    __u32        id;
    __u32        index;
    __u8        name[32];    /* Whatever */
    __u32        reserved;
};

/*
 *    D R I V E R   C A P A B I L I T I E S
 */
struct v4l2_capability {
    __u8    driver[16];    /* i.e. "bttv" */
    __u8    card[32];    /* i.e. "Hauppauge WinTV" */
    __u8    bus_info[32];    /* "PCI:" + pci_name(pci_dev) */
    __u32   version;        /* should use KERNEL_VERSION() */
    __u32    capabilities;    /* Device capabilities */
    __u32    reserved[4];
};