上文中驱动水星工业相机采集视频是采用官方例程修改,利用OpenCV显示函数实现的一个弹出的大窗口,无法控制。想着做一个播放界面,故采取QT实现了一个简单的界面播放器,使用多线程实现视频的采集。
采取QT中的多线程QThread类
直接放上代码 数据采集
// An highlighted block
#include "MERCamera.h"
//using namespace cv;
using namespace std;
QImage m_GlobalImage;
cv::Mat img;
//用户继承掉线事件处理类
class CSampleDeviceOfflineEventHandler : public IDeviceOfflineEventHandler
{
public:
void DoOnDeviceOfflineEvent(void* pUserParam)
{
cout << "收到设备掉线事件!" << endl;
}
};
//用户继承属性更新事件处理类
class CSampleFeatureEventHandler : public IFeatureEventHandler
{
public:
void DoOnFeatureEvent(const GxIAPICPP::gxstring&strFeatureName, void* pUserParam)
{
cout << "收到曝光结束事件!" << endl;
}
};
//用户继承采集事件处理类 用户需要继承ICaptureEventHandler虚基类实现自己的回调处理类
class CSampleCaptureEventHandler : public ICaptureEventHandler
{
public:
void DoOnImageCaptured(CImageDataPointer&objImageDataPointer, void* pUserParam)
{
cout << "收到一帧图像!" << endl;
cout << "ImageInfo: " << objImageDataPointer->GetStatus() << endl;
cout << "ImageInfo: " << objImageDataPointer->GetWidth() << endl;
cout << "ImageInfo: " << objImageDataPointer->GetHeight() << endl;
cout << "ImageInfo: " << objImageDataPointer->GetPayloadSize() << endl;
img.create(objImageDataPointer->GetHeight(), objImageDataPointer->GetWidth(), CV_8UC3);
void *pRGB24Buffer = NULL;
//假设原始数据是BayerRG8图像
pRGB24Buffer = objImageDataPointer->ConvertToRGB24(GX_BIT_0_7, GX_RAW2RGB_NEIGHBOUR, true);
memcpy(img.data, pRGB24Buffer, (objImageDataPointer->GetHeight()) * (objImageDataPointer->GetWidth()) * 3);
cv::flip(img, img, 0);
//cv::imshow("sss", img);
//cv::waitKey(1);
m_GlobalImage = cvMat2QImage(img);
cout << "帧数:" << objImageDataPointer->GetFrameID() << endl;
}
};
int StoreBMP(int PictureNum)
{
//声明事件回调对象指针
IDeviceOfflineEventHandler* pDeviceOfflineEventHandler = NULL;///<掉线事件回调对象
IFeatureEventHandler* pFeatureEventHandler = NULL;///<远端设备事件回调对象
ICaptureEventHandler* pCaptureEventHandler = NULL;///<采集回调对象
//初始化 使用其他接口之前,必须执行初始化
IGXFactory::GetInstance().Init();
try
{
do
{
//枚举设备
gxdeviceinfo_vector vectorDeviceInfo;
IGXFactory::GetInstance().UpdateDeviceList(1000, vectorDeviceInfo);
if (0 == vectorDeviceInfo.size())
{
cout << "无可用设备!" << endl;
break;
}
cout << vectorDeviceInfo[0].GetVendorName() << endl;
cout << vectorDeviceInfo[0].GetSN() << endl;
//打开第一台设备以及设备下面第一个流
CGXDevicePointer ObjDevicePtr = IGXFactory::GetInstance().OpenDeviceBySN(
vectorDeviceInfo[0].GetSN(),
GX_ACCESS_EXCLUSIVE);
CGXStreamPointer ObjStreamPtr = ObjDevicePtr->OpenStream(0);
//注册设备掉线事件【目前只有千兆网系列相机支持此事件通知】
GX_DEVICE_OFFLINE_CALLBACK_HANDLE hDeviceOffline = NULL;
pDeviceOfflineEventHandler = new CSampleDeviceOfflineEventHandler();
hDeviceOffline = ObjDevicePtr->RegisterDeviceOfflineCallback(pDeviceOfflineEventHandler, NULL);
//获取远端设备属性控制器
CGXFeatureControlPointer ObjFeatureControlPtr = ObjDevicePtr->GetRemoteFeatureControl();
//设置曝光时间(示例中写死us,只是示例,并不代表真正可工作参数)
//ObjFeatureControlPtr->GetFloatFeature("ExposureTime")->SetValue(50);
////注册远端设备事件:曝光结束事件【目前只有千兆网系列相机支持曝光结束事件】
////选择事件源
//ObjFeatureControlPtr->GetEnumFeature("EventSelector")->SetValue("ExposureEnd");
////使能事件
//ObjFeatureControlPtr->GetEnumFeature("EventNotification")->SetValue("On");
//GX_FEATURE_CALLBACK_HANDLE hFeatureEvent = NULL;
//pFeatureEventHandler = new CSampleFeatureEventHandler();
//hFeatureEvent = ObjFeatureControlPtr->RegisterFeatureCallback(
// "EventExposureEnd",
// pFeatureEventHandler,
// NULL);
//注册回调采集
pCaptureEventHandler = new CSampleCaptureEventHandler();
ObjStreamPtr->RegisterCaptureCallback(pCaptureEventHandler, NULL);
//发送开采命令
ObjStreamPtr->StartGrab();
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStart")->Execute();
//此时开采成功,控制台打印信息,直到输入任意键继续
getchar();
//发送停采命令
ObjFeatureControlPtr->GetCommandFeature("AcquisitionStop")->Execute();
ObjStreamPtr->StopGrab();
//注销采集回调
ObjStreamPtr->UnregisterCaptureCallback();
////注销远端设备事件
//ObjFeatureControlPtr->UnregisterFeatureCallback(hFeatureEvent);
////注销设备掉线事件
//ObjDevicePtr->UnregisterDeviceOfflineCallback(hDeviceOffline);
//释放资源
ObjStreamPtr->Close();
ObjDevicePtr->Close();
} while (0);
}
catch (CGalaxyException&e)
{
cout << "错误码: " << e.GetErrorCode() << endl;
cout << "错误描述信息: " << e.what() << endl;
}
catch (std::exception&e)
{
cout << "错误描述信息: " << e.what() << endl;
}
//反初始化库 关闭设备之后,不能再调用其他任何库接口
IGXFactory::GetInstance().Uninit();
//销毁事件回调指针
if (NULL != pCaptureEventHandler)
{
delete pCaptureEventHandler;
pCaptureEventHandler = NULL;
}
if (NULL != pDeviceOfflineEventHandler)
{
delete pDeviceOfflineEventHandler;
pDeviceOfflineEventHandler = NULL;
}
if (NULL != pFeatureEventHandler)
{
delete pFeatureEventHandler;
pFeatureEventHandler = NULL;
}
return 0;
}
QImage cvMat2QImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if (mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for (int row = 0; row < mat.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if (mat.type() == CV_8UC4)
{
qDebug() << "CV_8UC4";
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
采取的是回调采集方式,将帧采集函数放在线程中:
//线程入口函数
void SendTask::run()
{
StoreBMP(1);
}
采用定时器刷新QLable上的帧:
//定时器处理函数
void QtCamera::timerEvent(QTimerEvent * event)
{
//可以有多个定时器,每个定时器有不同的处理
if (event->timerId() == m_timeId)
{
ui.label->setPixmap(QPixmap::fromImage(m_GlobalImage));
}
}
结果:
QLabel上播放时视频有点延时,同时还未改进视频的缩放问题,有同学提出:
经过实际测试Qlabel显示image的效率很差,可以使用QWidget,然后在paintEvent里面drawPixmap。
这样的效率提升相当大,16路视频播放时CPU才20%;
QPainter使用的是GPU,显卡不好的话图片与图片显示之间会有白画面,显卡不是很差的话非常流畅。
马上试试。