yangsy
约 820 字大约 3 分钟
2024-11-08
警告
class FrameObserver : public Synexens::ISYFrameObserver
{
public:
virtual void OnFrameNotify(unsigned int nDeviceID, Synexens::SYFrameData* pFrameData = nullptr)
{
ProcessFrameData(nDeviceID, pFrameData);
}
};
FrameObserver g_FrameObserver;
// registration frame data notification
errorCode = Synexens::RegisterFrameObserver(&g_FrameObserver);
if (errorCode == Synexens::SYERRORCODE_SUCCESS)
{
PrintCodeInfo("RegisterFrameObserver Success", errorCode);
}
else
{
PrintCodeInfo("RegisterFrameObserver Falied", errorCode);
}
while (1)
{
for (int nDeviceIndex = 0; nDeviceIndex < nDeviceCount; nDeviceIndex++)
{
Synexens::SYFrameData* pLastFrameData = nullptr;
errorCode = Synexens::GetLastFrameData(pDeviceInfo[nDeviceIndex].m_nDeviceID, pLastFrameData);
if (errorCode == Synexens::SYERRORCODE_SUCCESS)
{
// getLastFrameData Success
ProcessFrameData(pDeviceInfo[nDeviceIndex].m_nDeviceID, pLastFrameData);
}
else
{
// getLatsFrameData failed
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
循环休眠时间自定,建议不超过20ms
//数据帧
struct SYFrameData
{
//帧数量
int m_nFrameCount = 0;
//帧信息
SYFrameInfo* m_pFrameInfo = nullptr;
//帧数据
void* m_pData = nullptr;
//数据长度
int m_nBuffferLength = 0;
};
//数据帧信息
struct SYFrameInfo
{
//帧类型
SYFrameType m_frameType = SYFRAMETYPE_NULL;
//高度(像素)
int m_nFrameHeight = 0;
//宽度(像素)
int m_nFrameWidth = 0;
};
SYFrameData:
数据帧数量 - 当前有几个数据帧
数据帧信息 - 当前数据帧的类型,宽度,高度
帧数据 - 当前帧的所有数据
数据长度 - 当前数据帧长度
SYFrameInfo:
帧类型 - 当前数据帧的类型(depth、rgb、raw、ir)
高度(像素) - 当前数据帧高度
宽度(像素) - 当前数据帧宽
// processing frame data
void ProcessFrameData(unsigned int nDeviceID, Synexens::SYFrameData* pFrameData = nullptr)
{
auto itStreamFind = g_mapStreamType.find(nDeviceID);
auto itStreamOpen = g_mapDeviceOpen.find(nDeviceID);
if (itStreamFind == g_mapStreamType.end() || (itStreamOpen == g_mapDeviceOpen.end() || itStreamOpen->second != true) )
{
return;
}
// RGBD processing logic
if (itStreamFind->second == Synexens::SYSTREAMTYPE_RGBD)
{
std::map<Synexens::SYFrameType, int> mapIndex;
std::map<Synexens::SYFrameType, int> mapPos;
int nPos = 0;
for (int nFrameIndex = 0; nFrameIndex < pFrameData->m_nFrameCount; nFrameIndex++)
{
mapIndex.insert(std::pair<Synexens::SYFrameType, int>(pFrameData->m_pFrameInfo[nFrameIndex].m_frameType, nFrameIndex));
mapPos.insert(std::pair<Synexens::SYFrameType, int>(pFrameData->m_pFrameInfo[nFrameIndex].m_frameType, nPos));
nPos += pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short);
}
auto itDepthIndex = mapIndex.find(Synexens::SYFRAMETYPE_DEPTH);
auto itRGBIndex = mapIndex.find(Synexens::SYFRAMETYPE_RGB);
int nRGBDWidth = pFrameData->m_pFrameInfo[itRGBIndex->second].m_nFrameWidth;
int nRGBDHeight = pFrameData->m_pFrameInfo[itRGBIndex->second].m_nFrameHeight;
unsigned short* pRGBDDepth = new unsigned short[nRGBDWidth * nRGBDHeight];
memset(pRGBDDepth, 0, sizeof(unsigned short) * nRGBDWidth * nRGBDHeight);
unsigned char* pRGBDRGB = new unsigned char[nRGBDWidth * nRGBDHeight * 3];
memset(pRGBDRGB, 0, sizeof(unsigned char) * nRGBDWidth * nRGBDHeight * 3);
Synexens::SYErrorCode errorCode = Synexens::GetRGBD(nDeviceID,
pFrameData->m_pFrameInfo[itDepthIndex->second].m_nFrameWidth,
pFrameData->m_pFrameInfo[itDepthIndex->second].m_nFrameHeight,
(unsigned short*)pFrameData->m_pData + mapPos[Synexens::SYFRAMETYPE_DEPTH],
pFrameData->m_pFrameInfo[itRGBIndex->second].m_nFrameWidth,
pFrameData->m_pFrameInfo[itRGBIndex->second].m_nFrameHeight,
(unsigned char*)pFrameData->m_pData + mapPos[Synexens::SYFRAMETYPE_RGB],
nRGBDWidth,
nRGBDHeight,
pRGBDDepth,
pRGBDRGB);
if (errorCode == Synexens::SYERRORCODE_SUCCESS)
{
// handle your own logic ...
PrintCodeInfo("GetRGBD Success", errorCode);
}
else
{
PrintCodeInfo("GetRGBD Failed", errorCode);
}
delete[] pRGBDDepth;
delete[] pRGBDRGB;
}
else // other
{
int nPos = 0;
for (int nFrameIndex = 0; nFrameIndex < pFrameData->m_nFrameCount; nFrameIndex++)
{
switch (pFrameData->m_pFrameInfo[nFrameIndex].m_frameType)
{
case Synexens::SYFRAMETYPE_RAW:
{
// RawData pointer starting position = pFrameData->m_pData + nPos
// RawData pointer end position = pFrameData->m_pData + pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short)
// handle your own logic ...
nPos += pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short);
break;
}
case Synexens::SYFRAMETYPE_DEPTH:
{
// DepthData pointer starting position = pFrameData->m_pData + nPos
// DepthData pointer end position = pFrameData->m_pData + pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short)
nPos += pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short);
break;
}
case Synexens::SYFRAMETYPE_IR:
{
// IrData pointer starting position = pFrameData->m_pData + nPos
// IrData pointer end position = pFrameData->m_pData + pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short)
// handle your own logic ...
nPos += pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short);
break;
}
case Synexens::SYFRAMETYPE_RGB:
{
// RGBData pointer starting position = pFrameData->m_pData + nPos
// RGBData pointer end position = pFrameData->m_pData + pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short)
// handle your own logic ...
nPos += pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameHeight * pFrameData->m_pFrameInfo[nFrameIndex].m_nFrameWidth * sizeof(short);
break;
}
}
}
}
}