CameraGrabber/savetask.cpp

56 lines
1.6 KiB
C++
Raw Permalink 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.

// SaveTask.cpp 文件中
#include "SaveTask.h"
void SaveTask::run()
{
if (m_rawData.isEmpty())
{
qWarning("SaveTask for camera %s received empty raw data. Aborting save.", qPrintable(m_camName));
return;
}
// --- 耗时的文件 I/O 操作在此处执行,在线程池中 ---
QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz");
// 构造文件名相机名称_时间戳.raw
QString filename = QString("%1_%2.raw")
.arg(m_camName)
.arg(timestamp);
// 使用相机名称创建保存路径的子目录
QString savePath = QDir::currentPath() + "/RawSavedImages/" + m_camName + "/"; // <<< 增加目录
QDir dir(savePath);
if (!dir.exists())
{
if (!dir.mkpath("."))
{
qWarning("Error: Failed to create save directory: %s", qPrintable(savePath));
return;
}
}
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())
{
qWarning() << QString("Error: Only %1 of %2 bytes written to %3")
.arg(bytesWritten).arg(m_rawData.size()).arg(fullPath);
}
// else { qInfo() << "Raw data saved successfully to:" << fullPath; }
}
else
{
qWarning() << "Error opening file for writing:" << fullPath;
}
}