我验证了一下这个解决方法。
把v4l.h头文件修改为v4l2.h,v4l2.h如下
#ifndef _V4L2_H_
#define _V4L2_H_
#include <stdio.h>
#include <stdlib.h> //stdio.h and stdlib.h are needed by perror function
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h> //O_RDWR
#include <unistd.h>
#include <sys/mman.h> //unistd.h and sys/mman.h are needed by mmap function
#include <stdbool.h>//false and true
#include <sys/ioctl.h>
#include <linux/videodev2.h>//v4l2 API
typedef struct _v4l2_struct
{
int fd;
struct v4l2_capability capability;
}v4l2_device;
extern int v4l2_open(char*,v4l2_device*);
extern int v4l2_close(v4l2_device*);
extern int v4l2_get_capability(v4l2_device*);
#define DEFAULT_DEVICE "/dev/video0"
int v4l2_open(char *dev,v4l2_device *vd)
{
if(!dev)
dev=DEFAULT_DEVICE;
if((vd->fd=open(dev,O_RDWR))<0)
{
perror("v4l2_open fail");
return -1;
}
if(v4l2_get_capability(vd))
return -1;
printf("video capture device name:%s\n",vd->capability.driver);
return 0;
}
int v4l2_close(v4l2_device *vd)
{
close(vd->fd);
return 0;
}
int v4l2_get_capability(v4l2_device *vd)
{
if(ioctl(vd->fd,VIDIOC_QUERYCAP,&vd->capability)<0)
{
perror("v4l2_get_capability fail");
return -1;
}
return 0;
}
#endif
把测试程序app-camera.c改为test.c,test.c如下
#include "v4l2.h"
v4l2_device vd;
void main()
{
v4l2_open(DEFAULT_DEVICE,&vd);
v4l2_close(&vd);
}
运行结果如图7所示。
图7
我得到了我想要的结果。