CODE: typedef struct _v4ldevice {int fd;//設備號 struct video_capability capability; struct video_channel channel[10]; struct video_picture picture; struct video_clip clip; struct video_window window; struct video_capture capture; struct video_buffer buffer; struct video_mmap mmap; struct video_mbuf mbuf; struct video_unit unit; unsigned char *map;//mmap方式獲取數據時,數據的首地址 pthread_mutex_t mutex; int frame; int framestat[2]; int overlay; }v4ldevice; |
CODE: * struct video_capability name[32] Canonical name for this interface type Type of interface channels Number of radio/tv channels if appropriate audios Number of audio devices if appropriate maxwidth Maximum capture width in pixels maxheight Maximum capture height in pixels minwidth Minimum capture width in pixels minheight Minimum capture height in pixels |
CODE: int v4lgetcapability(v4ldevice *vd) { if(ioctl(vd->fd, VIDIOCGCAP, &(vd->capability)) < 0) { v4lperror("v4lopen:VIDIOCGCAP"); return -1; } return 0; } * struct video_picture brightness Picture brightness hue Picture hue (colour only) colour Picture colour (colour only) contrast Picture contrast whiteness The whiteness (greyscale only) depth The capture depth (may need to match the frame buffer depth) palette Reports the palette that should be used for this image |
CODE: * struct video_mbuf size The number of bytes to map frames The number of frames offsets The offset of each frame |
CODE: int v4lgetmbuf(v4ldevice *vd) { if(ioctl(vd->fd, VIDIOCGMBUF, &(vd->mbuf))<0) { v4lperror("v4lgetmbuf:VIDIOCGMBUF"); return -1; } return 0; } |
CODE: unsigned char *v4lgetaddress(v4ldevice *vd) { return (vd->map + vd->mbuf.offsets[vd->frame]); } |
CODE: char* devicename="/dev/video0"; char* buffer; v4ldevice device; int width = 640; int height = 480; int frame = 0; v4lopen("/dev/video0",&device);//打開設備 v4lgrabinit(&device,width,height);//初始化設備,定義獲取的影像的大小 v4lmmap(&device);//內存映射 v4lgrabstart(&device,frame);//開始獲取影像 while(1){ v4lsync(&device,frame);//等待傳完一幀 frame = (frame+1)%2;//下一幀的frame v4lcapture(&device,frame);//獲取下一幀 buffer = (char*)v4lgetaddress(&device);//得到這一幀的地址 //buffer給出了圖像的首地址,你可以選擇將圖像顯示或保存...... //圖像的大小為 width*height*3 .......................... } |
CODE: int v4lopen(char *name, v4ldevice *vd) { int i; if((vd->fd = open(name,O_RDWR)) < 0) { return -1; } if(v4lgetcapability(vd)) return -1; } int v4lgrabinit(v4ldevice *vd, int width, int height) { vd->mmap.width = width; vd->mmap.height = height; vd->mmap.format = vd->picture.palette; vd->frame = 0; vd->framestat[0] = 0; vd->framestat[1] = 0; return 0; } int v4lmmap(v4ldevice *vd) { if(v4lgetmbuf(vd)<0) return -1; if((vd->map = mmap(0, vd->mbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, vd->fd, 0)) < 0) { return -1; } return 0; } int v4lgrabstart(v4ldevice *vd, int frame) { vd->mmap.frame = frame; if(ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap)) < 0) { return -1; } vd->framestat[frame] = 1; return 0; } int v4lsync(v4ldevice *vd, int frame) { if(ioctl(vd->fd, VIDIOCSYNC, &frame) < 0) { return -1; } vd->framestat[frame] = 0; return 0; } int c4lcapture(v4ldevice *vd, int frame) { vd->mmap.frame = frame; if(ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap)) < 0) { return -1; } vd->framestat[frame] = 1; return 0; |
[火星人 ] 如何利用Video4Linux獲取攝像頭數據已經有465次圍觀