背景
海康给的demo中只有MFC的c++例程,没有QT的。网上其他很多博客都是直接用MFC的开线程取图,我习惯用回调函数进行取图,并和信号槽绑定在一起。
UI界面
核心代码
声明回调函数
static void __stdcall ImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
static void __stdcall ImageCallBackEx2(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
注册回调函数
nRet = m_pcMyCamera[i]->RegisterImageCallBack(i == 0 ? ImageCallBackEx: ImageCallBackEx2,this);
回调函数实现
void __stdcall QtWidgetsApplication2::ImageCallBackEx2(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (pFrameInfo)
{
QtWidgetsApplication2* pThis = (QtWidgetsApplication2*)pUser;
pThis->ui->textBrowserPg1->append( QString("Get One Frame1: Width[%1], Height[%2], nFrameNum[%3]").arg(
pFrameInfo->nWidth).arg(pFrameInfo->nHeight).arg(pFrameInfo->nFrameNum));
QImage showImage;
if (pFrameInfo->enPixelType != PixelType_Gvsp_Mono8) {
showImage = QImage(pData, pFrameInfo->nWidth, pFrameInfo->nHeight, QImage::Format_RGB888);
}
else {
showImage = QImage(pData, pFrameInfo->nWidth, pFrameInfo->nHeight, QImage::Format_Grayscale8);
}
emit pThis->OnceImage2(showImage.copy());
}
}
回调函数触发信号,槽函数编写
connect(this, &QtWidgetsApplication2::OnceImage2, this, [=](QImage img) {
QMutexLocker locker(&image_locker);
showImage_global[1] = img;
update();
});
实时画面刷新
void QtWidgetsApplication2::paintEvent(QPaintEvent* ev)
{
QWidget::paintEvent(ev);
if (!showImage_global[0].isNull() && m_pcMyCamera[0])
{
QThread::msleep(10);
ui->label->setPixmap(QPixmap::fromImage(showImage_global[0]).scaled(QSize(ui->label->width(), ui->label->height()), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
if (!showImage_global[1].isNull() && m_pcMyCamera[1])
{
QThread::msleep(10);
ui->label2->setPixmap(QPixmap::fromImage(showImage_global[1]).scaled(QSize(ui->label2->width(), ui->label2->height()), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
}
问题
相机刷新过快,会导致软件卡顿,目前试过的方法可以通过降低相机帧率,不知道各位大佬有哪些方法?