需求

  打开检测摄像头工具,包括分辨率和帧率。

 

Demo

  

  

体验下载地址

  CSDN:https://download.csdn.net/download/qq21497936/12815691
  QQ群:1047134658(点击“文件”搜索“ffmpegCameraTool”,群内与博文同步更新)

 

涉及其他技术

  QCameraInfo打开摄像头偶尔拿不到摄像头;
  QCamera动态切换分辨率会导致崩溃;
  QCamera处理高分辨率存在卡顿问题;
  OpenCV无法拿取摄像头;
  OpenCV设置高分辨率存在帧率跟不上,卡顿问题;
  OpenCV保存高分辨率视频需要修改源码,否则限制mat上限大小为0xFFFF;
  OpenCV保存高分辨率修改源码后存储视频会导致通道混乱,需要手动矫正颜色通道。

 

v1.0.0功能

  • 程序启动打开计算机默认第一个摄像头,最高分辨率最高帧率打开;
  • 支持动态切换分辨率和帧率;
  • 支持原图显示,等比例显示;
  • 多个设备终端测试可用;
 

核心代码

FfmpegCameraManager.h

#ifndef FFMPEGCAMERAMANAGER_H
#define FFMPEGCAMERAMANAGER_H /************************************************************\
* 控件名称: FfmpegCameraManager, ffmpeg管理类(用于摄像头操作)
* 控件描述:
* 1.打开摄像头
* 2.支持动态切换分辨率
* 作者:红模仿 联系方式:QQ21497936
* 博客地址:https://blog.csdn.net/qq21497936
* 日期 版本 描述
* 2018年09年14日 v1.0.0 ffmpeg模块封装空类
* 2020年09年05日 v1.1.0 ffmpeg打开摄像头,支持的动态分辨率切换
\************************************************************/ #include <QObject>
#include <QString>
#include <QDebug>
#include <QTimer>
#include <QThread>
#include <QImage>
#include <QProcess>
#include <QMessageBox> extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
#include "libavformat/version.h"
#include "libavutil/time.h"
#include "libavutil/mathematics.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "errno.h"
#include "error.h"
} #define LOG qDebug()<<__FILE__<<__LINE__ class FfmpegCameraManager : public QObject
{
Q_OBJECT
public: public:
explicit FfmpegCameraManager(QObject *parent = nullptr); signals:
void signal_captureOneFrame(QImage image); public:
static QString getAvcodecConfiguration(); public:
bool init();
bool openUsbCamera();
QString getUsbCameraName();
QList<QString> getUsbCameraInfo(); public slots:
void slot_start();
void slot_stop();
void slot_setSizeFps(int index); protected slots:
void slot_captureOneFrame(); signals: public slots: private:
static bool _init; AVFormatContext *_pAVFormatContext; // 全局上下文
AVInputFormat *_pAVInputFormat;
AVDictionary* _pAVDictionary; // 打开编码器的配置 AVCodecContext *_pAVCodecContextForAudio; // 音频解码器上下文
AVCodecContext *_pAVCodecContextForVideo; // 视频解码器上下文(不带音频) AVCodec * _pAVCodecForAudio; // 音频解码器
AVCodec * _pAVCodecForVideo; // 视频解码器(不带音频) int _streamIndexForAudio; // 音频流序号
int _streamIndexForVideo; // 视频流序号 SwrContext *_pSwrContextForAudio; // 音频转换上下文 bool _running;
bool _first;
bool _opened;
uint8_t *_pOutBuffer; AVFrame * _pFrame;
AVFrame * _pFrameRGB;
AVPacket *_pAVPacket;
SwsContext *_pSwsContext; int _videoIndex; QString _cameraDescription; QList<QSize> _listSize;
QList<int> _listFps;
QList<QString> _listSizeFpsInfo;
int _currentSuzeFpsIndex;
}; #endif // FfmpegCameraManager_H

FfmpegCameraManager.cpp

