⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initvideo.c

📁 Linux下通过V4L实现视频采集
💻 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  "/mnt/cap.jpeg"   //抓图后图片的存放位置,自己定义即可 /*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");  fprintf(fp,"P6\n%d %d\n255\n",NTSC_WIDTH,NTSC_HEIGHT);  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 + -