You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
#include <iostream>
|
|
#include <opencv2/opencv.hpp>
|
|
#include <zmq.hpp>
|
|
|
|
int main() {
|
|
// 创建ZeroMQ上下文
|
|
zmq::context_t context(1);
|
|
|
|
// 创建套接字并连接到接收方
|
|
zmq::socket_t socket(context, zmq::socket_type::push);
|
|
socket.connect("ipc:///tmp/video_socket");
|
|
|
|
// 打开视频文件
|
|
cv::VideoCapture cap("1.mp4");
|
|
if (!cap.isOpened()) {
|
|
std::cerr << "Failed to open video file" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
// 获取视频帧率
|
|
double fps = cap.get(cv::CAP_PROP_FPS);
|
|
std::cout << "Original FPS: " << fps << std::endl;
|
|
|
|
// 逐帧读取并发送
|
|
cv::Mat frame;
|
|
while (cap.read(frame)) {
|
|
// 获取图像尺寸
|
|
int width = frame.cols;
|
|
int height = frame.rows;
|
|
int type = frame.type();
|
|
|
|
// 创建消息
|
|
zmq::message_t message(sizeof(int) * 3 + frame.total() * frame.elemSize() + sizeof(double));
|
|
|
|
// 将图像尺寸复制到消息
|
|
char* dataPtr = static_cast<char*>(message.data());
|
|
memcpy(dataPtr, &width, sizeof(int));
|
|
dataPtr += sizeof(int);
|
|
memcpy(dataPtr, &height, sizeof(int));
|
|
dataPtr += sizeof(int);
|
|
memcpy(dataPtr, &type, sizeof(int));
|
|
dataPtr += sizeof(int);
|
|
|
|
// 将图像数据复制到消息
|
|
memcpy(dataPtr, frame.data, frame.total() * frame.elemSize());
|
|
dataPtr += frame.total() * frame.elemSize();
|
|
|
|
// 将帧率信息复制到消息
|
|
memcpy(dataPtr, &fps, sizeof(double));
|
|
|
|
// 发送消息
|
|
socket.send(message);
|
|
|
|
// 打印帧率
|
|
std::cout << "Sent FPS: " << fps << std::endl;
|
|
}
|
|
|
|
// 关闭套接字
|
|
socket.close();
|
|
|
|
// 关闭 ZeroMQ 上下文
|
|
context.close();
|
|
|
|
return 0;
|
|
}
|