读取大恒工业相机码流&opencv调用显示

#include "stdafx.h"
#include "GxIAPI.h"
#include "DxImageProc.h" 
#include   
#include   
#include   
#include  

using namespace cv; 
using namespace std;

static void GX_STDC OnFrameCallbackFun(GX_FRAME_CALLBACK_PARAM* pFrame)
{
	int64_t nWidth = pFrame->nWidth;
	int64_t nHeight= pFrame->nHeight;
	BYTE *m_pBufferRaw = new BYTE[nWidth * nHeight];          ///< 原始图像数据
	BYTE *pRGB24Buf = new BYTE[nWidth * nHeight];          
	if (pFrame->status == 0)
	{
		memcpy(m_pBufferRaw, pFrame->pImgBuf, pFrame->nImgSize);
		for (int i = 0; i imageData = (char*)m_pBufferRaw;
		namedWindow("Image:",CV_WINDOW_NORMAL);
		cvShowImage("Image:",src); 
		cvWaitKey(1);
		cvReleaseImage(&src);
		delete []m_pBufferRaw;
		delete []pRGB24Buf;
	}
	return;    
}
int _tmain(int argc, _TCHAR* argv[])
{
	GX_STATUS status = GX_STATUS_SUCCESS;  
	GX_DEV_HANDLE hDevice = NULL;
	GX_OPEN_PARAM stOpenParam;
	uint32_t nDeviceNum = 0;

	//在起始位置调用GXInitLib()进行初始化,申请资源
	status = GXInitLib();
	if (status != GX_STATUS_SUCCESS)
	{
		return 0;
	}
	// 枚举设备列表
	status = GXUpdateDeviceList(&nDeviceNum, 1000);
	if ((status != GX_STATUS_SUCCESS) || (nDeviceNum <= 0))
	{
		return 0;
	}
	//打开设备
	stOpenParam.accessMode = GX_ACCESS_EXCLUSIVE;
	stOpenParam.openMode   = GX_OPEN_INDEX;
	stOpenParam.pszContent = "1";
	status = GXOpenDevice(&stOpenParam, &hDevice);
	if (status == GX_STATUS_SUCCESS)
	{
		//注册图像处理回调函数
		status = GXRegisterCaptureCallback(hDevice, NULL, OnFrameCallbackFun);
		//发送开采命令
		status = GXSendCommand(hDevice, GX_COMMAND_ACQUISITION_START);     


		while(1)
		{
		}
		//发送停采命令
		status = GXSendCommand(hDevice, GX_COMMAND_ACQUISITION_STOP);
		//注销采集回调
		status = GXUnregisterCaptureCallback(hDevice);
	}
	//在结束的时候调用GXCLoseLib()释放资源
	status = GXCloseDevice(hDevice);
	status = GXCloseLib();
	return 0;
}

如若涉及到opencv处理图像部分可设置全局变量,在回调函数中间IplImage* src拷贝出来,后在GXSendCommand函数下进行操作。

以上代码中涉及的opencv及大恒lib、dll请自行添加至环境中。

你可能感兴趣的