yangsy
About 736 wordsAbout 2 min
2024-11-08
Caution
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 Failed", 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));
}
The loop sleep time is customizable, not recommended to exceed 20ms
//Data frame
struct SYFrameData
{
//Number of frames
int m_nFrameCount = 0;
//Frame information
SYFrameInfo* m_pFrameInfo = nullptr;
//Frame data
void* m_pData = nullptr;
//Data length
int m_nBuffferLength = 0;
};
//Frame information
struct SYFrameInfo
{
//Frame type
SYFrameType m_frameType = SYFRAMETYPE_NULL;
//Height (pixels)
int m_nFrameHeight = 0;
//Width (pixels)
int m_nFrameWidth = 0;
};
SYFrameData:
Number of data frames - How many data frames are there currently
Frame information - The type, width, and height of the current data frame
Frame data - All data of the current frame
Data length - The length of the current data frame
SYFrameInfo:
Frame type - The type of the current data frame (depth, rgb, raw, ir)
Height (pixels) - The height of the current data frame
Width (pixels) - The width of the current data frame
// 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[nFrameInfo[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;
}
}
}
}
}