?? cap.c
字號:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "v4l.h"#include "v4lctoh.h"
#define norm VIDEO_MODE_NTSC#define DEFAULT_FILE_NAME "/home/drgee/H264/captran/cap/cap.ppm" //抓圖后圖片的存放位置,自己定義即可 /*typedef struct _v4l_struct v4l_device;*/
v4l_device vd;
int device_grab_frame(){ vd.frame_current =0; if (v4l_grab_frame(&vd,0)<0) return -1; return 0;} int device_next_frame(){ vd.frame_current ^=1; if(v4l_grab_frame(&vd,vd.frame_current)<0) return -1; return 0;} unsigned char *device_get_address(){ return (vd.map +vd.mbuf.offsets[vd.frame_current]);}
int device_init(char *dev)
{ if (dev==NULL)
dev = "/dev/video0"; if (v4l_open(dev,&vd)!=0) // initial set 1 return -1; if(v4l_get_picture(&vd)!=0) // initial set 2 return -1; if(v4l_get_channels(&vd)!=0) // initial set 3 return -1; printf("%s:initialization OK...%s\n" "%d channels \n" "%d audios \n", dev,vd.capability.name,vd.capability.channels,vd.capability.audios); return 0; }
int main(){ FILE *fp; int frame=0; unsigned char *img=""; if (device_init("/dev/video0")==-1) { perror("device_init:failed ...."); exit(1); } else printf("device_init: OK_QianKe \n"); if(v4l_set_norm(&vd, norm)!=0) // initial set 5 return -1; else printf("v4l_set_norm: OK_QianKe \n"); if(v4l_mmap_init(&vd)!= 0) // improve mmap return -1; else printf("v4l_mmap_init: OK_QianKe \n"); if(v4l_grab_init(&vd,NTSC_WIDTH,NTSC_HEIGHT)!= 0) // improve grab_init return -1; else printf("v4l_grab_init: OK_QianKe \n"); if(v4l_grab_sync(&vd)!= 0) // improve sync return -1; else printf("v4l_grab_sync: OK_QianKe \n"); //above do initialization word /*for(frame;frame<=10;frame++) { if(v4l_grab_frame(&vd,frame)!= 0) return -1; else printf("v4l_grab_frame: OK_QianKe \n"); //sleep(2); }*/ if(device_grab_frame()!=0) return -1; else printf("v4l_grab_frame :OK_QianKe \n"); /*if(device_next_frame()!=0) return -1; else printf("v4l_grab_frame_next frame ready...: OK QianKe \n");*/ if(v4l_grab_sync(&vd)!=0) return -1; else printf("v4l_grab_sync : OK_QianKe \n"); img = device_get_address(); //返回所采集的數據的起始地址 printf("\nImage pointer is: %p\n",img);//輸出所采集的數據的起始地址 fp = fopen(DEFAULT_FILE_NAME,"w"); if(fp==NULL) printf("Cannot open this file!\n"); fprintf(fp,"P6\n%d %d\n255\n",NTSC_WIDTH,NTSC_HEIGHT);// printf("The answer.\n"); fwrite(img,NTSC_WIDTH,3*NTSC_HEIGHT,fp); fclose(fp); v4l_close(&vd); exit(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -