Embedded topic: UVC camera

Source: Internet
Author: User

The so-called CMOS camera usually has a shit driver. If you want to apply the camera, you can use the UVC camera.

This class shows the UVC camera's various parameter settings. It should be noted that if the frame rate is not high, it is often caused by the exposure time being too long. This relationship is like this. The longer the exposure, the better the image imaging quality, but the frame rate naturally goes on.

 

#ifndef _UVC_CAMERA_H#define _UVC_CAMERA_Hclass UvcCamera{public:UvcCamera();~UvcCamera();int Open(const char* dev, int width, int height);void Close();int WaitForFrame(int ms);int ReadFrame(void* outbuf);private:int EnumFormats();int SetFrameRate(int fps);int SetOther();private:int m_fd;struct bufinfo{void *                  start;size_t                  length;};bufinfo m_bufs[10];int     m_bufcount;};#endif


 

# 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> # include "uvccamera. H "# define clear (x) memset (& (x), 0, sizeof (x) uvccam Era: uvccamera () {} uvccamera ::~ Uvccamera () {} int uvccamera: open (const char * Dev, int width, int height) {int ret, I; // open the device // m_fd = open (Dev, o_rdwr/* required */| o_nonblock, 0); m_fd = open (Dev, o_rdwr); If (m_fd <0) {printf ("failed to open capture device! \ N "); Return-1 ;}// set the frame rate enumformats (); // obtain the Camera Parameter struct v4l2_capability cap; ret = IOCTL (m_fd, vidioc_querycap, & Cap ); if (Ret <0) {printf ("failed to query capability (% d)", RET); Return-1 ;}// sets the image format struct v4l2_format v_fmt; clear (v_fmt); v_fmt.type = border; v_fmt.fmt.pix.width = width; v_fmt.fmt.pix.height = height; v_fmt.fmt.pix.pixelformat = border; v_fmt.fmt.pix.field = v4 L2_field_any; // v4l2_field_interlaced; ret = IOCTL (m_fd, vidioc_s_fmt, & v_fmt); If (Ret <0) {printf ("failed to set format (% d) \ n ", RET); Return-1;} ret = IOCTL (m_fd, vidioc_g_fmt, & v_fmt); If (Ret <0) {printf ("failed to set format (% d) \ n ", RET); Return-1;} printf (" real size: % d x % d \ n ", v_fmt.fmt.pix.width, v_fmt.fmt.pix.height); setother (); setframerate (15); // cout <"The Frame Rate:" <(parm-> par M. capture. timeperframe. numerator * 1000)/(parm-> parm. capture. timeperfr ame. denominator) <Endl; // imgdata_size = FMT. FMT. PIX. bytesperline * FMT. FMT. PIX. height; // calculate the image size // printf ("bytes per line: % d \ n", FMT. FMT. PIX. bytesperline); struct v4l2_requestbuffers v_req; clear (v_req); v_req.count = 4; v_req.type = reverse; v_req.memory = reverse; IOCTL (m_fd, reverse, & v_req); // Request Buffer. Count indicates the requested quantity. If (v_req.count <2) {printf ("insufficient buffer memory \ n"); Return-1;} m_bufcount = v_req.count; for (I = 0; I <m_bufcount; I ++) {struct v4l2_buffer v_buf; // a clear (v_buf) frame in the driver; v_buf.type = clear; v_buf.memory = v4l2_memory_mmap; v_buf.index = I; If (-1 = IOCTL (m_fd, vidioc_querybuf, & v_buf) // map user space {printf ("vidioc_querybuf error \ n ");} printf ("vbuf. length: % d \ N ", v_buf.length); void * PTR = MMAP (null/* start anywhere */, // create a v_ing relationship v_buf.length, prot_read | prot_write/* required */, map_shared/* Recommended */, m_fd, v_buf.m.offset); If (PTR = map_failed) {printf ("MMAP failed \ n");} else {m_bufs [I]. length = v_buf.length; m_bufs [I]. start = PTR ;}}for (I = 0; I <m_bufcount; I ++) {struct v4l2_buffer v_buf; clear (v_buf); v_buf.type = v4l2_buf_type_video_captur E; v_buf.memory = v4l2_memory_mmap; v_buf.index = I; If (-1 = IOCTL (m_fd, vidioc_qbuf, & v_buf )) // The requested buffer enters the queue {printf ("vidioc_qbuf failed \ n") ;}// start capturing image data Enum v4l2_buf_type v_type = v4l2_buf_type_video_capture; if (-1 = IOCTL (m_fd, vidioc_streamon, & v_type) {printf ("vidioc_streamon failed \ n"); Return-1;} return 0;} void uvccamera :: close () {int I; for (I = 0; I <m_bufcount; ++ I) {If (-1 = munmap (m_ B Ufs [I]. start, m_bufs [I]. length) {printf ("munmap error") ;}} close (m_fd) ;}int uvccamera: setother () {printf ("......... exposure ............ \ n "); if (1) {struct v4l2_control CTRL; ctrl. id = v4l2_cid_exposure_auto; If (0! = IOCTL (m_fd, vidioc_g_ctrl, & CTRL) {printf ("failed .... \ n "); Return-1;} printf (" exposure mode (old) = % d \ n ", ctrl. value); ctrl. value = v4l2_exposure_manual; If (0! = IOCTL (m_fd, vidioc_s_ctrl, & CTRL) {printf ("failed to set .... \ n "); Return-1 ;}}if (1) {struct v4l2_control CTRL; ctrl. id = v4l2_cid_exposure_absolute; If (0! = IOCTL (m_fd, vidioc_g_ctrl, & CTRL) {return-1;} printf ("exposure time (old) = % d \ n", ctrl. value); ctrl. value = 50; If (0! = IOCTL (m_fd, vidioc_s_ctrl, & CTRL) {return-1;} If (0! = IOCTL (m_fd, vidioc_g_ctrl, & CTRL) {return-1;} printf ("exposure time (new) = % d \ n", ctrl. value) ;}if (0) {v4l2_control CTRL = {0}; ctrl. id = v4l2_cid_exposure; ctrl. value = 0; If (IOCTL (m_fd, vidioc_g_ctrl, & CTRL) <0) {printf ("v4l2_cid_exposure failed. \ n "); Return-1;} printf (" v4l2_cid_exposure = % d \ n ", ctrl. value);} If (0) {struct v4l2_ext_controls ctrls = {0}; struct v4l2_ext_control CTRL = {0}; ctrls. ctrl_class = v4l2_ctrl_class_camera; ctrls. count = 0; ctrls. controls = & CTRL; ctrl. id = v4l2_cid_exposure_auto; ctrl. value = 0; If (IOCTL (m_fd, vidioc_g_ext_ctrls, & ctrls) <0) {printf ("vidioc_g_ext_ctrls failed. \ n "); Return-1;} printf (" v4l2_cid_exposure_auto = % d \ n ", ctrl. value); printf ("------- set ------ \ n"); ctrl. id = v4l2_cid_exposure_auto; ctrl. value = v4l2_exposure_shutter_priority; If (IOCTL (m_fd, vidioc_s_ext_ctrls, & ctrls) <0) {printf ("vidioc_g_ext_ctrls failed. \ n "); Return-1;} printf (" ------- after set ------ \ n "); If (IOCTL (m_fd, vidioc_g_ext_ctrls, & ctrls) <0) {printf ("vidioc_g_ext_ctrls failed. \ n "); Return-1;} printf (" v4l2_cid_exposure_auto = % d \ n ", ctrl. value);} if (1) {struct v4l2_ext_controls ctrls = {0}; struct v4l2_ext_control CTRL = {0}; ctrls. ctrl_class = v4l2_ctrl_class_camera; ctrls. count = 0; ctrls. controls = & CTRL; ctrl. id = v4l2_cid_exposure_absolute; ctrl. value = 0; If (IOCTL (m_fd, vidioc_g_ext_ctrls, & ctrls) <0) {printf ("v4l2_cid_exposure_absolute failed. \ n "); Return-1;} printf (" v4l2_cid_exposure_absolute = % d \ n ", ctrl. value); printf ("------- set v4l2_cid_exposure_absolute ------ \ n"); ctrl. value = 1; if (IOCTL (m_fd, vidioc_s_ext_ctrls, & ctrls) <0) {printf ("vidioc_g_ext_ctrls failed. \ n "); Return-1;} printf (" ------- after set v4l2_cid_exposure_absolute ------ \ n "); If (IOCTL (m_fd, vidioc_g_ext_ctrls, & ctrls) <0) {printf ("vidioc_g_ext_ctrls failed. \ n "); Return-1;} printf (" v4l2_cid_exposure_absolute = % d \ n ", ctrl. value);} return 0;} int uvccamera: setframerate (int fps) {struct v4l2_streamparm v_param; clear (v_param); v_param.type = complete; If (IOCTL (m_fd, vidioc_g_parm, & v_param) <0) {// error ("set Param:"); printf ("failed to set framerate. \ n "); Return-1;} // parm-> parm. capture. capturemode = v4l2_mode_highquality; duration = FPS; // time interval Denominator = 1; // If (IOCTL (m_fd, vidioc_s_parm, & v_param) <0) {// error ("set Param:"); printf ("failed to set framerate. \ n "); Return-1;} If (IOCTL (m_fd, vidioc_g_parm, & v_param) <0) {// error (" set Param :"); printf ("failed to set framerate. \ n "); Return-1 ;}// check whether the frame rate is set to printf (" framerate: % d/% d \ n ", v_param.parm.output.timeperframe.numerator, role ); return 0;} int uvccamera: enumformats () {struct v4l2_fmtdesc FMT; memset (& FMT, 0, sizeof (FMT); FMT. index = 0; FMT. type = v4l2_buf_type_video_capture; while (IOCTL (m_fd, vidioc_enum_fmt, & FMT) = 0) {FMT. index ++; printf ("{pixelformat = '% C % C', description =' % s'} \ n", FMT. pixelformat & 0xff, (FMT. pixelformat> 8) & 0xff, (FMT. pixelformat> 16) & 0xff, (FMT. pixelformat> 24) & 0xff, FMT. description);} return 0;} int uvccamera: waitforframe (int ms) {fd_set FDS; fd_zero (& FDs); // clear the specified file descriptor set fd_set (m_fd, & FDs); // Add a new file descriptor in the file descriptor set/* timeout. */struct timeval TV; TV. TV _sec = MS/1000; TV. TV _usec = (MS % 1000) * 1000000; int r = select (m_fd + 1, & FDS, null, null, & TV ); // determine whether the camera is readable (that is, whether the camera is ready). If (-1 = r) {return-1;} If (0 = r) {fprintf (stderr, "select timeout \ n"); Return 0;} return 1;} int uvccamera: readframe (void * outbuf) {struct v4l2_buffer v_buf; clear (v_buf); v_buf.type = clear; v_buf.memory = v4l2_memory_mmap; IOCTL (m_fd, vidioc_dqbuf, & v_buf); // frame buffer collected from the column // assert (BUF. index <m_bufcount); // printf ("Buf. index DQ is % d, \ n ", v_buf.index); // printf (" vbuf. bytes used = % d \ n ", v_buf.bytesused); // copy the image frame out. // int Len = m_bufs [v_buf.index]. length; int Len = v_buf.bytesused; // the actual size of memcpy (outbuf, m_bufs [v_buf.index]. start, Len); v_buf.bytesused = 0; IOCTL (m_fd, vidioc_qbuf, & v_buf); // return Len ;}


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.