V4L2 获取和配置摄像头程序示例

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);
}

程序运行说明:
本程序是摄像头参数获取和配置的示例程序,所以系统必须带有摄像头驱动程序及摄像头传感器
程序参数说明:
不带任何参数,则显示摄像头可配置的参数,即其当前配置值

@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

带两个参数,第一个是要配置选项的名称,第二项为其对应的值。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/603cc7a19f6e26b0f0537afeae32f281.html