//ffmpegDecode.h

#ifndef __FFMPEG_DECODE_H__
#define __FFMPEG_DECODE_H__ #include "global.h" extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
//图像转换结构需要引入的头文件
#include "libswscale/swscale.h"
}; class ffmpegDecode
{
public:
ffmpegDecode(char * file = NULL);
~ffmpegDecode(); cv::Mat getDecodedFrame();
cv::Mat getLastFrame();
int readOneFrame();
int getFrameIndex(){return m_framIndex;}; private:
AVFrame *pAvFrame;
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec; int i;
int videoindex;
int m_framIndex;
char *filepath;
int ret, got_picture;
SwsContext *img_convert_ctx;
int y_size;
AVPacket *packet; cv::Mat *pCvMat; bool m_initResult; bool init();
bool openDecode();
void prepare();
void get(AVCodecContext *pCodecCtx, SwsContext *img_convert_ctx,AVFrame *pFrame); public:
bool getInitResult(); }; #endif
//ffmpegDecode.cpp

#include "ffmpegDecode.h"
#include <QDebug> int time_out = ;
int firsttimeplay = ;
int interrupt_cb(void *ctx)
{
// do something time_out++;
if (time_out > ) {
time_out=;
if (firsttimeplay) {
firsttimeplay=;
return -;//这个就是超时的返回
}
}
return ;
} ffmpegDecode :: ~ffmpegDecode()
{
pCvMat->release(); pCvMat->release();
//释放本次读取的帧内存
av_free_packet(packet);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
} ffmpegDecode :: ffmpegDecode(char * file)
{
firsttimeplay = ;
pAvFrame = NULL/**pFrameRGB = NULL*/;
pFormatCtx = NULL;
pCodecCtx = NULL;
pCodec = NULL; pCvMat = new cv::Mat();
i=;
videoindex=;
m_framIndex =;
ret = ;
got_picture = ;
img_convert_ctx = NULL;
y_size = ;
packet = NULL; if (NULL == file)
{
filepath = "rtsp://admin:admin123@192.168.10.239:554";
}
else
{
filepath = file;
} m_initResult = false;
if(init())
{
if(openDecode())
{
prepare();
m_initResult =true;
}
} } bool ffmpegDecode::getInitResult()
{
return m_initResult;
} bool ffmpegDecode :: init()
{
printf("init start...\n");
//ffmpeg注册复用器,编码器等的函数av_register_all()。
//该函数在所有基于ffmpeg的应用程序中几乎都是第一个被调用的。只有调用了该函数,才能使用复用器,编码器等。
//这里注册了所有的文件格式和编解码器的库,所以它们将被自动的使用在被打开的合适格式的文件上。注意你只需要调用 av_register_all()一次,因此我们在主函数main()中来调用它。如果你喜欢,也可以只注册特定的格式和编解码器,但是通常你没有必要这样做。
av_register_all();
avformat_network_init();
// pFormatCtx = avformat_alloc_context();
// pFormatCtx->interrupt_callback.callback = interrupt_cb;//--------注册回调函数 // pFormatCtx->interrupt_callback.opaque = pFormatCtx;
//打开视频文件,通过参数filepath来获得文件名。这个函数读取文件的头部并且把信息保存到我们给的AVFormatContext结构体中。
//最后2个参数用来指定特殊的文件格式,缓冲大小和格式参数,但如果把它们设置为空NULL或者0,libavformat将自动检测这些参数。 AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "tcp", );
av_dict_set(&options, "stimeout", "", ); //设置超时断开连接时间,单位微秒 if(avformat_open_input(&pFormatCtx,filepath,NULL,&options)!=)
{
printf("无法打开文件\n");
return false;
} //查找文件的流信息,avformat_open_input函数只是检测了文件的头部,接着要检查在文件中的流的信息
if(avformat_find_stream_info(pFormatCtx,&options)<)
{
printf("Couldn't find stream information.\n");
return false;
}
printf("init finished...\n"); return true;
} bool ffmpegDecode :: openDecode()
{
printf("openDecode start...\n");
//在库里面查找支持该格式的解码器
videoindex = -;
for(i=; i<pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoindex=i;
break;
}
}
if(videoindex==-)
{
printf("Didn't find a video stream.\n");
return false;
}
pCodecCtx=pFormatCtx->streams[videoindex]->codec; //在库里面查找支持该格式的解码器
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
printf("Codec not found.\n");
return false;
} //打开解码器
if(avcodec_open2(pCodecCtx, pCodec,NULL) < )
{
printf("Could not open codec.\n");
return false;
}
printf("openDecode finished\n"); return true;
} void ffmpegDecode :: prepare()
{
//printf("prepare int\n");
//分配一个帧指针,指向解码后的原始帧
pAvFrame=av_frame_alloc();
y_size = pCodecCtx->width * pCodecCtx->height;
//分配帧内存
packet=(AVPacket *)av_malloc(sizeof(AVPacket));
av_new_packet(packet, y_size); //输出一下信息-----------------------------
printf("文件信息-----------------------------------------\n");
av_dump_format(pFormatCtx,,filepath,);
//av_dump_format只是个调试函数,输出文件的音、视频流的基本信息了,帧率、分辨率、音频采样等等
//printf("prepare out\n");
} int ffmpegDecode :: readOneFrame()
{
int result = ;
pCvMat->release();
result = av_read_frame(pFormatCtx, packet);
return result;
} cv::Mat ffmpegDecode :: getDecodedFrame()
{
readOneFrame();
if(packet->stream_index==videoindex)
{
//解码一个帧
ret = avcodec_decode_video2(pCodecCtx, pAvFrame, &got_picture, packet);
if(ret < )
{
printf("解码错误\n");
return cv::Mat();
}
if(got_picture)
{
m_framIndex++;
//根据编码信息设置渲染格式
if(img_convert_ctx == NULL){
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
}
//----------------------opencv
if (pCvMat->empty())
{
pCvMat->create(cv::Size(pCodecCtx->width, pCodecCtx->height),CV_8UC3);
} if(img_convert_ctx != NULL)
{
get(pCodecCtx, img_convert_ctx, pAvFrame);
}
}
}
av_free_packet(packet);
return *pCvMat; }
cv::Mat ffmpegDecode :: getLastFrame()
{
ret = avcodec_decode_video2(pCodecCtx, pAvFrame, &got_picture, packet);
if(got_picture)
{
//根据编码信息设置渲染格式
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL); if(img_convert_ctx != NULL)
{
get(pCodecCtx, img_convert_ctx,pAvFrame);
}
}
return *pCvMat;
} void ffmpegDecode :: get(AVCodecContext * pCodecCtx, SwsContext * img_convert_ctx, AVFrame * pFrame)
{
if (pCvMat->empty())
{
pCvMat->create(cv::Size(pCodecCtx->width, pCodecCtx->height),CV_8UC3);
} AVFrame *pFrameRGB = NULL;
uint8_t *out_bufferRGB = NULL;
pFrameRGB = av_frame_alloc(); //给pFrameRGB帧加上分配的内存;
int size = avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);
out_bufferRGB = new uint8_t[size];
avpicture_fill((AVPicture *)pFrameRGB, out_bufferRGB, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height); //YUV to RGB
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, , pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); memcpy(pCvMat->data,out_bufferRGB,size); delete[] out_bufferRGB;
av_free(pFrameRGB);
}
//crtspdecodethread.h

