Grab_Asynchronous.cpp

Get and process image in asynchronous mode.

The sample code below shows how to get and process image in asynchronous mode, avoiding the time consuming problem in synchronous mode.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <mutex>
#include <thread>
#include <iostream>
#include "MvCameraControl.h"
using namespace std;
bool g_bExit = false; // Thread ending marking
#define Max_Count 5 // Number of buffer zones in the queue, which can be adjusted as needed
uint64_t m_nImageSize = 0; // Image size
// Structures of imgae information for custom storage
typedef struct _stImageNode_
{
unsigned char* pData;
uint64_t nFrameLen;
unsigned int nWidth;
unsigned int nHeight;
unsigned int nFrameNum;
}stImageNode;
class ArrayQueue
{
public:
ArrayQueue()
{
this->size = 0;
this->start = 0;
this->end = 0;
this->Queue = NULL;
this->Qlen = 0;
}
~ArrayQueue()
{
g_mutex.lock();
for (int i = 0; i< Qlen; i++)
{
Queue[i].nFrameNum = 0;
Queue[i].nHeight = 0;
Queue[i].nWidth = 0;
Queue[i].nFrameLen = 0;
if(Queue[i].pData != NULL)
{
free(Queue[i].pData);
Queue[i].pData = NULL;
}
}
delete []Queue;
Queue = NULL;
size = 0;
start = 0;
end = 0;
g_mutex.unlock();
}
// Initialize queue
int Init(int nBufCount, uint64_t DefaultImagelen)
{
int nRet = 0 ;
this->Queue = new (std::nothrow)stImageNode[nBufCount];
if (this->Queue == NULL)
{
return MV_E_RESOURCE;
}
this->Qlen = nBufCount;
for (int i = 0; i< nBufCount; i++)
{
Queue[i].nFrameNum = 0;
Queue[i].nHeight = 0;
Queue[i].nWidth = 0;
Queue[i].nFrameLen = 0;
Queue[i].pData = (unsigned char*)malloc(DefaultImagelen);
if(NULL == Queue[i].pData)
{
return MV_E_RESOURCE;
}
}
return 0;
}
// Put data into queue
int push(int nFrameNum, int nHeight, int nWidth, unsigned char *pData, uint64_t nFrameLen)
{
g_mutex.lock();
if (size==Qlen)
{
g_mutex.unlock();
return MV_E_BUFOVER;
}
size++;
Queue[end].nFrameNum = nFrameNum;
Queue[end].nHeight = nHeight;
Queue[end].nWidth = nWidth;
Queue[end].nFrameLen = nFrameLen;
if (NULL != Queue[end].pData && NULL != pData)
{
memcpy(Queue[end].pData, pData, nFrameLen);
}
end = end == Qlen - 1 ? 0 : end + 1;
g_mutex.unlock();
return 0;
}
// Get data from queue
int poll(int &nFrameNum, int &nHeight, int &nWidth, unsigned char *pData, int &nFrameLen)
{
g_mutex.lock();
if (size == 0)
{
g_mutex.unlock();
return MV_E_NODATA;
}
nFrameNum =Queue[start].nFrameNum;
nHeight =Queue[start].nHeight;
nWidth =Queue[start].nWidth;
nFrameLen =Queue[start].nFrameLen;
if (NULL != pData && NULL != Queue[start].pData)
{
memcpy( pData,Queue[start].pData, nFrameLen);
}
size--;
start = start == Qlen - 1 ? 0 : start + 1;
g_mutex.unlock();
return 0;
}
private:
stImageNode *Queue;
int size;
int start;
int end;
int Qlen;
std::mutex g_mutex; // Mutex
};
ArrayQueue * m_queue = NULL; // Thread communication queue
// wait for user to input enter to stop grabbing or end the sample program
void PressEnterToExit(void)
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
g_bExit = true;
sleep(1);
}
bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
{
int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
// Print current camera IP and user-defined name
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
{
printf("Device Model Name: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chModelName);
printf("UserDefinedName: %s\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_GIGE_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stGigEInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CAMERALINK_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCMLInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_CXP_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stCXPInfo.chModelName);
}
else if (pstMVDevInfo->nTLayerType == MV_GENTL_XOF_DEVICE)
{
printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chUserDefinedName);
printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chSerialNumber);
printf("Model Name: %s\n\n", pstMVDevInfo->SpecialInfo.stXoFInfo.chModelName);
}
else
{
printf("Not support.\n");
}
return true;
}
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{
if (NULL == pFrameInfo || NULL == pData)
{
printf("ImageCallBackEx Input Param invalid.\n");
return;
}
int nRet = MV_OK;
nRet = m_queue->push(pFrameInfo->nFrameNum, pFrameInfo->nExtendWidth, pFrameInfo->nExtendHeight,pData, pFrameInfo->nFrameLenEx);//固定数组实现队列故是10 9 8 7 6 5 4 3 2 1
if (MV_OK != nRet)
{
printf("Add Image [%d] to list failed. \r\n", pFrameInfo->nFrameNum);
}
else
{
printf("Add Image [%d] to list success. \r\n", pFrameInfo->nFrameNum);
}
return;
}
static void* WorkThread(void* pUser)
{
int nRet = MV_OK;
int nWidth = 0;
int nHeight = 0;
int nFrameLen= 0;
int nFrameNum = 0;
unsigned char *pOutData = (unsigned char *) malloc(m_nImageSize);
if (NULL == pOutData)
{
printf("WorkThread malloc size [%lu] failed. \r\n",m_nImageSize);
return NULL;
}
printf("WorkThread Begin . \r\n");
while(true != g_bExit)
{
nWidth = 0;
nHeight = 0;
nFrameLen = 0;
nFrameNum = 0;
nRet = m_queue->poll(nFrameNum,nHeight,nWidth,pOutData,nFrameLen);
if (MV_OK != nRet)
{
usleep(1*1000*2);// Adjust as needed; // Polling failure may due to lack of nodes
continue;
}
else
{
printf("Get nWidth [%d] nHeight [%d] nFrameNum [%d] \r\n",nWidth,nHeight,nFrameNum);
// Process image as needed
#if 0
FILE* fp = NULL;
char szFileName[256] = {0};
sprintf(szFileName, "Image_%d_width_%d_height_%d_Len_%d.raw",nFrameNum,nWidth,nHeight,nFrameLen);
fp = fopen(szFileName, "wb+");
if (fp == NULL)
{
return MV_E_RESOURCE;
}
fwrite(pOutData, 1, nFrameLen, fp);
fclose(fp);
fp = NULL;
#endif
}
}
printf("WorkThread exit . \r\n");
if (pOutData)
{
free(pOutData);
pOutData = NULL;
}
return NULL;
}
int main()
{
int nRet = MV_OK;
void* handle = NULL;
do
{
// Initialize SDK
nRet = MV_CC_Initialize();
if (MV_OK != nRet)
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
// Enumerate devices
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
if (stDeviceList.nDeviceNum > 0)
{
for (int i = 0; i < stDeviceList.nDeviceNum; i++)
{
printf("[device %d]:\n", i);
MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
if (NULL == pDeviceInfo)
{
break;
}
PrintDeviceInfo(pDeviceInfo);
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
if (nIndex >= stDeviceList.nDeviceNum)
{
printf("Intput error!\n");
break;
}
// Select device and create handle
nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
if (MV_OK != nRet)
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
// Open device
nRet = MV_CC_OpenDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
int nWidth = 0;
int nHeight = 0;
MVCC_INTVALUE_EX stIntEx = {0};
nRet = MV_CC_GetIntValueEx(handle, "Width", &stIntEx);
if (MV_OK != nRet)
{
printf("Get IntValue fail! nRet [0x%x]\n", nRet);
break;
}
nWidth = stIntEx.nCurValue;
memset(&stIntEx, 0, sizeof(MVCC_INTVALUE_EX));
nRet = MV_CC_GetIntValueEx(handle, "Height", &stIntEx);
if (MV_OK != nRet)
{
printf("Get IntValue fail! nRet [0x%x]\n", nRet);
break;
}
nHeight = stIntEx.nCurValue;
nRet = MV_CC_GetIntValueEx(handle, "PayloadSize", &stIntEx);
if (MV_OK != nRet)
{
printf("Get IntValue fail! nRet [0x%x], use [%d] replace.\n", nRet, nHeight*nWidth*3);
m_nImageSize = nHeight*nWidth*3;
}
else
{
m_nImageSize = stIntEx.nCurValue;
}
// Initialize queue
m_queue = new (std::nothrow)ArrayQueue();
if (m_queue==NULL)
{
printf("m_queue is null! \n");
break;
}
nRet = m_queue->Init(Max_Count,m_nImageSize);
if (MV_OK != nRet)
{
printf("ArrayQueue init fail! nRet [0x%x]\n", nRet);
break;
}
// The optimal package size for detection network (valid for GigE camera only)
if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
{
int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
if (nPacketSize > 0)
{
nRet = MV_CC_SetIntValueEx(handle,"GevSCPSPacketSize",nPacketSize);
if(nRet != MV_OK)
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
nRet = MV_CC_SetBoolValue(handle, "AcquisitionFrameRateEnable", false);
if (MV_OK != nRet)
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
// Set trigger mode to off
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
if (MV_OK != nRet)
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
// Register a callback for grabbing image
nRet = MV_CC_RegisterImageCallBackEx(handle, ImageCallBackEx, handle);
if (MV_OK != nRet)
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
nRet = pthread_create(&nThreadID, NULL ,WorkThread , handle);
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
// Start image acquisition
nRet = MV_CC_StartGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
PressEnterToExit();
// Stop image acquisition
nRet = MV_CC_StopGrabbing(handle);
if (MV_OK != nRet)
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
// Unregister image callback
nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
if (MV_OK != nRet)
{
printf("UnRegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
// close device
nRet = MV_CC_CloseDevice(handle);
if (MV_OK != nRet)
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
// Destroy handle
nRet = MV_CC_DestroyHandle(handle);
if (MV_OK != nRet)
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
handle = NULL;
} while (0);
// Release the resource
if (m_queue)
{
delete m_queue;
m_queue = NULL;
}
printf("free buffer done\n");
if (handle != NULL)
{
handle = NULL;
}
// Finalize SDK
printf("exit.\n");
return 0;
}