参考:C++ 获取当前时间,并转换成string类型https://blog.csdn.net/G_66_hero/article/details/97487543
#include<opencv.hpp>
#include<opencv2\imgproc\types_c.h>//解决CV_RGB2GRAY:无法解析的标识符
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include<opencv2\imgproc\types_c.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <chrono>
using namespace cv;
using namespace std;
int main()
{
//1.从摄像头读入视频
VideoCapture cap(0);
//2.循环显示每一帧
while (1)
{
Mat cam;
cap >> cam;//获取相机当前帧图像
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
//转为字符串
std::stringstream ss;
// 可以分别以不同的形式进行显示
ss << std::put_time(std::localtime(&t), "%m-%d_%H:%M:%S");
//ss << std::put_time(std::localtime(&t), "%Y年%m月%d日%H时%M分%S秒");
//ss << std::put_time(std::localtime(&t), "%Y%m%d%H%M%S");
std::string str_time = ss.str();
//添加文本到图像
putText(cam, str_time, Point(50, 50), CV_FONT_HERSHEY_COMPLEX, 2, Scalar(25, 255, 25), 3);
imshow("Camera", cam);//显示当前图像
waitKey(1);//延时
}
return 0;
}