#includeunistd.h#includestdio.h#includestdlib.h#includestring.h#includepthread.h#includesys/types.h#includesys/stat.h#includesys/ioctl.h#includesys/mman.h#includefcntl.h#includeerrno.h#includelinux/ioctl.h#includelinux/types.h#includelinux/videodev2.h#includesetjmp.h#includecamera.h#includejpeglib.h#includemutexs.h#defineTRUE1#defineFALSE0#defineYUVimage_yuv.yuv#defineJPEGimage_jpeg.jpg#defineIMAGEWIDTH640#defineIMAGEHEIGHT480staticintcamerafd;staticstructv4l2_capabilitycap;structv4l2_fmtdescfmtdesc;structv4l2_formatfmt,fmtack;structv4l2_streamparmsetfps;structv4l2_requestbuffersreq;structv4l2_bufferbuf;enumv4l2_buf_typetype;staticunsignedcharframe_buffer[IMAGEWIDTH*IMAGEHEIGHT*3];structbuffer{void*start;unsignedintlength;}*buffers;intinit_v4l2(void){inti;intret=0;if(-1==(camerafd=open(/dev/video0,O_RDWR))){perror(opendev);return(FALSE);}if(-1==ioctl(camerafd,VIDIOC_QUERYCAP,&cap)){perror(VIDIOC_QUERYCAP);return(FALSE);}fmtdesc.index=0;fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;while(ioctl(camerafd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1){fmtdesc.index++;}fmt.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;fmt.fmt.pix.width=IMAGEWIDTH;fmt.fmt.pix.height=IMAGEHEIGHT;fmt.fmt.pix.pixelformat=V4L2_PIX_FMT_YUYV;fmt.fmt.pix.field=V4L2_FIELD_INTERLACED;if(ioctl(camerafd,VIDIOC_S_FMT,&fmt)==-1){perror(VIDIOC_S_FMT);return(FALSE);}if(ioctl(camerafd,VIDIOC_G_FMT,&fmt)==-1){perror(VIDIOC_G_FMT);return(FALSE);}return(TRUE);}intv4l2_grab(void){unsignedintn_buffers;req.count=4;req.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;req.memory=V4L2_MEMORY_MMAP;if(ioctl(camerafd,VIDIOC_REQBUFS,&req)==-1){perror(VIDIOC_REQBUFS);}buffers=calloc(req.count,sizeof(req));if(!buffers){printf(Outofmemory\n);return(FALSE);}for(n_buffers=0;n_buffersreq.count;n_buffers++){bzero(&buf,sizeof(buf));buf.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;buf.memory=V4L2_MEMORY_MMAP;buf.index=n_buffers;if(-1==ioctl(camerafd,VIDIOC_QUERYBUF,&buf)){perror(VIDIOC_QUERYBUF);exit(EXIT_FAILURE);}buffers[n_buffers].length=buf.length;buffers[n_buffers].start=mmap(NULL,buf.length,PROT_READ|PROT_WRITE,MAP_SHARED,camerafd,buf.m.offset);if(buffers[n_buffers].start==MAP_FAILED){perror(start);return(FALSE);}}if(-1==ioctl(camerafd,VIDIOC_QBUF,&buf)){perror(VIDIOC_QBUF);exit(EXIT_FAILURE);}}intcamera_start_stop(cam_flagflag){type=V4L2_BUF_TYPE_VIDEO_CAPTURE;if(Start==flag){if(-1==ioctl(camerafd,VIDIOC_STREAMON,&type)){perror(VIDIOC_STREAMON);exit(EXIT_FAILURE);}}elseif(Stop==flag){if(-1==ioctl(camerafd,VIDIOC_STREAMOFF,&type)){perror(VIDIOC_STREAOFF);}}elsereturn-1;return0;}intcamera_get(void){if(-1==ioctl(camerafd,VIDIOC_DQBUF,&buf)){perror(VIDIOC_DQBUF);returnFALSE;}FILE*fd=fopen(pic.yuyv,w);if(NULL==fd){perror(FOPEN);exit(EXIT_FAILURE);}fwrite(buffers[buf.index].start,buffers[buf.index].length,1,fd);printf(grabyuyvok\n);//yuyv_2_rgb();//encode_jpeg(frame_buffer);if(ioctl(camerafd,VIDIOC_QBUF,&buf)==-1){return-1;}return0;}intyuyv_2_rgb(void){inti,j;unsignedchary1,y2,u,v;intr1,g1,b1,r2,g2,b2;char*pointer;pointer=buffers[0].start;for(i=0;iIMAGEHEIGHT;i++){for(j=0;jIMAGEWIDTH/2;j++){y1=*(pointer+(i*IMAGEWIDTH/2+j)*4);u=*(pointer+(i*IMAGEWIDTH/2+j)*4+1);y2=*(pointer+(i*IMAGEWIDTH/2+j)*4+2);v=*(pointer+(i*IMAGEWIDTH/2+j)*4+3);r1=y1+1.370705*(v-128);g1=y1-0.337633*(u-128)-0.698001*(v-128);b1=y1+1.732446*(u-128);r2=y2+1.370705*(v-128);g2=y2-0.337633*(u-128)-0.698001*(v-128);b2=y2+1.732446*(u-128);if(r1250)r1=255;elseif(r110)r1=0;if(b1250)b1=255;elseif(b110)b1=0;if(g1250)g1=255;elseif(g110)g1=0;if(r2250)r2=255;elseif(r210)r2=0;if(b2250)b2=255;elseif(b210)b2=0;if(g2250)g2=255;elseif(g210)g2=0;*(frame_buffer+(i*IMAGEWIDTH/2+j)*6)=(unsignedchar)b1;*(frame_buffer+(i*IMAGEWIDTH/2+j)*6+1)=(unsignedchar)g1;*(frame_buffer+(i*IMAGEWIDTH/2+j)*6+2)=(unsignedchar)r1;*(frame_buffer+(i*IMAGEWIDTH/2+j)*6+3)=(unsignedchar)b2;*(frame_buffer+(i*IMAGEWIDTH/2+j)*6+4)=(unsignedchar)g2;*(frame_buffer+(i*IMAGEWIDTH/2+j)*6+5)=(unsignedchar)r2;}}return0;}intencode_jpeg(char*lpbuf){structjpeg_compress_structcinfo;structjpeg_error_mgrjerr;JSAMPROWrow_pointer[1];introw_stride;char*buf=NULL;intx;FILE*fptr_jpg=fopen(JPEG,wb);if(fptr_jpg==NULL){perror(fopen);return(FALSE);}cinfo.err=jpeg_std_error(&jerr);jpeg_create_compress(&cinfo);jpeg_stdio_dest(&cinfo,fptr_jpg);jpeg_mem_dest(&cinfo,&jpeg.jpeg_buf,&jpeg.jpeg_size);cinfo.image_width=IMAGEWIDTH;cinfo.image_height=IMAGEHEIGHT;cinfo.input_components=3;cinfo.in_color_space=JCS_RGB;jpeg_set_defaults(&cinfo);jpeg_set_quality(&cinfo,80,TRUE);jpeg_start_compress(&cinfo,TRUE);row_stride=IMAGEWIDTH*3;buf=malloc(row_stride);row_pointer[0]=buf;while(cinfo.next_scanlineIMAGEHEIGHT){for(x=0;xrow_stride;x+=3){buf[x]=lpbuf[x];buf[x+1]=lpbuf[x+1];buf[x+2]=lpbuf[x+2];}jpeg_write_scanlines(&cinfo,row_pointer,1);lpbuf+=row_stride;}jpeg_finish_compress(&cinfo);jpeg_destroy_compress(&cinfo);free(buf);return0;}intclose_v4l2(intbuf_num){inti;for(i=0;ibuf_num;i++){if(-1==munmap(buffers[i].start,buffers[i].length)){exit(EXIT_FAILURE);}}free(jpeg.jpeg_buf);//close(camerafd);free(bu