C++
获取当前时间并返回字符串
#include <iostream>
#include <unistd.h>
#include <chrono>
#include <iomanip>
#include <string>
std::string CurrentTimeString()
{
auto now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(¤tTime), "%Y-%m-%d %H:%M:%S");
std::string formattedTime = ss.str();
return formattedTime;
}
C
在YUV420图像上绘制包围盒
int DrawGreenRectOnYuv420p( uint8_t *yuv, int width, int height, int x, int y, int w, int h) {
uint8_t* yuv_y = yuv;
uint8_t* yuv_u = yuv_y + width * height;
uint8_t* yuv_v = yuv_u + width * height / 4;
int y_x = x, y_y = y;
int y_w = w, y_h = h;
int uv_x = x /2, uv_y = y / 2;
int uv_w = w /2, uv_h = h / 2;
for (int i = y_y; i < y_y + y_h; i++) {
uint8_t *ptr = yuv_y + i * width;
ptr[y_x] = ptr[y_x + y_w] = 128;
}
for (int j = y_x; j < y_x + y_w; j++) {
uint8_t *ptr = yuv_y + y_y * width;
ptr[j] = 128;
ptr = yuv_y + (y_y + y_h) * width;
ptr[j] = 128;
}
for (int i = uv_y; i < uv_y + uv_h; i++) {
uint8_t *ptr = yuv_u + i * width / 2;
ptr[uv_x] = ptr[uv_x + uv_w] = 0;
}
for (int j = uv_x; j < uv_x + uv_w; j++) {
uint8_t *ptr = yuv_u + uv_y * width / 2;
ptr[j] = 0;
ptr = yuv_u + (uv_y + uv_h) * width / 2;
ptr[j] = 0;
}
for (int i = uv_y; i < uv_y + uv_h; i++) {
uint8_t *ptr = yuv_v + i * width / 2;
ptr[uv_x] = ptr[uv_x + uv_w] = 0;
}
for (int j = uv_x; j < uv_x + uv_w; j++) {
uint8_t *ptr = yuv_v + uv_y * width / 2;
ptr[j] = 0;
ptr = yuv_v + (uv_y + uv_h) * width / 2;
ptr[j] = 0;
}
return 0;
}
在YUV420SP图像上绘制包围盒和实心矩形
static void DrawGreenRectOnYUV420SP(uint8_t *yuv, int width, int height, int x, int y, int w, int h) {
for (int i = y; i < y + h; i++) {
uint8_t *ptr = yuv + i * width;
ptr[x] = ptr[x + w] = 128;
}
for (int j = x; j < x + w; j++) {
uint8_t *ptr = yuv + y * width;
ptr[j] = 128;
ptr = yuv + (y + h) * width;
ptr[j] = 128;
}
int uvBaseIndex = width * height;
int uvWidth = width / 2;
int uvHeight = height / 2;
for (int i = y / 2; i < (y + h) / 2; i++) {
yuv[uvBaseIndex + 2 * i * uvWidth + x] = 0;
yuv[uvBaseIndex + 2 * i * uvWidth + x + 1] = 0;
yuv[uvBaseIndex + 2 * i * uvWidth + x + w] = 0;
yuv[uvBaseIndex + 2 * i * uvWidth + x + w + 1] = 0;
}
for (int j = x / 2; j < (x + w) / 2; j++) {
yuv[uvBaseIndex + (y) * uvWidth + 2 * j] = 0;
yuv[uvBaseIndex + (y) * uvWidth + 2 * j + 1] = 0;
yuv[uvBaseIndex + (y + h) * uvWidth + 2 * j] = 0;
yuv[uvBaseIndex + (y + h) * uvWidth + 2 * j + 1] = 0;
}
}
static void DrawMaskYUV420SP(uint8_t *yuv, int width, int height, int x, int y, int rectWidth, int rectHeight) {
for (int i = y; i < y + rectHeight; i++) {
for (int j = x; j < x + rectWidth; j++) {
yuv[i * width + j] = 99;
}
}
int uvBaseIndex = width * height;
int uvWidth = width / 2;
int uvHeight = height / 2;
for (int i = y / 2; i < (y + rectHeight) / 2; i++) {
for (int j = x / 2; j < (x + rectWidth) / 2; j++) {
yuv[uvBaseIndex + 2 * i * uvWidth + 2 * j] = 128;
yuv[uvBaseIndex + 2 * i * uvWidth + 2 * j + 1] = 128;
}
}
}