linux_cam_test文件
相机测试:
1 #include <stdio.h> 2 #include <string.h> 3 #include <errno.h> 4 #include <stdlib.h> 5 #include <sys/types.h> 6 #include <sys/stat.h> 7 #include <fcntl.h> 8 #include <time.h> 9 #include <sys/mman.h> 10 #include <assert.h> 11 #include <linux/videodev2.h> 12 #include <linux/fb.h> 13 14 15 #define CLEAR(x) memset(&(x), 0, sizeof(x)) 16 typedef unsigned char BYTE; 17 typedef unsigned short WORD; 18 typedef unsigned int DWORD; 19 typedef long LONG; 20 21 #define CHECKNUM 8 22 23 struct { 24 unsigned int type; 25 char *name; 26 } enum_fmt[]={ 27 {V4L2_CAP_VIDEO_CAPTURE, "V4L2_CAP_VIDEO_CAPTURE"}, 28 {V4L2_CAP_VIDEO_OUTPUT, "V4L2_CAP_VIDEO_OUTPUT"}, 29 {V4L2_CAP_VIDEO_OVERLAY, "V4L2_CAP_VIDEO_OVERLAY"}, 30 {V4L2_CAP_VIDEO_CAPTURE_MPLANE, "V4L2_CAP_VIDEO_CAPTURE_MPLANE"}, 31 {V4L2_CAP_VIDEO_OUTPUT_MPLANE, "V4L2_CAP_VIDEO_OUTPUT_MPLANE"}, 32 {V4L2_CAP_VIDEO_M2M_MPLANE, "V4L2_CAP_VIDEO_M2M_MPLANE"}, 33 {V4L2_CAP_VIDEO_M2M, "V4L2_CAP_VIDEO_M2M"}, 34 {V4L2_CAP_STREAMING, "V4L2_CAP_STREAMING"}, 35 }; 36 37 38 39 int open_camer_device(char *path) 40 { 41 int fd; 42 43 if((fd = open(path,O_RDWR | O_NONBLOCK)) < 0) 44 { 45 perror("Fail to open"); 46 exit(EXIT_FAILURE); 47 } 48 49 printf("open cam:%s success %d\n",path, fd); 50 return fd; 51 } 52 53 void enum_video_ctrls_and_menus(int fd){ 54 /* 55 \* To query the attributes of a control applications set the id field of a struct 56 \* v4l2_queryctrl and call the VIDIOC_QUERYCTRL ioctl with a pointer to this 57 \* structure. The driver fills the rest of the structure or returns an EINVAL 58 \* error code when the id is invalid. 59 \* 60 \* To enumerate controls call VIDIOC_QUERYCTRL with successive 61 \* id values starting from V4L2_CID_BASE up to and exclusive V4L2_CID_LASTP1, 62 \* or starting from V4L2_CID_PRIVATE_BASE until the driver returns EINVAL. 63 */ 64 struct v4l2_queryctrl queryctrl; /* Query Control structure as defined in <sys/videodev2.h> */ 65 struct v4l2_querymenu querymenu; /* Query Menu Structure as defined in <sys/videodev2.h> */ 66 67 fprintf(stdout,"Discovering controls:\n"); 68 69 70 memset (&queryctrl, 0, sizeof (queryctrl)); 71 for (queryctrl.id = V4L2_CID_BASE; queryctrl.id < V4L2_CID_LASTP1; queryctrl.id++) { 72 if ( ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl) == 0 ) { 73 /* Check to see if this control is permanently disabled and should be ignored by the application */ 74 if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) 75 continue; 76 /* We got a video control back */ 77 fprintf(stdout,"\nVIDIOC_QUERYCTRL(V4L2_CID_BASE+%d)\n", queryctrl.id-V4L2_CID_BASE); 78 fprintf(stdout," id: %d\n", queryctrl.id); 79 switch (queryctrl.type){ 80 case V4L2_CTRL_TYPE_INTEGER: 81 fprintf(stdout, " type: INTEGER\n"); 82 break; 83 case V4L2_CTRL_TYPE_BOOLEAN: 84 fprintf(stdout, " type: BOOLEAN\n"); 85 break; 86 case V4L2_CTRL_TYPE_MENU: 87 fprintf(stdout, " type: MENU\n"); 88 /* Additional information is required for menu controls, the name of menu items. 89 \* To query them applications set the id and index fields of struct v4l2_querymenu 90 \* and call the VIDIOC_QUERYMENU ioctl with a pointer to this structure. The driver 91 \* fills the rest of the structure or returns an EINVAL error code when the id or 92 \* index is invalid. Menu items are enumerated by calling VIDIOC_QUERYMENU with 93 \* successive index values from struct v4l2_queryctrl minimum (0) to maximum, inclusive. 94 */ 95 querymenu.id = queryctrl.id; 96 for (querymenu.index = queryctrl.minimum; querymenu.index < queryctrl.maximum; querymenu.index++){ 97 fprintf(stdout, " menu id:%d\n", querymenu.index); 98 fprintf(stdout, " menu name:%s\n", querymenu.name); 99 } 100 break; 101 case V4L2_CTRL_TYPE_BUTTON: 102 fprintf(stdout, " type: BUTTON\n"); 103 break; 104 } 105 fprintf(stdout," name: %s\n", queryctrl.name); 106 fprintf(stdout," minimum: %d\n", queryctrl.minimum); 107 fprintf(stdout," maximum: %d\n", queryctrl.maximum); 108 fprintf(stdout," step: %d\n", queryctrl.step); 109 fprintf(stdout," default_value: %d\n", queryctrl.default_value); 110 fprintf(stdout," flags: %d\n", queryctrl.flags); 111 } else { 112 113 if (errno == EINVAL) 114 continue; 115 116 perror("VIDIOC_QUERYCTRL"); 117 break; 118 } 119 120 } 121 122 } /* End of enum_video_ctrls_and_menus() */ 123 124 125 int main(int argc, char** argv) 126 { 127 int fd; 128 struct v4l2_fmtdesc fmt; 129 struct v4l2_capability cap; 130 struct v4l2_format stream_fmt; 131 struct v4l2_input input; 132 struct v4l2_control ctrl; 133 struct v4l2_streamparm stream; 134 int err; 135 int ret; 136 int i; 137 138 if (argc < 2) { 139 // printusage(argv[0]); 140 printf("please input path\n"); 141 return -1; 142 } 143 fd = open_camer_device(argv[1]); 144 145 146 147 for(i = 0; i < CHECKNUM; i++) 148 { 149 memset(&fmt,0,sizeof(fmt)); 150 fmt.index = 0; 151 fmt.type = enum_fmt[i].type; 152 //printf("enum_fmt:%s\n", enum_fmt[i].name); 153 while((ret = ioctl(fd,VIDIOC_ENUM_FMT,&fmt)) == 0) 154 { 155 fmt.index ++ ; 156 printf("%s:{pixelformat = %c%c%c%c},description = '%s'\n", 157 enum_fmt[i].name, 158 fmt.pixelformat & 0xff,(fmt.pixelformat >> 8)&0xff, 159 (fmt.pixelformat >> 16) & 0xff,(fmt.pixelformat >> 24)&0xff, 160 fmt.description); 161 } 162 } 163 printf("\n\n"); 164 enum_video_ctrls_and_menus(fd); 165 //查询视频设备支持的功能 166 ret = ioctl(fd,VIDIOC_QUERYCAP,&cap); 167 if(ret < 0){ 168 perror("FAIL to ioctl VIDIOC_QUERYCAP"); 169 //exit(EXIT_FAILURE); 170 } 171 //printf("capabilities:%08x\n", cap.capabilities); 172 printf("\ncheck the support capabilities\n"); 173 for(i = 0; i < CHECKNUM; i++) 174 { 175 if(cap.capabilities & enum_fmt[i].type) 176 printf("%s\n",enum_fmt[i].name); 177 } 178 printf("\n"); 179 if(!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) 180 { 181 printf("The Current device is not a video capture device\n"); 182 //exit(EXIT_FAILURE); 183 184 } 185 else 186 printf("The Current device is a video capture device\n"); 187 if(!(cap.capabilities & V4L2_CAP_STREAMING)) 188 { 189 printf("The Current device does not support streaming i/o\n"); 190 //exit(EXIT_FAILURE); 191 } 192 else 193 printf("The Current device support streaming i/o\n"); 194 195 return 0; 196 }