使用V4L2获取摄像头的信息
V4L2全称是Video for Linux 2,通过它可以驱动摄像头。
在Ubuntu中,已经内置了V4L2,因此不需要安装多余的东西。在WRTnode中,如果你需要使用V4L2,需要这样:
1
Libraries ---> libv4l
然后直接上代码: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include <stdio.h>
#include <fcntl.h>
#include <linux/videodev2.h>
char videodevice[] = "/dev/video0";
void printFrameInterval(int fd, unsigned int fmt, unsigned int width, unsigned int height)
{
struct v4l2_frmivalenum frmival;
memset(&frmival,0,sizeof(frmival));
frmival.pixel_format = fmt;
frmival.width = width;
frmival.height = height;
while (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival) == 0)
{
if (frmival.type == V4L2_FRMIVAL_TYPE_DISCRETE)
printf("[%dx%d] %f fps\n", width, height, 1.0*frmival.discrete.denominator/frmival.discrete.numerator);
else
printf("[%dx%d] [%f,%f] fps\n", width, height, 1.0*frmival.stepwise.max.denominator/frmival.stepwise.max.numerator, 1.0*frmival.stepwise.min.denominator/frmival.stepwise.min.numerator);
frmival.index++;
}
}
int main()
{
//打开摄像头设备
int fd = -1;
fd = open (videodevice, O_RDWR, 0);
if(fd<0){
printf("failed to open\n");
return -1;
}
printf("打开摄像头设备成功,%s\n", videodevice);
//查询摄像头信息,VIDIOC_QUERYCAP
printf("------------\n");
struct v4l2_capability cap;
ioctl (fd, VIDIOC_QUERYCAP, &cap);
printf("Driver Name:%s\nCard Name:%s\nBus info:%s\nDriver Version:%u.%u.%u\n\n",cap.driver,cap.card,cap.bus_info,(cap.version>>16)&0XFF, (cap.version>>8)&0XFF,cap.version&0XFF);
//查询摄像头视频格式,VIDIOC_ENUM_FMT
printf("------------\n");
struct v4l2_fmtdesc fmtdesc;
fmtdesc.index=0;
fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
printf("Supportformat:\n");
while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1)
{
printf("\t%d.%c%c%c%c\t%s\n",fmtdesc.index+1,fmtdesc.pixelformat & 0xFF, (fmtdesc.pixelformat >> 8) & 0xFF,(fmtdesc.pixelformat >> 16) & 0xFF, (fmtdesc.pixelformat >> 24) & 0xFF,fmtdesc.description);
fmtdesc.index++;
}
v4l2_close(fd);
return 0;
}1
2gcc test.c -o test -lv4l2
./test
这里我获取了三个摄像头的信息。