CameraGrabber/savetask.cpp
2025-10-24 18:22:20 +08:00

55 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "SaveTask.h"
// 实际的 run() 方法在 SaveTask.cpp 中实现
void SaveTask::run()
{
if (m_rawData.isEmpty())
{
qWarning("SaveTask received empty raw data. Aborting save.");
return;
}
// --- 耗时的文件 I/O 操作在此处执行,不阻塞主线程 ---
QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz");
// 构造文件名时间戳_宽度x高度_像素类型.raw
QString filename = QString("%1.raw")
.arg(timestamp);
// 设置保存路径
QString savePath = QDir::currentPath() + "/RawSavedImages/";
QDir dir(savePath);
if (!dir.exists())
{
dir.mkpath(".");
}
QString fullPath = savePath + filename;
QFile file(fullPath);
if (file.open(QIODevice::WriteOnly))
{
// 直接将 QByteArray 中的原始字节写入文件
qint64 bytesWritten = file.write(m_rawData);
file.close();
if (bytesWritten == m_rawData.size())
{
// 保存成功,可以发送日志或简单地使用 qInfo()
// qInfo() << "Raw data saved successfully to:" << fullPath;
}
else
{
qWarning() << QString("Error: Only %1 of %2 bytes written to %3")
.arg(bytesWritten).arg(m_rawData.size()).arg(fullPath);
}
}
else
{
qWarning() << "Error opening file for writing:" << fullPath;
}
}