mqtt_finanl

This commit is contained in:
2025-11-08 15:11:06 +08:00
parent fa6bc474dc
commit d77fe2fa5a
2 changed files with 43 additions and 19 deletions

Binary file not shown.

View File

@@ -20,7 +20,7 @@ VideoCapture cap(0);
Mat handleFrame;
const string mqtt_url = "tcp://192.168.12.1:1883";
const string clientId = "video_subData";
const string Topic = "/video/Data";
const string Topic = "/video/PersonData";
const int Qos = 1;
mqtt::async_client client(mqtt_url, clientId);
@@ -57,6 +57,9 @@ int main()
return -1;
}
// 初始化mqtt
MqttInit();
// 主处理循环
mainLoop(cap, pipe);
@@ -90,8 +93,20 @@ void MqttInit()
void getMsgCallback(mqtt::const_message_ptr msg)
{
string payload = msg->to_string();
cout << "Recv payload: " << payload << endl;
cout << payload << endl;
thread([payload]()
{
try {
// TODO 处理接收到到的位置数据
}
catch (const nlohmann::json::parse_error &e) {
cerr << "JSON 解析错误: " << e.what() << "\n原始 payload: " << payload << "\n";
}
catch (const std::exception &e) {
cerr << "处理消息异常: " << e.what() << "\n";
} })
.detach();
}
// 摄像头初始化
@@ -156,8 +171,18 @@ bool processFrame(VideoCapture &cap, FILE *pipe, Mat &frame, int64 &count, chron
// 拷贝视频帧
handleFrame = frame;
auto now_ms = chrono::system_clock::now();
auto timestamp = chrono::duration_cast<chrono::milliseconds>(now_ms.time_since_epoch()).count();
auto now_time = chrono::system_clock::to_time_t(now_ms);
auto ms = timestamp % 1000;
char timeStr[100];
struct tm *tm_info = localtime(&now_time);
sprintf(timeStr, "%02d:%02d:%02d.%03ld",
tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec, ms);
// 添加提示文本
putText(frame, "press 'q' to quit", Point(0, 20),
putText(frame, timeStr, Point(0, 20),
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 2, LINE_8);
// FPS计算与显示
@@ -173,9 +198,8 @@ bool processFrame(VideoCapture &cap, FILE *pipe, Mat &frame, int64 &count, chron
// 写入管道
fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
fflush(pipe);
// 可选:显示窗口
// imshow("测试画面", frame);
imshow("测试画面", frame);
return true;
}
@@ -188,16 +212,16 @@ void mainLoop(VideoCapture &cap, FILE *pipe)
Mat frame;
cout << "开始视频处理循环..." << endl;
// 创建全屏窗口
namedWindow("处理后的画面", WINDOW_NORMAL);
setWindowProperty("处理后的画面", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
// // 创建全屏窗口
// namedWindow("处理后的画面", WINDOW_NORMAL);
// setWindowProperty("处理后的画面", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
// 获取屏幕尺寸(通过获取全屏窗口的实际大小)
cv::Rect windowRect = getWindowImageRect("处理后的画面");
int screenWidth = windowRect.width > 0 ? windowRect.width : 1920;
int screenHeight = windowRect.height > 0 ? windowRect.height : 1080;
// // 获取屏幕尺寸(通过获取全屏窗口的实际大小)
// cv::Rect windowRect = getWindowImageRect("处理后的画面");
// int screenWidth = windowRect.width > 0 ? windowRect.width : 1920;
// int screenHeight = windowRect.height > 0 ? windowRect.height : 1080;
Mat displayFrame; // 用于存储缩放后的画面
Mat displayFrame; // 用于存储缩放后的画面
while (true)
{
@@ -206,9 +230,9 @@ void mainLoop(VideoCapture &cap, FILE *pipe)
break;
}
// 将 handleFrame 缩放到全屏尺寸
resize(handleFrame, displayFrame, Size(screenWidth, screenHeight));
imshow("处理后的画面", displayFrame);
// // 将 handleFrame 缩放到全屏尺寸
// resize(handleFrame, displayFrame, Size(screenWidth, screenHeight));
// imshow("处理后的画面", displayFrame);
// 检测退出键
if (cv::waitKey(1) == 'q')
{