首次使用工业相机调用SDK抓拍

首次使用工业相机调用SDK抓拍

感悟:其实单个工业相机调用SDK不是很难,但需要细心和耐心地读开发手册和demo,然后仿照demo去编写程序,使用环境win10+VS2015+Qt5.9.2。
步骤

  1. 载入SDK动态链接库
    1)在工程中引用CameraApi.h头文件;
    2)引用MVCAMSDK_X64.lib库文件;
    3)MVCAMSDK_X64.DLL放在代码同一目录或system32目录下。

  2. 初始化SDK ->CameraSdkInit()

  3. 枚举设备

  4. 初始化设备 ->CameraInit()

  5. 让SDK进入图像采集模式->CameraPlay()

  6. 抓取图像

  7. 关闭相机 ->CameraUnInit()

抓取图像

void test_camera_2::on_ptn_clicked() {
	
	//CameraSoftTrigger(g_hCamera);
	
		if (CameraGetImageBuffer(g_hCamera, &g_tFrameHead, &g_pRawBuffer, 1000) == CAMERA_STATUS_SUCCESS)
		{
			CameraImageProcess(g_hCamera, g_pRawBuffer, g_pRgbBuffer, &g_tFrameHead);
			CameraReleaseImageBuffer(g_hCamera, g_pRawBuffer);


			if (g_tFrameHead.uiMediaType == CAMERA_MEDIA_TYPE_MONO8) {
				
				memcpy(g_readBuf, g_pRgbBuffer, g_W_H_INFO.buffer_size);
				QSize imgsize = ui.label->size();

				QImage Qtemp(g_readBuf, g_W_H_INFO.sensor_width, g_W_H_INFO.sensor_height, QImage::Format_Indexed8);
				
				Qtemp.save("D:/images/qt.jpg", "JPG", 100);
				QImage img = Qtemp.scaled(imgsize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
				ui.label->setPixmap(QPixmap::fromImage(img));
				ui.label->show();


			}
			else {
	
				memcpy(g_readBuf, g_pRgbBuffer, g_W_H_INFO.buffer_size * 3);
				QSize imgsize = ui.label->size();
				QImage Qtemp2 = QImage((const uchar*)g_readBuf, g_W_H_INFO.sensor_width, g_W_H_INFO.sensor_height, QImage::Format_RGB888);
				//Qtemp2.save("D:/images/qtt.jpg", "JPG", 100);
				QImage img = Qtemp2.scaled(imgsize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
				ui.label->setPixmap(QPixmap::fromImage(img));
				ui.label->show();

			}
		}
	}

编译正确、调试界面正确,但没有显示图像,加载一张本地图像可以显示,得出结论,没有抓取到图像。通过阅读demo,发现g_W_H_INFO变量没有赋值。

int test_camera_2::GUI_init_parameter(int hCamera, tSdkCameraCapbility * pCameraInfo)
{
	GUI_set_Resolution(hCamera, pCameraInfo);
	
	return 0;
}

int  test_camera_2::GUI_set_Resolution(int hCamera, tSdkCameraCapbility * pCameraInfo)
{

	tSdkImageResolution     *pImageSizeDesc = pCameraInfo->pImageSizeDesc;// 预设分辨率选择
	tSdkImageResolution     sResolution;  //获取当前设置到分辨率

										  //获得当前预览的分辨率。
	CameraGetImageResolution(hCamera, &sResolution);

	g_W_H_INFO.sensor_width = pImageSizeDesc[sResolution.iIndex].iWidth;
	g_W_H_INFO.sensor_height = pImageSizeDesc[sResolution.iIndex].iHeight;
	g_W_H_INFO.buffer_size = g_W_H_INFO.sensor_width*g_W_H_INFO.sensor_height;

	ui.res_combobox->setCurrentIndex(sResolution.iIndex);

	return 1;
}

int  test_camera_2::GUI_init_Resolution(int hCamera, tSdkCameraCapbility * pCameraInfo)
{
	int                     i = 0;
	tSdkImageResolution     *pImageSizeDesc = pCameraInfo->pImageSizeDesc;// 预设分辨率选择
	int                     iImageSizeDesc = pCameraInfo->iImageSizeDesc; // 预设分辨率的个数,即pImageSizeDesc数组的大小

	for (i = 0; i<iImageSizeDesc; i++) {
		ui.res_combobox->addItem(QString::fromLocal8Bit(pImageSizeDesc[i].acDescription));
	}

	return 1;
}

图像结果显示:
首次使用工业相机调用SDK抓拍_第1张图片

你可能感兴趣的:(相机使用)