代码拉取完成,页面将自动刷新
#include "WHXYCap.h"
#include "sdk/whxy/LibXySensor.h"
#include <chrono>
#include <fstream>
#include <thread>
#ifdef _DEBUG
#pragma comment(lib,"libXySensor.lib")
#else
#pragma comment(lib,"libXySensor.lib")
#endif // DEBUG
class WHXYCaptrue :LibXySensor
{
public:
int w = 0, h = 0, dataFormat = 0;
LibXySensor whxySensor{};
uint64_t ouid = 0;
uint64_t uid = 0;
bool isFramIn = false;
bool isConnected = false;
unsigned char* imgBuffer = nullptr;
public:
void Regist()
{
this->whxySensor.RegistCallback(
std::bind(&WHXYCaptrue::VideoData, this, std::placeholders::_1),
std::bind(&WHXYCaptrue::VideoStatus, this, std::placeholders::_1, std::placeholders::_2));
}
void VideoData(const video_out_s& data)
{
int size = 0;
// 处理视频数据回调
h = data.frameInfo.height;
w = data.frameInfo.width;
dataFormat = data.frameInfo.type; // 假设 type 对应于 imgDataFormat
size = data.frameInfo.height * data.frameInfo.width * 3; // 假设每个像素3字节
if (imgBuffer == nullptr)
{
imgBuffer = new unsigned char[size]; // 假设每个像素3字节
isFramIn = true;
}
std::vector<uint8_t> rgbVec(size);
whxySensor.YuvToRgb(
data.yuvData,
rgbVec, data.frameInfo.width, data.frameInfo.height);
uid = data.frameInfo.uId;
memcpy_s(whxyCaptrue[0].imgBuffer, size, rgbVec.data(), rgbVec.size());
}
void VideoStatus(const ws_tatus_type_e& type, const std::string& msg)
{
isConnected = type == ws_tatus_type_e::Connected;
}
}whxyCaptrue[8];
char* WHXYCap::EnumDevs()
{
std::string sn = "";
//whxySensor.getSn(sn);
size_t len = sn.length();
if (len == 0) return (char*)"";
char* serialNosChar = new char[len + 1] {0};
memcpy_s(serialNosChar, len, sn.c_str(), len);
serialNosChar[len] = '\0';
return serialNosChar;
}
int WHXYCap::Run(int devID, const char* devSerialNo, const char* capFile, int* imgH, int* imgW, int* imgDataFormat)
{
whxyCaptrue[devID].ouid = 0;
whxyCaptrue[devID].isFramIn = false;
whxyCaptrue[devID].isConnected = false;
whxyCaptrue[devID].h = 0, whxyCaptrue[devID].w = 0, whxyCaptrue[devID].dataFormat = 0;
try
{
whxyCaptrue[devID].whxySensor.Connect(std::string(devSerialNo));
whxyCaptrue[devID].Regist();
int timeout = 2000; // 20秒超时
while (!((whxyCaptrue[devID].isConnected && whxyCaptrue[devID].isFramIn) || timeout-- < 0))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (timeout <= 0)
return ERR::EC::Err_DevOpenFail;
//*imgH = whxyCaptrue[devID].h;
//*imgW = whxyCaptrue[devID].w;
//*imgDataFormat = whxyCaptrue[devID].dataFormat;
return ERR::EC::OK;
}
catch (const std::exception&)
{
return ERR::EC::Err_DevOpenFail;
}
}
int WHXYCap::Cap(int devID, int imgH, int imgW, int imgDataFormat, unsigned char* imgBuffer, int* capFramIndex, ENUM::CAPTYPE CT)
{
if (!whxyCaptrue[devID].isConnected)
return ERR::EC::Err_DevOpenFail;
int timeout = 100;
while (!((whxyCaptrue[devID].ouid != whxyCaptrue[devID].uid) || timeout-- < 0))
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (timeout <= 0)
return ERR::EC::Err_NoNewFramIn;
whxyCaptrue[devID].ouid = whxyCaptrue[devID].uid; // 更新上次的帧索引
int size = imgH * imgW * 3; // 假设每个像素3字节
memcpy_s(imgBuffer, size, whxyCaptrue[devID].imgBuffer, size); // 假设每个像素3字节
*capFramIndex = static_cast<int>(whxyCaptrue[devID].uid); // 假设帧索引为0
return ERR::EC::OK;
}
int WHXYCap::Stop(int devID)
{
whxyCaptrue[devID].whxySensor.DisConnect();
if (whxyCaptrue[devID].imgBuffer)
{
delete[](whxyCaptrue[devID].imgBuffer);
whxyCaptrue[devID].imgBuffer = nullptr;
}
return ERR::EC::OK;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。