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

📄 xpic.c

📁 LINUX下编写的视频采集server端发送程序 用udp协议实现的
💻 C
字号:
#include <strings.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <stdarg.h>#include <linux/types.h>#include <linux/videodev.h>#include <sys/types.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/file.h>#include <sys/stat.h>#include <fcntl.h>#include <time.h>#include <unistd.h>#include <string.h> #include <netdb.h>#include <arpa/inet.h>#include <sys/times.h>#include <netinet/in.h>#include <sys/socket.h>#define VIDEO_PALETTE_JPEG 21   #define PORT 1234#define MAX 100    int main(int argc,char **argv){static int vf=0;int j=1;int get_jpegsize (unsigned char *buf, int insize);int  convertframe(unsigned char *dst,unsigned char *src,int size);int video_fd, status;FILE *fp;unsigned char *grab_data;unsigned char *grab_frame; clock_t oldtick,newtick; clock_t totalold,totalnew; float time = 0;struct video_capability grab_cap; int c; int width = 320,height = 240; int len,size,jpegsize; char *separateur;  static const char * short_options = "s:";struct video_picture grab_pic;struct video_mmap grab_map;struct video_mbuf grab_buf;int sockfd;struct sockaddr_in server;struct sockaddr_in client;int sin_size,num;char msg[MAX];loop: while((c = getopt(argc,argv,"s:h:")) != -1)   switch(c)     {     case 's':       width = strtoul (optarg,&separateur,10);       ++separateur;       height = strtoul (separateur,&separateur,10);       printf("width: %d height: %d \n",width,height);       break;     case 'h':       printf("no help!\n");     default:       break;     } totalold = clock();video_fd = open("/dev/video0", O_RDWR);if (video_fd == -1) {fprintf(stderr, "can not open video0");//\u6253\u5f00\u6444\u50cf\u5934exit(1);} oldtick = clock();if ((ioctl(video_fd, VIDIOCGCAP, &grab_cap)) < 0) {fprintf(stderr, "ioctl VIDEOCGCAP failed.");exit(1);} newtick = clock(); time = (double)(newtick - oldtick)/ CLOCKS_PER_SEC; printf("\n%f second is take to ioctl VIDEOCGCAP \n",time);printf("The VideoCap Name: %s\n", grab_cap.name);printf("The hannels: %d\n", grab_cap.channels);printf("The Audios: %d\n", grab_cap.audios);printf("The maxwidth: %d, maxheight: %d, minwidth %d, minheight: %d\n",grab_cap.maxwidth, grab_cap.maxheight, grab_cap.minwidth, grab_cap.minheight); oldtick = clock();if ((ioctl(video_fd, VIDIOCGPICT, &grab_pic)) < 0) {fprintf(stderr, "ioctl VIDIOCGPICT failed.");exit(1);} newtick = clock(); time = (double)(newtick - oldtick)/ CLOCKS_PER_SEC; printf("\n%f second is take to ioctl VIDIOCGPICT \n",time);printf("The brightness: %d\nThe hue: %d\nThe colour: %d\nThe contrast:%d\nThe whiteness: %d\nThe depth: %d\nThe palette: %d\n", grab_pic.brightness, grab_pic.hue, grab_pic.colour, grab_pic.contrast, grab_pic.whiteness, grab_pic.depth, grab_pic.palette); oldtick = clock();if ((ioctl(video_fd, VIDIOCGMBUF, &grab_buf)) < 0) {fprintf(stderr, "ioctl VIDIOCGMBUF, failed.");exit(1);} newtick = clock(); time = (double)(newtick - oldtick)/ CLOCKS_PER_SEC; printf("\n%f second is take to ioctl VIDIOCGMBUF \n",time);printf("The mapping size: %d\nThe mapping frames: %d\nThe mapping offset %d\n",grab_buf.size, grab_buf.frames, grab_buf.offsets);printf("The mapping size: %d\n",grab_buf.size);grab_map.width = width;grab_map.height = height;grab_map.format = VIDEO_PALETTE_JPEG;grab_map.frame = 0;size=(width * height);grab_data = (unsigned char*) malloc(grab_cap.maxwidth * grab_cap.maxheight * 3);grab_frame = (unsigned char*) malloc(grab_cap.maxwidth * grab_cap.maxheight * 3);if(vf==0){vf=vf+1;close(video_fd);goto loop;}if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1){perror("creat socket error");exit(1);}bzero(&server,sizeof(server));server.sin_family=AF_INET;server.sin_port=htons(PORT);server.sin_addr.s_addr=htonl(INADDR_ANY);if(bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1){perror("bind error");exit(1);}sin_size=sizeof(struct sockaddr_in);while(1){num=recvfrom(sockfd,msg,MAX,0,(struct sockaddr *)&client,&sin_size);if(num<0){perror("recv error");exit(1);}msg[num]='\0';while(1){len=read(video_fd,grab_data,size);jpegsize=convertframe(grab_frame,grab_data,size);printf("The %d jpegsize: %d\n",j,jpegsize);//printf("you got a message (%s) from %s\n",msg,inet_ntoa(client.sin_addr));sendto(sockfd,grab_frame,jpegsize,0,(struct sockaddr *)&client,sin_size);oldtick=clock();loop1: newtick=clock();time=(double)(newtick-oldtick)/CLOCKS_PER_SEC;if(time<0.02)goto loop1;j++;}if(!strcmp(msg,"quit"))break;}close(video_fd);close(sockfd);return 0;} //获得JPEG图片大小int get_jpegsize (unsigned char *buf, int insize){ int i; 	 for ( i= 1024 ; i< insize; i++) { 	if ((buf[i] == 0xFF) && (buf[i+1] == 0xD9)) return i+2; } return -1;}//把共享缓冲区中的数据放到一个变量中,通知系统已获得一帧int  convertframe(unsigned char *dst,unsigned char *src,int size){ 	int jpegsize =0;	jpegsize = get_jpegsize(src, size);	//if (jpegsize < 0) break;	memcpy(dst,src,jpegsize);	  return jpegsize;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -