一、摘要
在Linux操作系统下,摄像头的工作都要遵循V4L2的机制,下面介绍如何从无到有,编写代码去使能摄像头拍照。
使用V4L2基本上就是使用内核提供的函数接口,填充相应的函数,主要依靠ioctl()函数填充函数域,然后按照一定操作顺序(工作流程),就能拍照了。
二、操作顺序(工作流程)
前奏:
1、打开USB摄像头设备文件
2、获取驱动信息(VIDIOC_QUERYCAP)
3、设置图像格式(VIDIOC_S_FMT)
帧缓冲:
4、申请帧缓冲(VIDIOC_REQBUFS)
5、获取帧缓冲地址长度信息(VIDIOC_QUERYBUF)
6、使用mmap吧内核空间的帧缓冲映射到用户空间
7、帧缓冲入队列
8、开始采集图像(VIDIOC_STREAMON)
9、取出帧缓冲(VIDIOC_DQBUF)
10、访问帧缓冲(可以读写操作等等)
11、帧缓冲入队列,重新入队(VIDIOC_QBUF)
写代码的时候按照这个编写就行了。
如果用在嵌入式板子上,只用交叉编译以后就能用了
#include <stdio.h>
#include <stdlib.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 <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>
struct 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)
{
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;
/*帧出列*/
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
ioctl (fd, VIDIOC_DQBUF, &buf);
write(file_fd,buffers[buf.index].start,buffers[buf.index].length);
/*buf入列*/
ioctl(fd, VIDIOC_QBUF, &buf);
return 1;
}
/*帧出列*/
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
ioctl (fd, VIDIOC_DQBUF, &buf);
write(file_fd,buffers[buf.index].start,buffers[buf.index].length);
/*buf入列*/
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);
/*获取驱动信息*/
{
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);
/*获取驱动信息*/
ioctl (fd, VIDIOC_QUERYCAP, &cap); //驱动信息保存在cap结构体
printf("Driver Name:%s/n Card Name:%s/n Bus info:%s/n/n",cap.driver,cap.card,cap.bus_info);
/*设置图像格式*/
&pe = 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; //图像格式,此处是jpg
ioctl (fd, VIDIOC_S_FMT, &fmt) ;
/*申请图像缓冲区*/
&unt = 4;
printf("Driver Name:%s/n Card Name:%s/n Bus info:%s/n/n",cap.driver,cap.card,cap.bus_info);
/*设置图像格式*/
&pe = 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; //图像格式,此处是jpg
ioctl (fd, VIDIOC_S_FMT, &fmt) ;
/*申请图像缓冲区*/
&unt = 4;
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
ioctl (fd, VIDIOC_REQBUFS, &req);
buffers = calloc (unt, sizeof (*buffers));
for (n_buffers = 0; n_buffers < unt; ++n_buffers)
{
/*获取图像缓冲区的信息*/
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
buf.index = n_buffers;
ioctl (fd, VIDIOC_QUERYBUF, &buf);
& = V4L2_MEMORY_MMAP;
ioctl (fd, VIDIOC_REQBUFS, &req);
buffers = calloc (unt, sizeof (*buffers));
for (n_buffers = 0; n_buffers < unt; ++n_buffers)
{
/*获取图像缓冲区的信息*/
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
buf.index = n_buffers;
ioctl (fd, VIDIOC_QUERYBUF, &buf);
buffers[n_buffers].length = buf.length;
// 把内核空间中的图像缓冲区映射到用户空间
buffers[n_buffers].start = mmap (NULL , //通过mmap建立映射关系
buf.length,
PROT_READ | PROT_WRITE ,
MAP_SHARED ,
fd,
ffset);
}
/*图像缓冲入队*/
for (i = 0; i < n_buffers; ++i)
{
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
buf.index = i;
ioctl (fd, VIDIOC_QBUF, &buf);
}
//开始捕捉图像数据
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl (fd, VIDIOC_STREAMON, &type);
fd_set fds;
FD_ZERO (&fds);
&pe = V4L2_BUF_TYPE_VIDEO_CAPTURE;
& = V4L2_MEMORY_MMAP;
buf.index = i;
ioctl (fd, VIDIOC_QBUF, &buf);
}
//开始捕捉图像数据
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_frame();
for (i = 0; i < n_buffers; ++i)
munmap (buffers[i].start, buffers[i].length);
close (fd);
close (file_fd);
printf("Camera Done./n");mmap格式怎么打开
select(fd + 1, &fds, NULL, NULL, NULL);
/*读取一幅图像*/
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");mmap格式怎么打开
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论