This commit is contained in:
@@ -615,6 +615,43 @@ namespace BasicDemo
|
||||
}
|
||||
}
|
||||
|
||||
//private void SaveFrameAsImage(int cameraIndex, Bitmap frame)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// if (!recordingFlags[cameraIndex] || string.IsNullOrEmpty(recordingPaths[cameraIndex]))
|
||||
// return;
|
||||
|
||||
// int currentFrame = frameCounters[cameraIndex]++;
|
||||
// string framesFolder = Path.Combine(recordingPaths[cameraIndex], "frames");
|
||||
|
||||
// // 添加相机编号到文件名,方便识别
|
||||
// string imagePath = Path.Combine(framesFolder, $"Cam{cameraIndex + 1}_frame_{currentFrame:D6}.jpg");
|
||||
|
||||
// // 使用高质量压缩保存图片
|
||||
// ImageCodecInfo jpgEncoder = GetEncoderInfo("image/jpeg");
|
||||
// EncoderParameters encoderParams = new EncoderParameters(1);
|
||||
// encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L); // 85%质量
|
||||
|
||||
// lock (locks[cameraIndex])
|
||||
// {
|
||||
// frame.Save(imagePath, jpgEncoder, encoderParams);
|
||||
// }
|
||||
|
||||
// imagePaths[cameraIndex].Add(imagePath);
|
||||
|
||||
// // 每50帧更新一次录制状态
|
||||
// if (currentFrame % 50 == 0)
|
||||
// {
|
||||
// UpdateRecordingStatus(cameraIndex, $"已录制 {currentFrame} 帧", Color.Green);
|
||||
// Console.WriteLine($"相机{cameraIndex + 1}已保存 {currentFrame} 帧");
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine($"保存图片失败(相机{cameraIndex + 1}): {ex.Message}");
|
||||
// }
|
||||
//}
|
||||
private void SaveFrameAsImage(int cameraIndex, Bitmap frame)
|
||||
{
|
||||
try
|
||||
@@ -625,8 +662,12 @@ namespace BasicDemo
|
||||
int currentFrame = frameCounters[cameraIndex]++;
|
||||
string framesFolder = Path.Combine(recordingPaths[cameraIndex], "frames");
|
||||
|
||||
// 添加相机编号到文件名,方便识别
|
||||
string imagePath = Path.Combine(framesFolder, $"Cam{cameraIndex + 1}_frame_{currentFrame:D6}.jpg");
|
||||
// 添加时间戳到文件名中(可选)
|
||||
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmssfff");
|
||||
string imagePath = Path.Combine(framesFolder, $"Cam{cameraIndex + 1}_{timestamp}_frame_{currentFrame:D6}.jpg");
|
||||
|
||||
// 在图片上添加时间戳水印
|
||||
Bitmap watermarkedFrame = AddTimestampWatermark(frame);
|
||||
|
||||
// 使用高质量压缩保存图片
|
||||
ImageCodecInfo jpgEncoder = GetEncoderInfo("image/jpeg");
|
||||
@@ -635,9 +676,12 @@ namespace BasicDemo
|
||||
|
||||
lock (locks[cameraIndex])
|
||||
{
|
||||
frame.Save(imagePath, jpgEncoder, encoderParams);
|
||||
watermarkedFrame.Save(imagePath, jpgEncoder, encoderParams);
|
||||
}
|
||||
|
||||
// 释放水印图片资源
|
||||
watermarkedFrame.Dispose();
|
||||
|
||||
imagePaths[cameraIndex].Add(imagePath);
|
||||
|
||||
// 每50帧更新一次录制状态
|
||||
@@ -653,6 +697,81 @@ namespace BasicDemo
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为图片添加时间戳水印
|
||||
/// </summary>
|
||||
private Bitmap AddTimestampWatermark(Bitmap originalImage)
|
||||
{
|
||||
// 创建新的位图,复制原始图像
|
||||
Bitmap watermarkedImage = new Bitmap(originalImage);
|
||||
|
||||
using (Graphics graphics = Graphics.FromImage(watermarkedImage))
|
||||
{
|
||||
// 设置高质量绘制
|
||||
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||||
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
||||
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
|
||||
|
||||
// 创建字体(可以根据图片大小调整)
|
||||
Font font = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Pixel);
|
||||
|
||||
// 时间戳文本
|
||||
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
|
||||
// 测量文本大小
|
||||
SizeF textSize = graphics.MeasureString(timestamp, font);
|
||||
|
||||
// 计算位置(左上角,留一些边距)
|
||||
int margin = 10;
|
||||
PointF position = new PointF(margin, margin);
|
||||
|
||||
// 添加背景色以提高可读性
|
||||
RectangleF backgroundRect = new RectangleF(
|
||||
position.X - 2,
|
||||
position.Y - 2,
|
||||
textSize.Width + 4,
|
||||
textSize.Height + 4);
|
||||
|
||||
// 绘制半透明背景
|
||||
using (Brush backgroundBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
|
||||
{
|
||||
graphics.FillRectangle(backgroundBrush, backgroundRect);
|
||||
}
|
||||
|
||||
// 绘制时间戳文本
|
||||
using (Brush textBrush = new SolidBrush(Color.White))
|
||||
{
|
||||
graphics.DrawString(timestamp, font, textBrush, position);
|
||||
}
|
||||
|
||||
// 添加相机信息(可选)
|
||||
// 如果你想要在图片中显示相机编号,可以取消下面的注释
|
||||
/*
|
||||
string cameraInfo = $"相机 {GetCurrentCameraIndex() + 1}";
|
||||
SizeF cameraTextSize = graphics.MeasureString(cameraInfo, font);
|
||||
PointF cameraPosition = new PointF(margin, margin + textSize.Height + 5);
|
||||
|
||||
RectangleF cameraBackgroundRect = new RectangleF(
|
||||
cameraPosition.X - 2,
|
||||
cameraPosition.Y - 2,
|
||||
cameraTextSize.Width + 4,
|
||||
cameraTextSize.Height + 4);
|
||||
|
||||
using (Brush backgroundBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
|
||||
{
|
||||
graphics.FillRectangle(backgroundBrush, cameraBackgroundRect);
|
||||
}
|
||||
|
||||
using (Brush textBrush = new SolidBrush(Color.White))
|
||||
{
|
||||
graphics.DrawString(cameraInfo, font, textBrush, cameraPosition);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
return watermarkedImage;
|
||||
}
|
||||
|
||||
// 获取图像编码器
|
||||
private ImageCodecInfo GetEncoderInfo(string mimeType)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user