概述
UVC摄像头由于其免驱特性目前在各种场合都有大量的应用,本文仅根据个人开发UVC摄像头过程进行总结记录。
有很多博主写的很好,大家可以都看一看。
UVC简介
什么是UVC
UVC,全称为:USB video(device) class,是微软与另外几家设备厂商联合推出的为USB视频捕获设备定义的协议标准,目前已成为USB org标准之一。
使用 UVC 的好处是USB 在 Video这块也成为一项标准了之后,硬件在各个程序之间彼此运行会更加顺利,而且也省略了驱动程序安装这一环节,操作系统只要是 Windows XP SP2 之后的版本都可以支持 UVC,Linux系统自2.4以后的内核都支持了大量的设备驱动,其中支持UVC设备。
UVC摄像头开发:V4L2协议框架
V4L2 :video for linux version 2 ,是 Linux 里一套标准的视频驱动,是针对uvc免驱usb设备的编程框架,主要用于采集usb摄像头等。
V4L2:https://baike.so.com/doc/497647-526878.html
IO接口
在Linux编程中,一般使用ioctl函数来对设备的I/O通道进行管理。Linux系统查询可知接口描述如下:
NAME
ioctl - control device
SYNOPSIS
#include <sys/ioctl.h>
int ioctl(int d, unsigned long request, ...);
DESCRIPTION
The ioctl() function manipulates the underlying device parameters of special files. In particular, many operating characteristics of character special files (e.g., terminals) may be controlled with ioctl() requests. The argument d must be an open file descriptor.
The second argument is a device-dependent request code. The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion.
An ioctl() request has encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp in bytes. Macros and defines used in specifying an ioctl() request are located in the file <sys/ioctl.h>.
RETURN VALUE
Usually, on success zero is returned. A few ioctl() requests use the return value as an output parameter and return a nonnegative value on success. On error, -1 is returned, and errno is set appropriately.
ERRORS
EBADF d is not a valid descriptor.
EFAULT argp references an inaccessible memory area.
EINVAL request or argp is not valid.
ENOTTY d is not associated with a character special device.
ENOTTY The specified request does not apply to the kind of object that the descriptor d references.
这里给出摄像头一般控制指令和参数,如下:
/****************************************************************************************************
编号 | 描述 | 使用命令cmd | 主要参数结构体
1 | 查询驱动程序和硬件信息 | VIDIOC_QUERYCAP | struct v4l2_capability cap
2 | 枚举设备所支持的image_format | VIDIOC_ENUM_FMT | struct v4l2_fmtdesc
3 | 获取当前image_format | VIDIOC_G_FMT | struct v4l2_format
3.1 | 设置当前image_format | VIDIOC_S_FMT | struct v4l2_format
4 | 获取当前stream信息 | VIDIOC_G_PARM | struct v4l2_streamparm
4.1 | 设置当前stream信息 | VIDIOC_S_PARM | struct v4l2_streamparm /此处可以设置帧率信息
5 | 获取相机一般控制项 | VIDIOC_G_CTRL | struct v4l2_control
6 | 查询当前关键参数具体信息 | VIDIOC_QUERYCTRL | struct v4l2_queryctrl
7 | 设置相机一般控制项 | VIDIOC_S_CTRL | struct v4l2_control
struct v4l2_streamparm
{
enum v4l2_buf_type type;
union{
struct v4l2_captureparm capture;
struct v4l2_outputparm output;
_u8 raw_data[200];
}parm;
};
struct v4l2_captureparm
{
_u32 capability;//是否可以被timeperframe控制帧数 如可,则设置为:V4L2_CAP_TIMEPERFRAME
_u32 capturemode; //是否为高清模式。如是则设为:V4L2_MODE_HIGHQUALIT