#ifndef CRTSPDECODETHREAD_H
#define CRTSPDECODETHREAD_H #include <QThread>
#include <QMutex>
#include "ffmpegDecode.h" class CRTSPDecodeThread : public QThread
{
Q_OBJECT public:
CRTSPDecodeThread(QObject* parent);
~CRTSPDecodeThread(); void SetCameraParam(QString, int, int cameraID);
void run(); signals:
void SendVideoFrame(cv::Mat);
void SendDetectFrame(cv::Mat); private:
ffmpegDecode* m_pVdoDecode;
bool m_isExist;
unsigned long m_FrameCount;
int m_detectInterval; VideoCapture m_VideoCap; QString m_cameraURL;
QMutex m_Mutex; int m_cameraID;
bool m_decodeInitResult;
}; #endif // CRTSPDECODETHREAD_H
//crtspdecodethread.cpp

#include "crtspdecodethread.h"
#include "ffmpegDecode.h"
#include <QDebug>
#include <QDateTime>
#include <queue>
#include <QMutexLocker> extern bool g_ImportLib;
std::queue<ST_IMGINFO> g_OrgImgQueue; CRTSPDecodeThread::CRTSPDecodeThread(QObject* parent):QThread(parent)
{
m_isExist = false;
} CRTSPDecodeThread::~CRTSPDecodeThread()
{
requestInterruption();
quit();
wait(); m_isExist = true;
} void CRTSPDecodeThread::SetCameraParam(QString strURL, int iInterval, int cameraID)
{
m_cameraID = cameraID;
m_detectInterval = iInterval;
m_cameraURL =strURL;
if(m_cameraURL == "USB")
{
bool bRet = m_VideoCap.open();
if(!bRet)
{
qDebug()<<"打开USB摄像头失败...";
}
}
else
{
m_pVdoDecode = new ffmpegDecode((char*)strURL.toStdString().c_str());
m_decodeInitResult = m_pVdoDecode->getInitResult();
} } void CRTSPDecodeThread::run()
{ m_FrameCount = ; cv::Mat img;
unsigned long iRTSPOfflineTick = GetTickCount();
while(!isInterruptionRequested())
{
if(m_isExist)
{
break;
} if(m_cameraURL == "USB")
{
m_VideoCap>>img;
}
else
{
if(m_decodeInitResult)
{
img =m_pVdoDecode->getDecodedFrame();
}
} if(!img.empty())
{
m_FrameCount++;
//cvtColor(img, img, COLOR_BGR2RGB);
iRTSPOfflineTick = GetTickCount(); emit SendVideoFrame(img); if(m_FrameCount % m_detectInterval == )
{ ST_IMGINFO imgInfo;
img.copyTo(imgInfo.img); imgInfo.camera_id =m_cameraID;// m_pManager->GetCameraID(); QDateTime dtTime;
imgInfo.time = dtTime.currentDateTime().toString("yyyy-MM-dd HH:mm:ss"); QMutexLocker lock(&m_Mutex);
g_OrgImgQueue.push(imgInfo); } img.release();
}
else
{
qDebug()<<"获取原始视频帧失败..."; if( (GetTickCount() -iRTSPOfflineTick ) > *)
{
qDebug()<<"重新打开视频流..."; iRTSPOfflineTick = GetTickCount(); if(m_cameraURL == "USB")
{
bool bRet = m_VideoCap.open();
if(!bRet)
{
qDebug()<<"打开USB摄像头失败...";
}
}
else
{
delete m_pVdoDecode;
m_pVdoDecode = NULL; m_pVdoDecode = new ffmpegDecode((char*)m_cameraURL.toStdString().c_str());
m_decodeInitResult = m_pVdoDecode->getInitResult();
}
}
}
}
}