...
void FfmpegCameraManager::slot_captureOneFrame()
{
if(_first)
{
// 读取一个媒体文件的数据包以获取流信息
if(avformat_find_stream_info(_pAVFormatContext, NULL) < 0)
{
LOG << "Couldn't find stream information";
}else{
LOG << "Success find stream information";
}
// 循环查找数据包包含的流信息,直到找到视频类型的流
// 便将其记录下来 保存到videoStream变量中
_videoIndex = -1;
for(int index = 0; index < _pAVFormatContext->nb_streams; index++)
{
if(_pAVFormatContext->streams[index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
_videoIndex = index;
break;
}
}
if(_videoIndex == -1)
{
LOG << "Couldn't find a video stream";
}else{
LOG << "Success find a video stream";
} _pAVCodecContextForVideo = _pAVFormatContext->streams[_videoIndex]->codec;
_pAVCodecForVideo = avcodec_find_decoder(_pAVCodecContextForVideo->codec_id);
//软编码
// _pAVCodecForVideo = avcodec_find_encoder(AV_CODEC_ID_H264);
//硬编码
// _pAVCodecForVideo = avcodec_find_encoder_by_name("nvenc_h264");
if(_pAVCodecForVideo == NULL)
{
qDebug() << ("Codec not found.\n");
}else{ qDebug() << "Codec found Successfuly!\n";
}
if(avcodec_open2(_pAVCodecContextForVideo, _pAVCodecForVideo, NULL) < 0)//打开解码器
{
LOG << "Failed to open codec";
}else{
LOG << "Success open codec";
} //分配一个AVFrame并将其字段设置为默认值
if(_pFrame == 0)
{
_pFrame = av_frame_alloc();
}
if(_pFrameRGB == 0)
{
_pFrameRGB = av_frame_alloc();
}
//分配和返回一个SwsContext你需要它来执行使用swsscale()的缩放/转换操作
_pSwsContext = sws_getContext(_pAVCodecContextForVideo->width,
_pAVCodecContextForVideo->height,
_pAVCodecContextForVideo->pix_fmt,
_pAVCodecContextForVideo->width,
_pAVCodecContextForVideo->height,
AV_PIX_FMT_RGB32,
SWS_BICUBIC,
NULL,
NULL,
NULL);
int numBytes = avpicture_get_size(AV_PIX_FMT_RGB32,
_pAVCodecContextForVideo->width,
_pAVCodecContextForVideo->height);
LOG << "numBytes:" << numBytes;
_pOutBuffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *)_pFrameRGB,
_pOutBuffer,
AV_PIX_FMT_RGB32,
_pAVCodecContextForVideo->width,
_pAVCodecContextForVideo->height);//根据指定的图像参数和提供的图像数据缓冲区设置图像域
int ySize = _pAVCodecContextForVideo->width * _pAVCodecContextForVideo->height;
LOG;
//分配一个packet
if(_pAVPacket == 0)
{
LOG;
_pAVPacket = (AVPacket *)malloc(sizeof(AVPacket));
//分配packet的数据
av_new_packet(_pAVPacket, ySize);
}else{
LOG;
av_free_packet(_pAVPacket);
av_new_packet(_pAVPacket, ySize);
LOG;
}
_first = false;
}
// 解码压缩
if(av_read_frame(_pAVFormatContext, _pAVPacket) < 0)
{
LOG << "解码失败";
return;
}
if(_pAVPacket->stream_index == _videoIndex)
{
int gotPicture;
// 解码一帧视频数据
int ret = avcodec_decode_video2(_pAVCodecContextForVideo, _pFrame, &gotPicture, _pAVPacket);
if(ret < 0)
{
LOG << "decode error";
}
if(gotPicture)
{
// 缩放图像切片,并将得到的缩放切片放在pFrameRGB->data图像中
sws_scale(_pSwsContext,
(uint8_t const * const *)_pFrame->data,
_pFrame->linesize,
0,
_pAVCodecContextForVideo->height,
_pFrameRGB->data,
_pFrameRGB->linesize); QImage tmpImg((uchar *)_pOutBuffer,
_pAVCodecContextForVideo->width,
_pAVCodecContextForVideo->height,
QImage::Format_RGB32);
QImage image = tmpImg.copy();
LOG << "get a pciture";
emit signal_captureOneFrame(image);
QTimer::singleShot(10, this, SLOT(slot_captureOneFrame()));
}
}
}
...
 

