一、读取摄像头的基础知识
1、读取摄像头用到的类库Libavdevice:可以读取电脑(或者其他设备上)的多媒体设备的数据,或者输出数据到指定的多媒体设备上等。
这个库支持以下设备作为输入端:avfoundation、bktr、dshow(本文使用)、fbdev、gdigrab、jack、lavfi、libcdio、openal、vfwcap等
支持以下设备作为输出端:alsa、caca、decklink、fbdev、opengl、sndio、xv等
2、libavdevice使用
头文件:#include "libavdevice/avdevice.h"
注册:libavdevice:avdevice_register_all()
2.1使用libavdevice读取数据和直接打开视频文件类似。因为系统的设备也被FFmpeg认为是一种输入的格式(即AVInputFormat)。使用FFmpeg打开一个普通的视频文件如下:
AVFormatContext *formatContext = avformat_alloc_context();
avformat_open_input(&formatContext , "Warcraft3_End.avi",nullptr,nullptr);
2.2使用libavdevice,唯一的不同在于需要首先查找用于输入的设备。(av_find_input_format())
AVFormatContext *formatContext = avformat_alloc_context();
AVInputFormat *ifmt=av_find_input_format("dshow");//windows上视频的推流格式
avformat_open_input(&formatContext , "video=Integrated Camera", ifmt,NULL);
注:1、如果是指定了vfwcap设备作为输入设备,在URL中指定打开第0个摄像头设备如下
avformat_open_input(&formatContext , 0, ifmt,NULL);
2、URL的格式是"video={设备名称}",设备名称外面不能加引号,注意空格。
3、dshow的设备名称必须要提前获取
2.3可以在 我的电脑--》管理--》设备管理器 中查找本机摄像头名称也可以通过代码获取
获取摄像头数量和名称
.pro文件中添加如下:
QT += multimedia QT += multimediawidgets
//头文件QCameraInfo、QCamera
QList<QCameraInfo> cameraList = QCameraInfo::availableCameras();
qDebug()<<cameraList.size()<<endl;
for(int i=0;i<cameraList.size();i++)
{
qDebug()<<camera