Qt:解码海康视频格式并显示在QLabel上
2017-07-03 23:47
585 查看
海康的格式比较特殊,几乎是私有的,解码的话要使用海康的库才行。
先要下载SDK,地址如下:
我这里下载的是64位的,我本地的环境是Win10 64位, Qt是5.9.0 64bit,编译器使用VS2013
使用的话,还是挺方便的
pro中导入海康的库:
代码中的使用:
这里只列了mainwindow.cpp中的源码,核心的都在这里了。UI上只有一个label用于显示QPixmap还有一个button用户唤出文件选择框。
先要下载SDK,地址如下:
http://www1.hikvision.com/cn/download_more_406.html
我这里下载的是64位的,我本地的环境是Win10 64位, Qt是5.9.0 64bit,编译器使用VS2013
使用的话,还是挺方便的
pro中导入海康的库:
LIBS += -L$$PWD/hik -lPlayCtrl
代码中的使用:
#include "mainwindow.h" // Qt lib import #include <QDebug> #include <QMetaObject> #include <QPixmap> #include <QImage> #include <QFileDialog> #include <QStandardPaths> #include <QThread> // UI import #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui_(new Ui::MainWindow) { ui_->setupUi(this); qRegisterMetaType< QPixmap >( "QPixmap" ); connect( ui_->pushButton, &QPushButton::clicked, this, &MainWindow::play ); } MainWindow::~MainWindow() { delete ui_; } bool MainWindow::yv12ToRGB888(const unsigned char *yv12, unsigned char *rgb888, int width, int height) { if ( ( width < 1 ) || ( height < 1 ) || ( yv12 == nullptr ) || ( rgb888 == nullptr ) ) { return false; } const auto &&len = width * height; unsigned char const *yData = yv12; unsigned char const *vData = &yData[ len ]; unsigned char const *uData = &vData[ len >> 2 ]; int rgb[ 3 ]; int yIdx, uIdx, vIdx, idx; for ( auto i = 0; i < height; ++i ) { for ( auto j = 0; j < width; ++j ) { yIdx = i * width + j; vIdx = ( i / 2 ) * ( width / 2 ) + ( j / 2 ); uIdx = vIdx; rgb[ 0 ] = static_cast< int >( yData[ yIdx ] + 1.370705 * ( vData[ uIdx ] - 128 ) ); rgb[ 1 ] = static_cast< int >( yData[ yIdx ] - 0.698001 * ( uData[ uIdx ] - 128 ) - 0.703125 * ( vData[ vIdx ] - 128 ) ); rgb[ 2 ] = static_cast< int >( yData[ yIdx ] + 1.732446 * ( uData[ vIdx ] - 128 ) ); for ( auto k = 0; k < 3; ++k ) { idx = ( i * width + j ) * 3 + k; if( ( rgb[ k ] >= 0 ) && ( rgb[ k ] <= 255 ) ) { rgb888[ idx ] = static_cast< unsigned char >( rgb[ k ] ); } else { rgb888[ idx ] = ( rgb[ k ] < 0 )? ( 0 ) : ( 255 ); } } } } return true; } void MainWindow::decCallback(long /*nPort*/, char *pBuf, long /*nSize*/, FRAME_INFO *pFrameInfo, void *nUser, void * /*nReserved2*/) { auto this_ = reinterpret_cast< MainWindow * >( nUser ); if ( ( pFrameInfo->nWidth < 1 ) || ( pFrameInfo->nHeight < 1 ) ) { qDebug() << "decCallback: width or height error"; return; } // qDebug() << pFrameInfo->nWidth; // qDebug() << pFrameInfo->nHeight; switch ( pFrameInfo->nType ) { case T_RGB32: { break; } case T_UYVY: { break; } case T_YV12: { QImage image( pFrameInfo->nWidth, pFrameInfo->nHeight, QImage::Format_RGB888 ); if ( !yv12ToRGB888( reinterpret_cast< unsigned char * >( pBuf ), image.bits(), pFrameInfo->nWidth, pFrameInfo->nHeight ) ) { qDebug() << "decCallback: yv12ToRGB888 error"; break; } QMetaObject::invokeMethod( this_->ui_->label, "setPixmap", Qt::QueuedConnection, Q_ARG( QPixmap, QPixmap::fromImage( image ) ) ); break; } } return; } void MainWindow::endCallback(long nPort, void *pUser) { auto this_ = reinterpret_cast< MainWindow * >( pUser ); qDebug() << "endCallback:" << nPort << this_; } void MainWindow::play() { const auto &&filePath = QFileDialog::getOpenFileName( this, "Choose mp4 file", QStandardPaths::writableLocation( QStandardPaths::DesktopLocation ), "mp4 (*.mp4)" ); if ( filePath.isEmpty() ) { return; } ui_->pushButton->hide(); const auto &&local8Bit = filePath.toLocal8Bit(); auto buf = new char[ static_cast< size_t >( local8Bit.size() ) + 1 ]; memcpy( buf, local8Bit.data(), static_cast< size_t >( local8Bit.size() ) ); qDebug() << "PlayM4_OpenFile: return:" << PlayM4_OpenFile( port_, buf ); PlayM4_SetDecCallBackExMend( port_, decCallback, nullptr, 0, reinterpret_cast< void * >( this ) ); PlayM4_SetFileEndCallback( port_, endCallback, reinterpret_cast< void * >( this ) ); PlayM4_Play( port_, nullptr ); // QThread::sleep( 1 ); // PlayM4_Fast( port_ ); // PlayM4_Fast( port_ ); }
这里只列了mainwindow.cpp中的源码,核心的都在这里了。UI上只有一个label用于显示QPixmap还有一个button用户唤出文件选择框。
相关文章推荐
- QT之海康解码显示小技巧
- ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)
- ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)
- QT之海康解码显示小技巧 ~setUpdatesEnabled
- ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)
- [转]QT 显示视频
- 模拟摄像头,AV视频信号线解码,PAL制 NTSC,输入解码显示,终于搞定,记录下!
- 在QT中用QLabel显示一张图像及QImage/QPixmap/QBitmap/QPicture
- 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
- qt生成的exe可执行程序打包到其他电脑上执行时jpg、gif图片不能正常显示,但png等其他格式可以显示
- Qt:实时显示大华摄像头数据到QLabel上
- 视频和QT的GUI显示到LCD的不同显示层—透过GUI能看到视频
- H264的视频格式H264支持4:2:0的连续或隔行视频的编码和解码
- OpenCV3.2+Qt5.8.0+Win10开发视频监控系统----(3)Qt之视频显示窗口固定
- 视频编解码格式比较 h.264 vs h.265 vs Theora (1080p)
- 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
- 音视频编解码和多媒体格式总结
- ShanaEncoder V3.8----视频编解码转换格式利器
- Qt5之扩展对话框与在QLabel中显示中文的办法
- Qt5--学习笔记-+openCV2-客户端、服务端回环视频显示