Linux-based camera acquisition V4l2__linux

Source: Internet
Author: User
 The source code of the collective is as follows: 
#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 & lt;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> struc
        T buffer {void * start;
size_t length;
 
};
struct buffer *buffers;
unsigned long n_buffers;

unsigned long file_length;
int file_fd;
Char *dev_name = "/dev/video3";

int FD;
     
     static int read_frame (void) {struct V4l2_buffer buf;
     /* frame out/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 Row * *     IOCTL (FD, VIDIOC_QBUF, &buf);
return 1;
     int main (int argc,char * * argv) {struct v4l2_capability cap;
     struct V4l2_format fmt;
     struct V4l2_requestbuffers req; 
     struct V4l2_buffer buf;
     unsigned int i;
     
     
     Enum V4l2_buf_type type; FILE_FD = open ("Test.jpg", O_RDWR |
    
     O_creat, 0777); FD = open (Dev_name, O_RDWR |

     O_nonblock, 0);
      /* Get drive 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);
     /* Set Image format */fmt.type = v4l2_buf_type_video_capture;
     Fmt.fmt.pix.width = 320;
     Fmt.fmt.pix.height = 240;
     Fmt.fmt.pix.field = v4l2_field_interlaced;

     Fmt.fmt.pix.pixelformat = V4l2_pix_fmt_mjpeg;
      
     IOCTL (FD, VIDIOC_S_FMT, &fmt);
     /* Request 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 the information for the 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 space Buffers[n_buffers].start = mmap (NULL,//mapping relationship via MMAP) Buf.length, Prot_read |
                                        Prot_write, map_shared, FD,
     Buf.m.offset);
              /* Image buffering Team/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 data type = 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; }

The main frame above the stuff.

This is just a relatively simple process, for the actual operation we may be able to the camera data through the main controller, and then in the framebuffer to render. The specific block diagram is as follows:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.