QT 工业相机多线程抓拍方式

线程代码:
.h 文件

//抓取结果数据指针
	Pylon::CGrabResultPtr ptrGrabResultM[MAX_IMAGE_RANGE];

.cpp文件

//抓拍线程
void GigeCamera::run()
{
	try {
		// 开始抓取c_countOfImagesToGrab images.
		//相机默认设置连续抓取模式
		cameraM.StartGrabbing();

		while (cameraM.IsGrabbing())
		{
			// 等待接收和恢复图像,超时时间设置为1000 ms.
			imgLockM[grabIndexM].lock();
			cameraM.RetrieveResult(1000, ptrGrabResultM[grabIndexM], Pylon::TimeoutHandling_ThrowException);
			imgLockM[grabIndexM].unlock();

			grabIndexM += 1;
			if (grabIndexM >= MAX_IMAGE_RANGE)
				grabIndexM = 0;
		}

		cameraM.StopGrabbing();
		//Pylon::PylonTerminate();
	}
	catch (GenICam::GenericException &e)
	{
		// Error handling.
		std::cout << "An exception occurred." << std::endl
			<< e.GetDescription() << std::endl;

		cameraM.StopGrabbing();
		Pylon::PylonTerminate();
		return;
	}

}
//获取线程中图片函数
int GigeCamera::SavePhoto(QImage& grabImg)
{
	Pylon::CPylonImage pylonImage;
	imgLockM[genVediolIndexM].lock();
	if (ptrGrabResultM[genVediolIndexM].IsValid() && ptrGrabResultM[genVediolIndexM]->GrabSucceeded())
	{
		//const uint8_t *pImageBuffer = (uint8_t *)ptrGrabResultM[genHalIndexM]->GetBuffer();
		formatConverterM.Convert(pylonImage, ptrGrabResultM[genVediolIndexM]);
		cv::Mat grabTmpm = cv::Mat(ptrGrabResultM[genVediolIndexM]->GetHeight(), ptrGrabResultM[genVediolIndexM]->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer());
		
		cv::Mat rgb;
		cvtColor(grabTmpm, rgb, CV_BGR2RGB);
		QImage rgbImage((const unsigned char*)(rgb.data), rgb.cols, rgb.rows, QImage::Format_RGB888);
		grabImg = rgbImage.copy();
		
		//grabTmp.copyTo(grabImg);
	}
	imgLockM[genVediolIndexM].unlock();

	genVediolIndexM += 1;
	if (genVediolIndexM >= MAX_IMAGE_RANGE)
		genVediolIndexM = 0;
	return 0;
}

使用方式:

gigeCameraM = new GigeCamera();  //创建相机
ui->SerialNumber->addItems(gigeCameraM->getSerialNumber());
	gigeCameraM->openCamera(ui->SerialNumber->currentText());//获取当前选择的相机序列号
	gigeCameraM->start();//启动进程



//获取图片;直接调线程里面的函数
gigeCameraM->SavePhoto(*imgM);

完成。关键代码都在,剩下的自己琢磨。

你可能感兴趣的:(QT)