1、操作流程:
2、查找并打开摄像头设备:
(1)、使用vfwcap作为输入设备:
//查找摄像头设备
//1.VFW: Video for Windows 屏幕捕捉设备。输入URL是设备的序号,从0至9。
if(nullptr == (pInFmt = const_cast<AVInputFormat*>(av_find_input_format("vfwcap"))))
{
qDebug() << "find AVInputFormat failed." << endl;
break;
}
//打开输入设备
if(avformat_open_input(&pFmtCtx, 0, pInFmt, nullptr) < 0)
{
qDebug() << "open camera failed." << endl;
break;
}
(2)、使用dshow作为输入设备:
if(nullptr == (pInFmt = const_cast<AVInputFormat*>(av_find_input_format("dshow"))))
{
qDebug() << "find AVInputFormat failed." << endl;
break;
}
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
QString urlString = QString("video=") + cameras.at(0).description();
//打开输入设备
if(avformat_open_input(&pFmtCtx, urlString.toStdString().c_str(), pInFmt, nullptr) < 0)
{
qDebug() << "open camera failed." << endl;
break;
}
上面使用QT中的QCameraInfo类查找摄像头名称,我们还可以使用ffmpeg中的如下命令来查找:
ffmpeg -list_devices true -f dshow -i dummy
3、实现:
(1)、创建一个多线程类,在多线程类中完成视频帧的获取、解码等复杂操作:
#ifndef VIDEOTHREAD_H
#define VIDEOTHREAD_H
#include <QImage>
#include <QObject>
class videoThread : public QObject
{
Q_OBJECT
public:
explicit videoThread(QObject *parent = nullptr);
signals:
void sig_sendQImage(QImage);
public slots:
void slot_cameraVideo();
};
#endif // VIDEOTHREAD_H
#include "videothread.h"
#include <QThread>
#include <QDebug>
#include <QCameraInfo>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavfilter/avfilter.h"
#include "libavutil/avutil.h"
#include "libavutil/ffversion.h"
#include <libavutil/imgutils.h>
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
#include "libpostproc/postprocess.h"
}
videoThread::videoThread(QObject *parent) : QObject(parent)
{
}
void videoThread::slot_cameraVideo()
{
AVFormatContext* pFmtCtx = nullptr;
AVInputFormat* pInFmt = nullptr;
int nVideoIndex = -1;
AVCodecParameters* pCodecParam = nullptr;
AVCodecContext * pCodecCtx = nullptr;
AVCodec * pCodec = nullptr;
AVFrame* pFrame = av_frame_alloc();
AVFrame* pFrameRGB = av_frame_alloc();
AVPacket* pkt = nullptr;
do
{
//注册组件:libavdevice
avdevice_register_all();
//创建设备上下文
if(nullptr == (pFmtCtx = avformat_alloc_context()))
{
qDebug() << "create AVFormatContext failed." << endl;
break;
}
//查找摄像头设备
//1.VFW: Video for Windows 屏幕捕捉设备。输入URL是设备的序号,从0至9。
//2.dshow: 使用Directshow。注意作者机器上的摄像头设备名称是
//“HD User Facing”,使用的时候需要改成自己电脑上摄像头设备的名称。
if(1)//dshow
{
if(nullptr == (pInFmt = const_cast<AVInputFormat*>(av_find_input_format("dshow"))))
{
qDebug() << "find AVInputFormat failed." << endl;
break;
}
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
QString urlString = QString("video=") + cameras.at(0).description();
//打开输入设备
if(avformat_open_input(&pFmtCtx, urlString.toStdString().c_str(), pInFmt, nullptr) < 0)
{
qDebug() << "open camera failed." << endl;
break;
}
}
else //vfwcap
{
if(nullptr == (pInFmt = const_cast<AVInputFormat*>(av_find_input_format("vfwcap"))))
{
qDebug() << "find AVInputFormat failed." << endl;
break;
}
//打开输入设备
if(avformat_open_input(&pFmtCtx, 0, pInFmt, nullptr) < 0)
{
qDebug() << "open camera failed." << endl;
break;
}
}
//查找流信息
if(avformat_find_stream_info(pFmtCtx, NULL) < 0){
qDebug() << "cannot find stream info." << endl;
break;
}
for(size_t i = 0;i < pFmtCtx->nb_streams;i++){
if(pFmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){
nVideoIndex = i;
break;
}
}
if(nVideoIndex == -1){
qDebug() << "cannot find video stream." << endl;
break;
}
//查找编码器
pCodecParam = pFmtCtx->streams[nVideoIndex]->codecpar;
if(nullptr == (pCodec = const_cast<AVCodec*>(avcodec_find_decoder(pCodecParam->codec_id))))
{
qDebug() << "cannot find codec." << endl;
break;
}
//创建编码器上下文
if(nullptr == (pCodecCtx = avcodec_alloc_context3(pCodec))){
qDebug() << "cannot alloc codecContext." << endl;
break;
}
if(avcodec_parameters_to_context(pCodecCtx, pCodecParam) < 0){
qDebug() << "cannot initialize codecContext." << endl;
break;
}
//打开编码器
if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0){
qDebug() << "cannot open codec." << endl;
break;
}
//设置帧数据转换上下文
struct SwsContext *img_convert_ctx = nullptr;
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
uint8_t* out_buffer = (unsigned char*)av_malloc(static_cast<unsigned long long>(numBytes) * sizeof(unsigned char));
//绑定内存块
if(av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize,
out_buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1) < 0)
{
qDebug() << "av_image_fill_arrays failed." << endl;
break;
}
int ret;
pkt = av_packet_alloc();
av_new_packet(pkt, pCodecCtx->width * pCodecCtx->height);
while(av_read_frame(pFmtCtx, pkt) >= 0){
if(pkt->stream_index == nVideoIndex){
if(avcodec_send_packet(pCodecCtx, pkt)>=0){
while((ret = avcodec_receive_frame(pCodecCtx, pFrame)) >= 0){
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
return;
}
//将解码后数据转换为Rgb格式数据
sws_scale(img_convert_ctx,
pFrame->data, pFrame->linesize,
0, pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);
//将img发送给界面进行显示
QImage img(out_buffer, pCodecCtx->width, pCodecCtx->height, QImage::Format_RGB888);
emit sig_sendQImage(img);
QThread::msleep(10);
}
}
av_packet_unref(pkt);
}
}
}while(0);
av_packet_free(&pkt);
avcodec_close(pCodecCtx);
avcodec_parameters_free(&pCodecParam);
av_frame_free(&pFrame);
av_frame_free(&pFrameRGB);
if(pFmtCtx)
{
avformat_close_input(&pFmtCtx);
avformat_free_context(pFmtCtx);
}
}
(2)多线程的创建和开启实现:
void MainWindow::createWorkThread()
{
videoThread* pVideoThread = new videoThread;
pVideoThread->moveToThread(&m_workThread);
//线程结束后销毁videoThread对象
connect(&m_workThread, &QThread::finished, pVideoThread, &QObject::deleteLater);
//绑定相关信号槽
connect(this, &MainWindow::sig_cameraVideo, pVideoThread, &videoThread::slot_cameraVideo);
connect(pVideoThread, &videoThread::sig_sendQImage, this, &MainWindow::slot_displayImage);
m_workThread.start();
}
void MainWindow::releaseWorkThread()
{
m_workThread.quit();
m_workThread.wait();
}
void MainWindow::slot_displayImage(QImage img)
{
ui->labDisplay->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::on_btnPlay_clicked()
{
emit sig_cameraVideo();
}
4、简单界面显示: