#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <mutex>
#include <thread>
#include <iostream>
using namespace std;
#define Max_Count 5 //队列 缓冲区个数; 根据实际情况调节
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();
}
int Init(int nBufCount, uint64_t DefaultImagelen)
{
int nRet = 0 ;
this->Queue = new (std::nothrow)stImageNode[nBufCount];
if (this->Queue == NULL)
{
}
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 0;
}
int push(int nFrameNum, int nHeight, int nWidth, unsigned char *pData, uint64_t nFrameLen)
{
g_mutex.lock();
if (size==Qlen)
{
g_mutex.unlock();
}
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;
}
int poll(int &nFrameNum, int &nHeight, int &nWidth, unsigned char *pData, int &nFrameLen)
{
g_mutex.lock();
if (size == 0)
{
g_mutex.unlock();
}
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;
};
{
int c;
while ( (c = getchar()) != '\n' && c != EOF );
fprintf( stderr, "\nPress enter to exit.\n");
while( getchar() != '\n');
sleep(1);
}
{
if (NULL == pstMVDevInfo)
{
printf("The Pointer of pstMVDevInfo is NULL!\n");
return false;
}
{
printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
}
{
}
{
}
{
}
{
}
{
}
else
{
printf("Not support.\n");
}
return true;
}
{
if (NULL == pstFrame || NULL == pstFrame->
pBufAddr)
{
printf("ImageCallbackEx2 Input Param invalid.\n");
return;
}
{
}
else
{
}
if (false == bAutoFree &&
NULL != pUser)
{
}
return;
}
{
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");
{
nWidth = 0;
nHeight = 0;
nFrameLen = 0;
nFrameNum = 0;
nRet =
m_queue->poll(nFrameNum,nHeight,nWidth,pOutData,nFrameLen);
{
usleep(1*1000*2);
continue;
}
else
{
printf("Get nWidth [%d] nHeight [%d] nFrameNum [%d] \r\n",nWidth,nHeight,nFrameNum);
#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)
{
}
fwrite(pOutData, 1, nFrameLen, fp);
fclose(fp);
fp = NULL;
#endif
}
}
printf("WorkThread exit . \r\n");
if (pOutData)
{
free(pOutData);
pOutData = NULL;
}
return NULL;
}
{
do
{
{
printf("Initialize SDK fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
break;
}
{
{
printf("[device %d]:\n", i);
if (NULL == pDeviceInfo)
{
break;
}
}
}
else
{
printf("Find No Devices!\n");
break;
}
printf("Please Intput camera index: ");
unsigned int nIndex = 0;
scanf("%d", &nIndex);
{
printf("Intput error!\n");
break;
}
{
printf("MV_CC_CreateHandle fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_CC_OpenDevice fail! nRet [%x]\n", nRet);
break;
}
int nWidth = 0;
int nHeight = 0;
{
printf("Get IntValue fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("Get IntValue fail! nRet [0x%x]\n", nRet);
break;
}
{
printf("Get IntValue fail! nRet [0x%x], use [%d] replace.\n", nRet, nHeight*nWidth*3);
}
else
{
}
m_queue =
new (std::nothrow)ArrayQueue();
{
printf("m_queue is null! \n");
break;
}
{
printf("ArrayQueue init fail! nRet [0x%x]\n", nRet);
break;
}
{
if (nPacketSize > 0)
{
{
printf("Warning: Set Packet Size fail nRet [0x%x]!\n", nRet);
}
}
else
{
printf("Warning: Get Packet Size fail nRet [0x%x]!\n", nPacketSize);
}
}
{
printf("set AcquisitionFrameRateEnable fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_CC_SetTriggerMode fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_CC_RegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
pthread_t nThreadID;
if (nRet != 0)
{
printf("thread create failed.ret = %d\n",nRet);
break;
}
{
printf("MV_CC_StartGrabbing fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_CC_StopGrabbing fail! nRet [%x]\n", nRet);
break;
}
{
printf("UnRegisterImageCallBackEx fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_CC_CloseDevice fail! nRet [%x]\n", nRet);
break;
}
{
printf("MV_CC_DestroyHandle fail! nRet [%x]\n", nRet);
break;
}
} while (0);
{
}
printf("free buffer done\n");
{
}
printf("exit.\n");
return 0;
}