项目实战:Qt+ffmpeg摄像头检测工具的更多相关文章

  1. TF项目实战(SSD目标检测)-VOC2007

    TF项目实战(SSD目标检测)-VOC2007 训练好的模型和代码会公布在网上: 步骤: 1.代码地址:https://github.com/balancap/SSD-Tensorflow 2.解压s ...

  2. AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件

    AJ分享,必须精品 一:效果 二:封装好的工具条 NYComposeToolbar.h 带代理方法 #import <UIKit/UIKit.h> typedef enum { NYCom ...

  3. 项目实战:Qt+Ffmpeg+OpenCV相机程序(打开摄像头、支持多种摄像头、分辨率调整、翻转、旋转、亮度调整、拍照、录像、回放图片、回放录像)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  4. 【大话QT之十六】使用ctkPluginFramework插件系统构建项目实战

    "使用ctkPluginFramework插件系统构建项目实战",这篇文章是写博客以来最纠结的一篇文章. 倒不是由于技术都多么困难,而是想去描写叙述一个项目架构採用ctkPlugi ...

  5. 项目实战:Qt手机模拟器拉伸旋转框架

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  6. QT5 QSS QML界面美化视频课程系列 QT原理 项目实战 C++1X STL

    QT5 QSS QML界面美化视频课程系列 QT原理 项目实战 C++1X STL 课程1   C语言程序设计高级实用速成课程 基础+进阶+自学 课程2   C语言程序设计Windows GDI图形绘 ...

  7. 项目规范性检测工具Lint

    项目规范性检测工具lint.bat 一.Lint基本概念介绍 Android Lint是SDK Tools 16 (ADT 16)之后才引入的工具,通过它对Android工程源代码进行扫描和检查,可发 ...

  8. 项目实战10.1—企业级自动化运维工具应用实战-ansible

    实战环境: 公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出多套环境可以共开发和测试人员做测 ...

  9. Visual Studio 2015开发Qt项目实战经验分享(附项目示例源码)

    Visual Studio 2015开发Qt项目实战经验分享(附项目示例源码)    转 https://blog.csdn.net/lhl1124281072/article/details/800 ...

  10. 机器学习_线性回归和逻辑回归_案例实战:Python实现逻辑回归与梯度下降策略_项目实战:使用逻辑回归判断信用卡欺诈检测

    线性回归: 注:为偏置项,这一项的x的值假设为[1,1,1,1,1....] 注:为使似然函数越大,则需要最小二乘法函数越小越好 线性回归中为什么选用平方和作为误差函数?假设模型结果与测量值 误差满足 ...

随机推荐

  1. [转帖]Tomcat maxKeepAliveRequests

    https://www.cnblogs.com/turn2i/p/10480088.html 在写这个问题前,其实我是为了分析项目碰到的一个tcp close wait问题.这个问题就不在这里讲了. ...

  2. [转帖]linux下/proc/sysrq-trigger详解

    /proc/sysrq-trigger详解 这是一组"魔术组合键",只要内核没有被完全锁住,不管内核在做什么事情,使用这些组合键能即时打印出内核的信息. 使用SysRq组合键是了解 ...

  3. Core 文件的简单学习

    背景 最近公司内经常出现jvm进程宕机的情况. 宕机之后没有产生jvm的dump文件.比如xxx.hprof 但是产生了 core.$pid的文件. 曾经在aarch64架构上宕机时曾经想学习一下co ...

  4. 解锁前端新潜能:如何使用 Rust 锈化前端工具链

    ​ 前言 近年来,Rust的受欢迎程度不断上升.首先,在操作系统领域,Rust 已成为 Linux 内核官方认可的开发语言之一,Windows 也宣布将使用 Rust 来重写内核,并重写部分驱动程序. ...

  5. vue3封装搜索表单组件

    seacrch 表单完成的功能 1.根据配置json配置项自动生成表单 ok 2.是响应式的排版 ok 3.点击搜索按钮会向上抛出值 ok 4.点击重置按钮会自动清空数据,不需要父组件额外的处理 ok ...

  6. TienChin-课程管理-课程更新接口

    更改包名 将之前的 entity 更改为 domain: 将之前的 validator 包当中的校验分组接口移动到 common 模块当中,因为其它模块也需要使用就放到公共当中进行存储. 更改完毕之后 ...

  7. C++ 通过SQLite实现命令行工具

    本文介绍了一个基于 C++.SQLite 和 Boost 库的简单交互式数据库操作 Shell.该 Shell 允许用户通过命令行输入执行各种数据库操作,包括添加.删除主机信息,设置主机到特定主机组, ...

  8. LyScript 从文本中读写ShellCode

    LyScript 插件通过配合内存读写,可实现对特定位置的ShellCode代码的导出,或者将一段存储在文本中的ShellCode代码插入到程序堆中,此功能可用于快速将自己编写的ShellCode注入 ...

  9. .NetCore 三种生命周期注入方式

    .NetCore彻底诠释了"万物皆可注入"这句话的含义,在.NetCore中到处可见注入的使用.因此core中也提供了三种注入方式的使用,分别是: AddTransient:每次请 ...

  10. spring cloud与加密库jasypt(ulisesbocchio)冲突问题定位

    背景 最近在项目上遇到个问题.项目就是普通的spring cloud,spring cloud在spring boot的基础上多了一些东西,比如支持bootstrap上下文(通过bootstrap.y ...