Linux platform
CodePudding user response:
#include#include
#include
#include
#include
#include
#include
#include
#include
#include
# include "v4l2grab. H"
# define TRUE 1
# define FALSE 0
# define VIDEO_DEV "/dev/video0
"# define BMP "image_bmp. BMP
"# define YUV "image_yuv. YUV
"
# define IMAGE_WIDTH 1280
# define IMAGE_HEIGHT 720
Static int fd.
The static struct v4l2_capability cap;
Struct v4l2_fmtdesc fmtdesc;
Struct v4l2_format FMT, fmtack;
Struct v4l2_streamparm setfps;
Struct v4l2_requestbuffers the req.
Struct v4l2_buffer buf.
Enum v4l2_buf_type type;
Unsigned char frame_buffer [IMAGE_HEIGHT IMAGE_WIDTH * * 3];
Struct buffer
{
Void * start;
Unsigned int length;
} * buffers;
Int init_v4l2 (void)
{
int i;
Int ret=0;
//the open video
If ((fd=open (VIDEO_DEV O_RDWR))==1)
{
Printf (" Error opening V4L interface \ n ");
Return (FALSE);
}
//query cap
If (ioctl (fd, VIDIOC_QUERYCAP, & amp; Cap)==1)
{
Printf (" Error opening device % s: unable to query device. \ n ", VIDEO_DEV);
Return (FALSE);
}
The else
{
Driver: printf (" % s \ n "t \ \ t, cap. The driver);
Printf (" card: \ t \ n \ t % s ", cap, card);
Printf (" bus_info: \ n \ t % s ", cap, bus_info);
Printf (" version: \ n \ t % d ", cap, version);
Printf (" x "capabilities: \ t % \ n", cap, "capabilities);
If ((cap. "capabilities & amp; V4L2_CAP_VIDEO_CAPTURE)==V4L2_CAP_VIDEO_CAPTURE)
{
Printf (" Device % s: supports the capture. \ n ", VIDEO_DEV);
}
If ((cap. "capabilities & amp; V4L2_CAP_STREAMING)==V4L2_CAP_STREAMING)
{
Printf (" Device % s: supports streaming. \ n ", VIDEO_DEV);
}
}
//emu all support FMT
Fmtdesc. Index=0;
Fmtdesc. Type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
Printf (" the Support the format: \ n ");
While (ioctl (fd, VIDIOC_ENUM_FMT, & amp; Fmtdesc)!=1)
{
Printf (" \ t % d % s \ n ", fmtdesc. Index + 1, fmtdesc. The description).
Fmtdesc. Index++;
}
//set the FMT
FMT. Type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
FMT. FMT. Pix. Pixelformat=V4L2_PIX_FMT_YUYV;
FMT. FMT. Pix. Height=IMAGE_HEIGHT;
FMT. FMT. Pix. Width=IMAGE_WIDTH;
FMT. FMT. Pix. Field=V4L2_FIELD_INTERLACED;
If (ioctl (fd, VIDIOC_S_FMT, & amp; FMT)==1)
{
Printf (" Unable to set the format \ n ");
return FALSE;
}
If (ioctl (fd, VIDIOC_G_FMT, & amp; FMT)==1)
{
Printf (" Unable to get the format \ n ");
return FALSE;
}
{
Printf (" FMT. Type: % d t \ \ t \ n ", FMT. Type);
Printf (" pix. Pixelformat: \ t % % % % c c c c \ n ", FMT. FMT. Pix. Pixelformat & amp; 0 XFF, (FMT. FMT. Pix. Pixelformat & gt;> 8) & amp; 0 XFF, (FMT. FMT. Pix. Pixelformat & gt;> 16) & amp; 0 XFF, (FMT. FMT. Pix. Pixelformat & gt;> 24) & amp; 0 XFF);
Printf (" pix. Height: \ t \ n \ t % d ", FMT. FMT. Pix, height);
Printf (" pix. Width: \ t \ n \ t % d ", FMT. FMT. Pix. Width);
Printf (" pix. Field: \ t \ n \ t % d ", FMT. FMT. Pix. The field);
}
Printf (" \ t [OK] init % s \ n ", VIDEO_DEV);
return TRUE;
}
Int v4l2_grab (void)
{
Unsigned int n_buffers;
//request for 4 buffers
The req. Count=4;
The req. Type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
The req.=V4L2_MEMORY_MMAP memory;
If (ioctl (fd, VIDIOC_REQBUFS, & amp; The req)==1)
{
Printf (" request for buffers error \ n ");
}
//mmap for buffers
Buffers=malloc (the req. Count * sizeof (* buffers));
if (! Buffers)
{
Printf (" Out of memory \ n ");
Return (FALSE);
}
For (n_buffers=0; N_buffers & lt; The req. Count; N_buffers + +)
{
Buf. Type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
Buf.=V4L2_MEMORY_MMAP memory;
Buf. Index=n_buffers;
//query buffers
If (ioctl (fd, VIDIOC_QUERYBUF, & amp; Buf)==1)
{
Printf (" query buffer error \ n ");
Return (FALSE);
}
Buffers [n_buffers]. Length=buf. Length;
//map
Buffers [n_buffers]. Start=mmap (NULL, buf. Length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf. M.o ffset);
If (buffers [n_buffers]. Start==MAP_FAILED)
{
Printf (" buffer map error \ n ");
Return (FALSE);
}
}
//the queue
For (n_buffers=0; N_buffers & lt; The req. Count; N_buffers + +)
{
Buf. Index=n_buffers;
The ioctl (fd, VIDIOC_QBUF, & amp; Buf);
}
Type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
The ioctl (fd, VIDIOC_STREAMON, & amp; Type);
The ioctl (fd, VIDIOC_DQBUF, & amp; Buf);
Printf (" grab yuyv OK \ n ");
Return (TRUE);
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull