QT+工业相机问题
想用QT+工业相机采集视频在界面上显示,现在已经可以采集到视频,但是在QLabel上播放时视频很卡顿,我是通过信号槽来调动的,是不是相机视频一般用共享内存比较快,这个没试过,,,
下面是相机的主要代码
void Grab::run()
{
int width=659,height=494;
int bytePerLine=(width*24+31)/8;//图像每行字节对齐
unsigned char *graydata=new unsigned char[bytePerLine*height];//存储处理后的数据
const unsigned char *data;
unsigned char r,g,b;
QImage *grayImg;//处理后灰度图像
QSize colourImgSize = QSize(659,494);
QImage grayImg2(colourImgSize, QImage::Format_Indexed8);
// The exit code of the sample application.
// int exitCode = 0;
// Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
// is initialized during the lifetime of this object.
Pylon::PylonAutoInitTerm autoInitTerm;
printf("Unable to allocate libmodbus context\n");
try
{
// Create an instant camera object with the camera device found first.
CInstantCamera camera( CTlFactory::GetInstance().CreateFirstDevice());
// Print the model name of the camera.
cout << "Using device " << camera.GetDeviceInfo().GetModelName() << endl;
// The parameter MaxNumBuffer can be used to control the count of buffers
// allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer = 5;
// Start the grabbing of c_countOfImagesToGrab images.
// The camera device is parameterized with a default configuration which
// sets up free-running continuous acquisition.
camera.StartGrabbing();
// This smart pointer will receive the grab result data.
CGrabResultPtr ptrGrabResult;
int count=0;
// Camera.StopGrabbing() is called automatically by the RetrieveResult() method
// when c_countOfImagesToGrab images have been retrieved.
while ( camera.IsGrabbing())
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
camera.RetrieveResult( 5000, ptrGrabResult, TimeoutHandling_ThrowException);
printf("Grabbing\n");
// Image grabbed successfully?
if (ptrGrabResult->GrabSucceeded()) 、、判断是否成功抓取图像
{
int width = 659;
int height =494;
unsigned char *colourImgDataPtr = (uint8_t *) ptrGrabResult->GetBuffer();
int w_8=(659*8+31)/32*4; //
// QByteArray imageByteArray = QByteArray( (const char*)colourImgDataPtr,659*494 );
//uchar* transData = (unsigned char*)imageByteArray.data();
QSize colourImgSize2(660,494);
QVector<QRgb> grayTable;
for(int i = 0; i < 256; i++)
grayTable.push_back(qRgb(i,i,i));
QImage desImage = QImage(colourImgSize2, QImage::Format_Indexed8);
uchar* transData=desImage.bits();
printf("image %d\n",count);
count++;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
*transData=*colourImgDataPtr;
transData++;
colourImgDataPtr++;
}
transData++;
*transData=150;
}
desImage.setColorTable(grayTable);
emit log(sMessage,&desImage); 发射信号
#ifdef PYLON_WIN_BUILD
// Display the grabbed image.
Pylon::DisplayImage(1, ptrGrabResult);
#endif
}
else
{
sMessage="Error: "+QString::number(ptrGrabResult->GetErrorCode(),10);
}
}
}
catch (GenICam::GenericException &e)
{
//<< e.GetDescription() << endl;
sMessage+="An exception occurred.";//+e.GetDescription();
}
}