V4L2 Camera Program

Source: Internet
Author: User

Since the completion of the use of camera cameras, and then check the Internet, and finally a slight modification to the current time to save as. jpeg format of the picture, and finally slightly added some of their own small understanding, to share with you.

--------------------------------------------------------------------------------------------------------------- --------------------

the camera used is ZC30. 1P, has been loaded into the kernel, the output format is JPEG, size is 640*480, so the image size after the calculation of the time is 640*480*3.


CAM.C:

#include <fcntl.h> #include <stdlib.h> #include <sys/mman.h> #include <linux/videodev2.h>#  Include <time.h> #include <stdio.h> #include <string.h>int main () {//////add by Liuzj time_t T;  struct TM *tmp;  T = time (NULL);  TMP = localtime (&t);  Char buffer1[30];  Char path[30];  End int FD = open ("/dev/video0", O_RDWR);  printf ("Lzj------->>>fd is%d\n", FD);  struct v4l2_capability cap;  IOCTL (FD,VIDIOC_QUERYCAP,&AMP;CAP); printf ("Lzj---------->>>>>driver name:%s\ncard name:%s\nbus info:%s\n", Cap.driver,cap.card,cap.bus  _info);  struct V4L2_FMTDESC fmtdesc;   Fmtdesc.index = 0;  Fmtdesc.type = v4l2_buf_type_video_capture; while (IOCTL (FD,VIDIOC_ENUM_FMT,&AMP;FMTDESC)! =-1) {printf ("Lzj-------->>>>>fmtdesc.description is   %s\n ", fmtdesc.description);  Fmtdesc.index + +;  }//////struct V4l2_format fmt;  Fmt.type = v4l2_buf_type_video_capture;  IOCTL (FD,VIDIOC_G_FMT,&AMP;FMT); Printf("Lzj----------->>>>>fmt.fmt.width is%d\nfmt.fmt.pix.height are%d\nfmt.fmt.pix.colorspace is%d\n",  Fmt.fmt.pix.width,fmt.fmt.pix.height,fmt.fmt.pix.colorspace);  struct V4l2_requestbuffers req;  Req.count = 4;  Req.type = v4l2_buf_type_video_capture;  Req.memory = V4l2_memory_mmap;  IOCTL (FD,VIDIOC_REQBUFS,&AMP;REQ);    struct buffer {void *start;  unsigned int length;  }*buffers;  buffers = (struct buffer*) calloc (req.count, sizeof (*buffers));  unsigned int n_buffers = 0;    for (n_buffers = 0; n_buffers < Req.count; ++n_buffers) {struct V4l2_buffer buf;    memset (&buf,0,sizeof (BUF));    Buf.type = v4l2_buf_type_video_capture;    Buf.memory = V4l2_memory_mmap;    Buf.index = n_buffers;      if (IOCTL (fd,vidioc_querybuf,&buf) = =-1) {printf ("Lzj---------_>>>>>>error\n");      Close (FD);    Exit (-1);    } buffers[n_buffers].length = Buf.length; Buffers[n_buffers].start = Mmap (null,buf.length,prot_read| Prot_write,map_shAred,fd,buf.m.offset);      if (map_failed = = Buffers[n_buffers].start) {printf ("Lzj--------__>>>>>error 2\n");      Close (FD);    Exit (-1);  }}////unsigned int i;  Enum V4l2_buf_type type;    for (i = 0; i < 4; i++) {struct V4l2_buffer buf;    Buf.type = v4l2_buf_type_video_capture;    Buf.memory = V4l2_memory_mmap;    Buf.index = i;  IOCTL (FD,VIDIOC_QBUF,&AMP;BUF);  } type = V4l2_buf_type_video_capture;  IOCTL (Fd,vidioc_streamon,&type);  struct V4l2_buffer buf;  Buf.type = v4l2_buf_type_video_capture;  Buf.memory = V4l2_memory_mmap;  IOCTL (FD,VIDIOC_DQBUF,&AMP;BUF);  Add by Liuzj strftime (buffer1, sizeof (Buffer1), "%y-%m-%d_%h-%m.jpeg", TMP); snprintf (path,sizeof (path), "/data/%s", buffer1); Modify by Liuzj int fdyuyv = open (path,o_wronly|  o_creat,00700);  printf ("\nlzj--------->>>>fdyuyv is%d\n", fdyuyv);  int resultyuyv = write (fdyuyv,buffers[buf.index].start,640*480*3); printf ("Lzj--------->>>resultyuyv is%d\n", RESULTYUYV);  Close (FDYUYV);  Close (FD); return 0;}