Qt FFMPEG+OpenCV开启摄像头的更多相关文章

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

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

  2. QT creator中使用opencv采集摄像头信息

    之前在QT creator上成功编译了opencv,由于课题需要,需要采集摄像头的信息.故搜集了网上的一些资料,依葫芦画瓢的照着做了一下,终于简单的成功采集了信息. 打开QTcreator,新建一个w ...

  3. QT与openCV,与PCL结合!

    (1):详解QT多媒体框架:给予视频播放器 原文链接:http://mobile.51cto.com/symbian-271123.htm 对于使用主框架的QT程序,实现Qimage的转换可借鉴下面程 ...

  4. Opencv——将摄像头拍摄写成视频文件

    这里主要利用了Opencv打开摄像头的代码,以及写入视频的函数,只是这里要注意的是摄像头好像没有帧率,在cvCreateVideoWriter,时要自己设置 #include"cv.h&qu ...

  5. openCV 调用摄像头

    OpenCV调用摄像头 环境 python:python3.6 摄像头:网络摄像头 Python库:openCV # -*- coding: utf-8 -*- # @author leone # @ ...

  6. Raspberry Pi 4B 使用OpenCV访问摄像头picamera模块

    目录 1.OpenCV安装 (1)安装依赖 (2)下载OpenCV源码 (3)安装pip (4)安装Python虚拟机 (5)编译OpenCV (6)验证安装 2.使用OpenCV和Python控制摄 ...

  7. 基于opencv网络摄像头在ubuntu下的视频获取

     基于opencv网络摄像头在ubuntu下的视频获取 1  工具 原料 平台 :UBUNTU12.04 安装库  Opencv-2.3 2  安装编译运行步骤 安装编译opencv-2.3  参 ...

  8. OpenCv调用摄像头拍照代码

    近期在研究OpenCv对摄像头的调用.现将代码贴出,供大家批评指正. 1.申明 #include"./opencv2/opencv.hpp" #ifdef _DEBUG #prag ...

  9. 基于opencv在摄像头ubuntu根据视频获取

     基于opencv在摄像头ubuntu根据视频获取 1  工具 原料 平台 :UBUNTU12.04 安装库  Opencv-2.3 2  安装编译执行步骤 安装编译opencv-2.3  參考h ...

