Save the camera image in JPG format. The camera needs to be GSPCA-free. If the UVC is used in the UVC format, insert the Huffman table in the image. Otherwise it will not display properly.
Program code:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<assert.h>#include<getopt.h>#include<fcntl.h>#include<unistd.h>#include<errno.h>#include<malloc.h>#include<sys/stat.h>#include<sys/types.h>#include<sys/time.h>#include<sys/mman.h>#include<sys/ioctl.h>#include<asm/types.h>#include<linux/videodev2.h>structBuffer {void*start; size_t length;}; structBuffer *buffers;unsignedLongn_buffers;unsignedLongfile_length;intfile_fd;Char*dev_name ="/dev/video0";intFD;Static intRead_frame (void){ structV4l2_buffer buf; /*Frame dequeue*/Buf.type=v4l2_buf_type_video_capture; Buf.memory=V4l2_memory_mmap; IOCTL (FD, VIDIOC_DQBUF,&buf); Write (file_fd,buffers[buf.index].start,buffers[buf.index].length); /*buf into Row*/IOCTL (FD, VIDIOC_QBUF,&buf); return 1;} intMain (intargcChar**argv) { structv4l2_capability Cap; structV4l2_format FMT; structv4l2_requestbuffers req; structV4l2_buffer buf; unsignedinti; enumV4l2_buf_type type; FILE_FD= Open ("test.jpg", O_RDWR | O_creat,0777); FD= Open (Dev_name, O_RDWR | O_nonblock,0); /*Get driver Information*/IOCTL (FD, Vidioc_querycap,&cap); printf ("Driver name:%s\n Card name:%s\n Bus info:%s\n\n", Cap.driver,cap.card,cap.bus_info); /*Formatting Images*/Fmt.type=v4l2_buf_type_video_capture; Fmt.fmt.pix.width= the; Fmt.fmt.pix.height= -; Fmt.fmt.pix.field=v4l2_field_interlaced; Fmt.fmt.pix.pixelformat=V4l2_pix_fmt_mjpeg; IOCTL (FD, VIDIOC_S_FMT,&FMT); /*Request an image buffer*/Req.count=4; Req.type=v4l2_buf_type_video_capture; Req.memory=V4l2_memory_mmap; IOCTL (FD, VIDIOC_REQBUFS,&req); Buffers= Calloc (Req.count,sizeof(*buffers)); for(N_buffers =0; N_buffers < Req.count; ++n_buffers) { /*get information about an image buffer*/Buf.type=v4l2_buf_type_video_capture; Buf.memory=V4l2_memory_mmap; Buf.index=n_buffers; IOCTL (FD, VIDIOC_QUERYBUF,&buf); Buffers[n_buffers].length=buf.length; //mapping image buffers in kernel space to user spaceBuffers[n_buffers].start = Mmap (NULL,//establishing mapping relationships through mmapBuf.length, Prot_read|prot_write, map_shared, FD, Buf.m.offset); } /*Queue Image Buffer*/ for(i =0; i < n_buffers; ++i) {buf.type=v4l2_buf_type_video_capture; Buf.memory=V4l2_memory_mmap; Buf.index=i; IOCTL (FD, VIDIOC_QBUF,&buf); } //Start capturing image dataType =v4l2_buf_type_video_capture; IOCTL (FD, Vidioc_streamon,&type); Fd_set FDS; Fd_zero (&FDS); Fd_set (FD,&FDS); Select(FD +1, &FDS, NULL, NULL, NULL); /*Read an image*/Read_frame (); for(i =0; i < n_buffers; ++i) Munmap (Buffers[i].start, buffers[i].length); Close (FD); Close (FILE_FD); printf ("Camera done.\n"); return 0;}
Ubuntu-linux system reads USB camera data (GSPCA)