mqtt_finanl
This commit is contained in:
Binary file not shown.
@@ -20,7 +20,7 @@ VideoCapture cap(0);
|
|||||||
Mat handleFrame;
|
Mat handleFrame;
|
||||||
const string mqtt_url = "tcp://192.168.12.1:1883";
|
const string mqtt_url = "tcp://192.168.12.1:1883";
|
||||||
const string clientId = "video_subData";
|
const string clientId = "video_subData";
|
||||||
const string Topic = "/video/Data";
|
const string Topic = "/video/PersonData";
|
||||||
const int Qos = 1;
|
const int Qos = 1;
|
||||||
mqtt::async_client client(mqtt_url, clientId);
|
mqtt::async_client client(mqtt_url, clientId);
|
||||||
|
|
||||||
@@ -57,6 +57,9 @@ int main()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 初始化mqtt
|
||||||
|
MqttInit();
|
||||||
|
|
||||||
// 主处理循环
|
// 主处理循环
|
||||||
mainLoop(cap, pipe);
|
mainLoop(cap, pipe);
|
||||||
|
|
||||||
@@ -90,8 +93,20 @@ void MqttInit()
|
|||||||
void getMsgCallback(mqtt::const_message_ptr msg)
|
void getMsgCallback(mqtt::const_message_ptr msg)
|
||||||
{
|
{
|
||||||
string payload = msg->to_string();
|
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;
|
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);
|
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 2, LINE_8);
|
||||||
|
|
||||||
// FPS计算与显示
|
// 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);
|
fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
|
||||||
fflush(pipe);
|
fflush(pipe);
|
||||||
|
|
||||||
// 可选:显示窗口
|
// 可选:显示窗口
|
||||||
// imshow("测试画面", frame);
|
imshow("测试画面", frame);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -188,16 +212,16 @@ void mainLoop(VideoCapture &cap, FILE *pipe)
|
|||||||
Mat frame;
|
Mat frame;
|
||||||
cout << "开始视频处理循环..." << endl;
|
cout << "开始视频处理循环..." << endl;
|
||||||
|
|
||||||
// 创建全屏窗口
|
// // 创建全屏窗口
|
||||||
namedWindow("处理后的画面", WINDOW_NORMAL);
|
// namedWindow("处理后的画面", WINDOW_NORMAL);
|
||||||
setWindowProperty("处理后的画面", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
|
// setWindowProperty("处理后的画面", WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
|
||||||
|
|
||||||
// 获取屏幕尺寸(通过获取全屏窗口的实际大小)
|
// // 获取屏幕尺寸(通过获取全屏窗口的实际大小)
|
||||||
cv::Rect windowRect = getWindowImageRect("处理后的画面");
|
// cv::Rect windowRect = getWindowImageRect("处理后的画面");
|
||||||
int screenWidth = windowRect.width > 0 ? windowRect.width : 1920;
|
// int screenWidth = windowRect.width > 0 ? windowRect.width : 1920;
|
||||||
int screenHeight = windowRect.height > 0 ? windowRect.height : 1080;
|
// int screenHeight = windowRect.height > 0 ? windowRect.height : 1080;
|
||||||
|
|
||||||
Mat displayFrame; // 用于存储缩放后的画面
|
Mat displayFrame; // 用于存储缩放后的画面
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@@ -205,10 +229,10 @@ void mainLoop(VideoCapture &cap, FILE *pipe)
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将 handleFrame 缩放到全屏尺寸
|
// // 将 handleFrame 缩放到全屏尺寸
|
||||||
resize(handleFrame, displayFrame, Size(screenWidth, screenHeight));
|
// resize(handleFrame, displayFrame, Size(screenWidth, screenHeight));
|
||||||
imshow("处理后的画面", displayFrame);
|
// imshow("处理后的画面", displayFrame);
|
||||||
// 检测退出键
|
// 检测退出键
|
||||||
if (cv::waitKey(1) == 'q')
|
if (cv::waitKey(1) == 'q')
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user