Linux下控制免驱USB摄像头的信息

本文介绍了在Linux系统下,利用v4l2接口控制UVC免驱USB摄像头的详细步骤,包括打开摄像头、获取摄像头信息、设置参数、内存映射、数据获取和转换。代码示例中,还展示了如何使用QT进行视频显示。
摘要由CSDN通过智能技术生成
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值