Linux下控制免驱USB摄像头的信息-支持arm
--霜之小刀(转载请注明作者)
若有问题,请联系邮箱:lihn1011@163.com
QQ:2279557541
其实在linux下获取摄像头信息非常的简单。(这里我说的是UVC的USB摄像头哈,也就是所谓的免驱摄像头)
另外我操作摄像头使用的是v4l2方式,linux系统自带,无需借助三方库,但是视频的显示使用的是QT使用了第三方库
一般来说也就下面几步。
1、打开摄像头
2、获取摄像头信息
3、设置摄像头信息
4、控制摄像头开始摄像
5、设置视频数据的内存映射
6、获取视频数据
7、将视频数据转换为界面能显示的数据
8、显示
首先摄像头相关的是1~6步骤,我这里分装了一个类,先上代码
camara.h
#ifndef CAMARA_H
#define CAMARA_H
#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 <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 <string>
#include <map>
#include <vector>
struct CamaraBuffer
{
void* m_start;
unsigned int m_length;
};
class Camara
{
public:
Camara();
bool OpenCamara(const std::string& dev_name);
bool Init(const std::string& dev_name, int width, int height);
void* GetVideo();
void FreeVideo();
private:
std::string m_camara_name;
int m_camara_fd;
struct v4l2_capability m_camara_info;
std::map<int/*pixmapformat*/, std::pair<int/*type*/,std::string> > m_camara_format;
std::vector<CamaraBuffer> m_buffer_list;
struct v4l2_buffer m_current_buf;
};
#endif // CAMARA_H
camara.cpp
#include "Camara.h"
Camara::Camara():m_camara_fd(0)
{
}
bool Camara::OpenCamara(const std::string &dev_name)
{
//打开摄像头
int fd = open(dev_name.c_str(), O_RDWR,0);
if(fd < 0)
{
printf("Camara Open Fiald:%d\r\n", fd);
retur