随机推荐

  1. Group by 分组查询 实战

    实战经历,由于本人在共享单车上班,我们的单车管理模块,可以根据单车号查询单车,但是单车号没有设置unique(独一无二约束),说以这就增加了单车号可能重复的风险,但是一般情况下,单车号是不会重复的,因 ...

  2. [我的阿里云服务器] —— WorkPress

    前言: WordPress是基于PHP和MYSQL编成的一套博客系统,因此一般会选择LAMP环境来让它最稳定地运行, 这里的LAMP指的是Linux.Apache.MySQL.PHP,我们我的阿里云服 ...

  3. 8.2,常用模块介绍:sys,os,time,random

    sys: 介绍:主要包含涉及python编译器与系统交互的函数. 常用函数: import sys print(sys.argv)#本文件名,已经运行该程序时的参数 #[如在命令窗口中python3 ...

  4. WOE和IV

    woe全称是"Weight of Evidence",即证据权重,是对原始自变量的一种编码形式. 进行WOE编码前,需要先把这个变量进行分组处理(离散化) 其中,pyi是这个组中响 ...

  5. 腾讯云Centos安装jdk8

    1.下载jdk1.8的tar cd /usr/local/src #切换到该目录下 wget url #下载jdk8的tar包 2.下载完成后解压tar包 tar -zxvf jdk-8u152-li ...

  6. 学习vue.js的正确姿势(转载)

    最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ...

  7. SerialPort类的用法与示例

    转:https://www.cnblogs.com/hwBeta/p/6926363.html Microsoft .Net框架SerialPort类的用法与示例 从Microsoft .Net 2. ...

  8. Nginx服务器报 "Too Many Open Files"

    近日服务器上的运行的一个站点经常性出现500错误.查了下服务器负载,负载正常.而后查询了下nginx记录的站点运行错误日志,发现提示Too many open files.因为站点静态文件居多,而且h ...

  9. sqlserver sql优化案例及思路

    始sql: SELECT TOP 100 PERCENT ZZ.CREW_NAME AS 机组, ZZ.CREW_ID, AA.年度时间, CC.当月时间, DD.连续七天时间 AS 最近七天 FRO ...

  10. 解决:Determining IP Information for eth0...问题

    环境:Centos 6.2     VMWare Workstation 7.1.2  故障现象: 在虚拟机中启动Centos,在启动页面中停留在Determining IP Information ...