After the cross-compilation is done, you can move to the board and run directly.

/apps;: pic

Lzj------->>>FD is 3

LZJ---------->>>>>driver name:zc3xx

Card name:pc Camera

Bus info:usb-s3c24xx-1.1

LZJ-------->>>>>fmtdesc.description is JPEG

Lzj----------->>>>>fmt.fmt.width is 640

Fmt.fmt.pix.height is 480

Fmt.fmt.pix.colorspace is 7  

LZJ--------->>>>fdyuyv is 4

LZJ--------->>>resultyuyv is 118784

Will print out the format of my webcam support and the relevant parameters, and then go to my directory to find my image, and then use the TFTP command to upload it to the computer to view.

/apps;: cd/data/

/data;: LS

2016-05-09 12-47.jpeg

/date: TFTP-PR 2016-05-09 12-47.jpeg 10.228.26.4




And then I put a little bit of my note 1. Open camera int fd = open ("/dev/video0", O_RDWR); 2. Capturing the camera capability and attributes ioctl (FD,VIDIOC_QUERYCAP,&AMP;CAP); 1. View the video formats that are supported by the current webcam ioctl (FD,VIDIOC_ENUM_FMT,&AMP;FMTDESC) 2. View Video Frame format ioctl (FD,VIDIOC_G_FMT,&AMP;FMT);//Read the current drive's capture format 5. Apply buffer frame ioctl to the driver (fd,vidioc_reqbufs,&req);// Request buffer frame to drive Req.count = 4;//Request 4 Buffers req.memory = v4l2_memory_mmap;//settings use MMAP to access memory 6. Request physical memory and make a memory map maps the requested frame buffer to the user space, This allows you to manipulate the captured frames directly, without having to replicate them.    The requested frame buffers are all queued to hold the collected data and are mapped using MMAP.     struct buffer {void *start;    unsigned int length; }*buffers;buffers = (struct buffer*) calloc (req.count, sizeof (*buffers)); for (n_buffers = 0; n_buffers < Req.count; ++n    _buffers) {struct V4l2_buffer buf;    memset (&buf,0,sizeof (BUF));    Buf.type = v4l2_buf_type_video_capture;    Buf.memory = V4l2_memory_mmap;    Buf.index = n_buffers;      if (IOCTL (fd,vidioc_querybuf,&buf) = =-1) {printf ("Lzj---------_>>>>>>error\n");      Close (FD);    Exit (-1);    } buffers[n_buffers].length = Buf.length; Buffers[n_bufFers].start = Mmap (null,buf.length,prot_read|    Prot_write,map_shared,fd,buf.m.offset); if (map_failed = = Buffers[n_buffers].start) {printf ("Lzj--------__>>>>>error 2\n");    7. Start video transmission for (i = 0; i < 4; i++) {struct V4l2_buffer buf;    Buf.type = v4l2_buf_type_video_capture;    Buf.memory = V4l2_memory_mmap;    Buf.index = i;  IOCTL (FD,VIDIOC_QBUF,&AMP;BUF);  }//puts 4 buffers into the input queue and Vidioc_qbuf controls commands for them.  Type = V4l2_buf_type_video_capture; IOCTL (fd,vidioc_streamon,&type);//start video capture 8. Capture an image from the output queue of the video buffer to get a video buffer that has already saved a frame of video data. struct V4l2_buffer buf;buf.type = V4l2_buf_type_video_capture;buf.memory = V4l2_memory_mmap;ioctl (Fd,VIDIOC_DQBUF, &AMP;BUF)///from the buffer of the video to get a buffer data into the BUF, vidioc_dqbuf for its control command. 9. Save the captured image to the current time strftime (Buffer1, sizeof (Buffer1), "%y-%m-%d_%h-%m.jpeg", TMP), snprintf (path,sizeof (path), "/ data/%s ", buffer1); int fdyuyv = open (path,o_wronly| o_creat,00700); int resultyuyv = write (fdyuyv,buffers[buf.index].start,640*480*3); close (FDYUYV); close (FD);










V4L2 Camera Program

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.