1 #include <stdio.h> 2 #include <string.h> 3 #include <errno.h> 4 5 #include <sys/types.h> 6 #include <sys/stat.h> 7 #include <fcntl.h> 8 9 #include <linux/videodev2.h> 10 //#include <linux/videodev.h> 11 #include <libv4l1-videodev.h> 12 13 /* 14 0 -- 不是v4l设备 15 1 -- v4l 设备 16 2 -- v4l2 设备 17 */ 18 int test_v4l_version(int fd) 19 { 20 int ret = 0; 21 char dummy[256]; 22 23 if (-1 != ioctl(fd,VIDIOC_QUERYCAP,dummy)) { 24 ret = 2; 25 } 26 27 else if (-1 != ioctl(fd,VIDIOCGCAP,dummy)) { 28 29 ret = 1; 30 } 31 32 return ret; 33 } 34 35 int v4l_open(char *dev, v4l_device *vd) 36 { 37 if (!dev) 38 dev = ”/dev/video0”; 39 if ((vd ->fd = open(dev, O_RDWR)) < 0) { 40 perror("v4l_open:"); 41 return -1; 42 } 43 if (v4l_get_capability(vd)) 44 return -1; 45 if (v4l_get_picture(vd)) 46 retu rn -1; 47 return 0; 48 } 49 50 int v4l_get_capability(v4l_device *vd) 51 { 52 if (ioctl(vd ->fd, VIDIOCGCAP, &(vd->capability)) < 0) { 53 perror("v4l_get_capability:"); 54 return -1; 55 } 56 return 0; 57 } 58 59 60 int main(int argc,char * argv[]) 61 { 62 char dev_name[64] = "/dev/video0"; 63 int cam_fd =-1; 64 65 if(argc>1) 66 { 67 strncpy(dev_name,argv[1],sizeof(dev_name)-1); 68 } 69 70 printf("open device %s\n",dev_name); 71 cam_fd = open(dev_name,O_RDWR|O_NONBLOCK); 72 73 if(cam_fd == -1) 74 { 75 printf("open failure \n"); 76 return -1; 77 } 78 79 switch(test_v4l_version(cam_fd)) 80 { 81 case 0: 82 printf("%s:fd %d isn't v4l deivce\n",dev_name,cam_fd); 83 return -1; 84 break; 85 case 1: 86 printf("\n### video4linux device info [%s] ###\n",dev_name); 87 return -2; 88 break; 89 case 2: 90 printf("\n### v4l2 device info [%s] ###\n",dev_name); 91 break; 92 } 93 94 95 96 97 close(cam_fd); 98 99 return 0; 100 101 }
相关文章: