|
|
@@ -58,6 +58,179 @@ SenderCustom sender_custom;
|
|
|
cv::Mat cv_frame;
|
|
|
|
|
|
|
|
|
+//原始视频流(.rawv)文件格式:未压缩,逐帧顺序存放原始RGBA数据
|
|
|
+//文件头(32字节) + N帧原始帧数据,每帧 stride*height 字节
|
|
|
+namespace {
|
|
|
+
|
|
|
+const int kRawHeaderSize = 32;
|
|
|
+const quint32 kRawVersion = 1;
|
|
|
+
|
|
|
+//写入视频流文件头,frameCount/fps 在录制结束时回填
|
|
|
+void writeRawHeader(QFile *file, int width, int height, int stride, int fps, int frameCount)
|
|
|
+{
|
|
|
+ QByteArray header(kRawHeaderSize, '\0');
|
|
|
+ memcpy(header.data(), "RAWV", 4);
|
|
|
+ auto put = [&header](int offset, quint32 value) {
|
|
|
+ memcpy(header.data() + offset, &value, sizeof(quint32));
|
|
|
+ };
|
|
|
+ put(4, kRawVersion);
|
|
|
+ put(8, static_cast<quint32>(width));
|
|
|
+ put(12, static_cast<quint32>(height));
|
|
|
+ put(16, static_cast<quint32>(stride));
|
|
|
+ put(20, static_cast<quint32>(fps));
|
|
|
+ put(24, static_cast<quint32>(frameCount));
|
|
|
+ if (file->seek(0)) {
|
|
|
+ file->write(header);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//读取视频流文件头字段
|
|
|
+quint32 readHeaderValue(const QByteArray &header, int offset)
|
|
|
+{
|
|
|
+ quint32 value = 0;
|
|
|
+ memcpy(&value, header.constData() + offset, sizeof(quint32));
|
|
|
+ return value;
|
|
|
+}
|
|
|
+
|
|
|
+//原始视频流的帧元信息(分析与融合的后台任务用,避免跨线程访问播放器成员)
|
|
|
+struct RawVideoMeta {
|
|
|
+ QString path;
|
|
|
+ int width = 0;
|
|
|
+ int height = 0;
|
|
|
+ int stride = 0;
|
|
|
+ int frameCount = 0;
|
|
|
+};
|
|
|
+
|
|
|
+//读取第index帧原始数据为BGRA的cv::Mat
|
|
|
+bool readRawVideoFrame(QFile &file, const RawVideoMeta &meta, int index, cv::Mat &out)
|
|
|
+{
|
|
|
+ const qint64 frameBytes = static_cast<qint64>(meta.stride) * meta.height;
|
|
|
+ if(!file.seek(kRawHeaderSize + frameBytes * index)) return false;
|
|
|
+ const QByteArray buffer = file.read(frameBytes);
|
|
|
+ if(buffer.size() < frameBytes) return false;
|
|
|
+ out.create(meta.height, meta.width, CV_8UC4);
|
|
|
+ const int copyBytes = qMin(meta.stride, meta.width * 4);
|
|
|
+ for(int y = 0; y < meta.height; y++){
|
|
|
+ memcpy(out.ptr(y), buffer.constData() + static_cast<qint64>(y) * meta.stride, copyBytes);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+//遍历全部帧,返回聚焦评分(综合Laplacian方差/Tenengrad/Brenner)最高的帧序号,失败返回-1
|
|
|
+int findSharpestFrameIndex(const RawVideoMeta &meta, OpencvFunc &func)
|
|
|
+{
|
|
|
+ QFile file(meta.path);
|
|
|
+ if(!file.open(QIODevice::ReadOnly)){
|
|
|
+ ERROR("findSharpestFrameIndex: 打开视频流文件失败:{}",meta.path.toStdString());
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ int bestIndex = -1;
|
|
|
+ double bestScore = -1.0;
|
|
|
+ cv::Mat frame, small;
|
|
|
+ //降采样后再评分:清晰度是相对量,分辨率降低可大幅减少CPU耗时
|
|
|
+ const cv::Size smallSize(qMax(1, meta.width / 4), qMax(1, meta.height / 4));
|
|
|
+ for(int i = 0; i < meta.frameCount; i++){
|
|
|
+ if(!readRawVideoFrame(file, meta, i, frame)) continue;
|
|
|
+ cv::resize(frame, small, smallSize, 0, 0, cv::INTER_AREA);
|
|
|
+ const double score = func.evaluateFocusScore(small).combinedScore;
|
|
|
+ if(score > bestScore){
|
|
|
+ bestScore = score;
|
|
|
+ bestIndex = i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(bestIndex < 0){
|
|
|
+ ERROR("findSharpestFrameIndex: 未读到有效帧:{}",meta.path.toStdString());
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ LOG("findSharpestFrameIndex: 最清晰帧={} 评分={} 总帧数={}",bestIndex,bestScore,meta.frameCount);
|
|
|
+ return bestIndex;
|
|
|
+}
|
|
|
+
|
|
|
+//读取视频中最清晰的一帧
|
|
|
+bool findSharpestFrame(const RawVideoMeta &meta, OpencvFunc &func, cv::Mat &out, int &bestIndex)
|
|
|
+{
|
|
|
+ bestIndex = findSharpestFrameIndex(meta, func);
|
|
|
+ if(bestIndex < 0) return false;
|
|
|
+ QFile file(meta.path);
|
|
|
+ if(!file.open(QIODevice::ReadOnly)) return false;
|
|
|
+ return readRawVideoFrame(file, meta, bestIndex, out);
|
|
|
+}
|
|
|
+
|
|
|
+//融合:以最清晰帧为中心,向上/下邻域取帧做景深(多焦点)融合
|
|
|
+//离最清晰帧越远的帧越离焦(或处于轴移动过程),混入会明显拉低融合质量,故只取最清晰帧邻域
|
|
|
+bool fuseRawVideoFrames(const RawVideoMeta &meta, OpencvFunc &func, cv::Mat &out, int maxFrames)
|
|
|
+{
|
|
|
+ const int center = findSharpestFrameIndex(meta, func);
|
|
|
+ if(center < 0) return false;
|
|
|
+ //按距最清晰帧的远近依次取帧:0,-1,+1,-2,+2...
|
|
|
+ std::vector<int> indices;
|
|
|
+ indices.reserve(maxFrames);
|
|
|
+ indices.push_back(center);
|
|
|
+ for(int d = 1; static_cast<int>(indices.size()) < maxFrames; d++){
|
|
|
+ if(center - d >= 0) indices.push_back(center - d);
|
|
|
+ if(static_cast<int>(indices.size()) >= maxFrames) break;
|
|
|
+ if(center + d < meta.frameCount) indices.push_back(center + d);
|
|
|
+ if(center - d < 0 && center + d >= meta.frameCount) break;
|
|
|
+ }
|
|
|
+ QFile file(meta.path);
|
|
|
+ if(!file.open(QIODevice::ReadOnly)){
|
|
|
+ ERROR("fuseRawVideoFrames: 打开视频流文件失败:{}",meta.path.toStdString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ std::vector<cv::Mat> frames;
|
|
|
+ frames.reserve(indices.size());
|
|
|
+ for(int index : indices){
|
|
|
+ cv::Mat frame, bgr;
|
|
|
+ if(!readRawVideoFrame(file, meta, index, frame)) continue;
|
|
|
+ cv::cvtColor(frame, bgr, cv::COLOR_BGRA2BGR);
|
|
|
+ frames.push_back(bgr);
|
|
|
+ }
|
|
|
+ if(frames.empty()) return false;
|
|
|
+ if(frames.size() == 1){
|
|
|
+ out = frames.front();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ LOG("fuseRawVideoFrames: 融合中心帧={} 参与融合帧数={} (总帧数={})",
|
|
|
+ center,(int)frames.size(),meta.frameCount);
|
|
|
+ //outDir传空:融合输入拷贝不再额外落盘,结果由调用方按归档命名保存
|
|
|
+ out = func.fuseAdvanced(frames, QString());
|
|
|
+ return !out.empty();
|
|
|
+}
|
|
|
+
|
|
|
+//结果图片统一后处理:锐化(Unsharp Mask)+对比度增强,与拍照锐化图参数一致
|
|
|
+cv::Mat enhanceResultImage(OpencvFunc &func, const cv::Mat &image)
|
|
|
+{
|
|
|
+ cv::Mat bgr;
|
|
|
+ if(image.channels() == 4) cv::cvtColor(image, bgr, cv::COLOR_BGRA2BGR);
|
|
|
+ else bgr = image;
|
|
|
+ cv::Mat sharp = func.unsharpMask(bgr, 2.0, 3.0);
|
|
|
+ return func.enhanceContrast(sharp, 0);
|
|
|
+}
|
|
|
+
|
|
|
+//保存结果图片(jpg,质量95,与拍照图片一致):左上角红色文件名水印
|
|
|
+bool saveResultImage(const QString &path, const cv::Mat &image)
|
|
|
+{
|
|
|
+ QDir().mkpath(QFileInfo(path).absolutePath());
|
|
|
+ cv::Mat out = image.clone();
|
|
|
+ cv::putText(out,
|
|
|
+ QFileInfo(path).fileName().toStdString(),
|
|
|
+ cv::Point(15, 50),
|
|
|
+ cv::FONT_HERSHEY_SIMPLEX,
|
|
|
+ 3.0, //增大水印字体大小
|
|
|
+ cv::Scalar(0,0,255), //红色 red
|
|
|
+ 3);
|
|
|
+ std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 95};
|
|
|
+ if(!cv::imwrite(path.toStdString(), out, params)){
|
|
|
+ ERROR("saveResultImage: 保存失败:{}",path.toStdString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ LOG("saveResultImage: 已保存:{}",path.toStdString());
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+
|
|
|
FPSTimer::FPSTimer(QObject *parent) : QObject(parent)
|
|
|
{
|
|
|
m_timer = new QTimer(this);
|
|
|
@@ -158,6 +331,21 @@ Frames::Frames(QObject * parent)
|
|
|
Frames::~Frames()
|
|
|
{
|
|
|
qDebug() << "=== Frames Destructor Start ===";
|
|
|
+ // 0. 若仍在录制,回填文件头并关闭视频流文件(析构期间不回发信号,避免触发QML逻辑)
|
|
|
+ if (m_recording) {
|
|
|
+ m_recording = false;
|
|
|
+ if (m_recordFile) {
|
|
|
+ if (m_recordFrameCount > 0) {
|
|
|
+ const qint64 elapsed_ms = m_recordElapsed.elapsed();
|
|
|
+ int fps = elapsed_ms > 0 ? qRound(m_recordFrameCount * 1000.0 / elapsed_ms) : 0;
|
|
|
+ writeRawHeader(m_recordFile, m_recordWidth, m_recordHeight, m_recordStride,
|
|
|
+ fps > 0 ? fps : 1, m_recordFrameCount);
|
|
|
+ }
|
|
|
+ m_recordFile->close();
|
|
|
+ delete m_recordFile;
|
|
|
+ m_recordFile = nullptr;
|
|
|
+ }
|
|
|
+ }
|
|
|
// 1. 先停止所有定时器
|
|
|
if (m_1s && m_1s->isActive()) {
|
|
|
m_1s->stop();
|
|
|
@@ -497,6 +685,8 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
#endif
|
|
|
}
|
|
|
}else{ //关闭相机
|
|
|
+ //关闭相机前先结束录制,保证已录制的视频流落盘
|
|
|
+ stopRecord();
|
|
|
if(param::camera_type.contains("qcamera")){
|
|
|
|
|
|
}else if(param::camera_type.contains("uvc")){
|
|
|
@@ -753,13 +943,13 @@ Frames::saveMatAsJpeg(const QVideoFrame &frame){
|
|
|
cv::Mat fused_sharp = m_opencvfunc.unsharpMask(image, 2.0, 3.0);
|
|
|
//对比度
|
|
|
cv::Mat Contrast_Image = m_opencvfunc.enhanceContrast(fused_sharp,0);
|
|
|
- //左上角水印:浅绿色字体,文字为图片名称(不含目录)
|
|
|
+ //左上角水印:红色字体,文字为图片名称(不含目录)
|
|
|
cv::putText(Contrast_Image,
|
|
|
QFileInfo(name_sharp).fileName().toStdString(),
|
|
|
- cv::Point(15, 40),
|
|
|
+ cv::Point(15, 50),
|
|
|
cv::FONT_HERSHEY_SIMPLEX,
|
|
|
- 2.2, //增大水印字体大小
|
|
|
- cv::Scalar(144,238,144), //浅绿色 lightgreen
|
|
|
+ 3.0, //增大水印字体大小
|
|
|
+ cv::Scalar(0,0,255), //红色 red
|
|
|
3);
|
|
|
std::vector<int> jpegParams = {cv::IMWRITE_JPEG_QUALITY, 95};
|
|
|
if (!cv::imwrite(name_sharp.toStdString(), Contrast_Image, jpegParams)) {
|
|
|
@@ -820,6 +1010,10 @@ Frames::processFrameAsync(const RawFrame& frame){
|
|
|
default:
|
|
|
return;
|
|
|
}
|
|
|
+ //录制中:把未压缩的原始帧写入视频流文件(与实时显示使用同一份数据)
|
|
|
+ if (m_recording) {
|
|
|
+ writeRecordFrame(m_frame);
|
|
|
+ }
|
|
|
QVideoFrameFormat vfmt(
|
|
|
QSize(m_frame.cols, m_frame.rows),
|
|
|
QVideoFrameFormat::Format_RGBA8888
|
|
|
@@ -938,6 +1132,7 @@ void
|
|
|
Frames::registerQmlType()
|
|
|
{
|
|
|
qmlRegisterType< Qt6QCamera::Frames > ( "Frames", 0, 1, "Frames" );
|
|
|
+ qmlRegisterType< Qt6QCamera::RawVideoPlayer > ( "Frames", 0, 1, "RawVideoPlayer" );
|
|
|
}
|
|
|
|
|
|
int
|
|
|
@@ -1078,5 +1273,364 @@ Frames::manualCapture(){
|
|
|
LOG("manualCapture: 收到抓图指令!");
|
|
|
}
|
|
|
|
|
|
+//*录制按钮:开始录制原始(未压缩)视频流,存放到相机打开时创建的归档目录*/
|
|
|
+bool
|
|
|
+Frames::startRecord(){
|
|
|
+ if(m_recording) return true;
|
|
|
+ if(!camera_sw()){
|
|
|
+ WARN("startRecord: 相机未打开,无法录制!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(picture_path_floader.isEmpty()){
|
|
|
+ ERROR("startRecord: 归档目录不存在,无法录制!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ createFolder(picture_path_floader);
|
|
|
+ QString path = picture_path_floader + "/record_" +
|
|
|
+ QDateTime::currentDateTime().toString("yyyyMMddhhmmss") + ".rawv";
|
|
|
+ QFile *file = new QFile(path);
|
|
|
+ if(!file->open(QIODevice::WriteOnly | QIODevice::Truncate)){
|
|
|
+ ERROR("startRecord: 创建视频流文件失败:{}",path.toStdString());
|
|
|
+ delete file;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //先写入占位文件头,停止录制时回填帧数与帧率
|
|
|
+ writeRawHeader(file, 0, 0, 0, 0, 0);
|
|
|
+ {
|
|
|
+ QMutexLocker locker(&m_recordMutex);
|
|
|
+ m_recordFile = file;
|
|
|
+ m_recordPath = path;
|
|
|
+ m_recordWidth = 0;
|
|
|
+ m_recordHeight = 0;
|
|
|
+ m_recordStride = 0;
|
|
|
+ m_recordFrameCount = 0;
|
|
|
+ m_recordElapsed.start();
|
|
|
+ m_recording = true;
|
|
|
+ }
|
|
|
+ LOG("startRecord: 开始录制原始视频流:{}",path.toStdString());
|
|
|
+ emit recordingChanged();
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+//*停止录制:回填文件头并关闭文件,视频流保存到文件系统*/
|
|
|
+void
|
|
|
+Frames::stopRecord(){
|
|
|
+ QFile *file = nullptr;
|
|
|
+ QString path;
|
|
|
+ int width = 0, height = 0, stride = 0, frameCount = 0;
|
|
|
+ {
|
|
|
+ QMutexLocker locker(&m_recordMutex);
|
|
|
+ if(!m_recording) return;
|
|
|
+ m_recording = false;
|
|
|
+ file = m_recordFile;
|
|
|
+ m_recordFile = nullptr;
|
|
|
+ path = m_recordPath;
|
|
|
+ width = m_recordWidth;
|
|
|
+ height = m_recordHeight;
|
|
|
+ stride = m_recordStride;
|
|
|
+ frameCount = m_recordFrameCount;
|
|
|
+ }
|
|
|
+ if(file){
|
|
|
+ //写入过程中若因写盘失败被关闭,这里重新打开以便回填文件头
|
|
|
+ if(!file->isOpen()){
|
|
|
+ file->open(QIODevice::ReadWrite);
|
|
|
+ }
|
|
|
+ const qint64 elapsed_ms = m_recordElapsed.elapsed();
|
|
|
+ if(frameCount > 0){
|
|
|
+ //按实际录制时长换算平均帧率,播放时按该帧率回放
|
|
|
+ int fps = elapsed_ms > 0 ? qRound(frameCount * 1000.0 / elapsed_ms) : 0;
|
|
|
+ if(fps <= 0) fps = 1;
|
|
|
+ writeRawHeader(file, width, height, stride, fps, frameCount);
|
|
|
+ }
|
|
|
+ file->close();
|
|
|
+ delete file;
|
|
|
+ //没有录制到任何有效帧时删除空文件
|
|
|
+ if(frameCount <= 0) QFile::remove(path);
|
|
|
+ }
|
|
|
+ LOG("stopRecord: 录制结束,共{}帧,文件:{}",frameCount,path.toStdString());
|
|
|
+ emit recordingChanged();
|
|
|
+ if(frameCount > 0) emit recordFileSaved(path);
|
|
|
+}
|
|
|
+
|
|
|
+//*取流线程:把当前未压缩原始帧写入视频流文件*/
|
|
|
+void
|
|
|
+Frames::writeRecordFrame(const cv::Mat &frame){
|
|
|
+ QMutexLocker locker(&m_recordMutex);
|
|
|
+ if(!m_recording || !m_recordFile || frame.empty() || !frame.isContinuous()) return;
|
|
|
+ const int stride = frame.cols * static_cast<int>(frame.elemSize());
|
|
|
+ if(m_recordWidth == 0){
|
|
|
+ //以第一帧确定录制尺寸
|
|
|
+ m_recordWidth = frame.cols;
|
|
|
+ m_recordHeight = frame.rows;
|
|
|
+ m_recordStride = stride;
|
|
|
+ }
|
|
|
+ //尺寸变化(如分辨率切换)的帧直接跳过,保证文件内每帧大小一致
|
|
|
+ if(frame.cols != m_recordWidth || frame.rows != m_recordHeight) return;
|
|
|
+ //之前写入失败已关闭文件,不再继续尝试
|
|
|
+ if(!m_recordFile->isOpen()) return;
|
|
|
+ const qint64 size = static_cast<qint64>(stride) * frame.rows;
|
|
|
+ if(m_recordFile->write(reinterpret_cast<const char*>(frame.data), size) != size){
|
|
|
+ ERROR("writeRecordFrame: 写入视频流失败,停止写入:{}",m_recordPath.toStdString());
|
|
|
+ m_recordFile->close();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ m_recordFrameCount++;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//
|
|
|
+// RawVideoPlayer
|
|
|
+//
|
|
|
+
|
|
|
+RawVideoPlayer::RawVideoPlayer(QObject *parent)
|
|
|
+ : QObject(parent)
|
|
|
+{
|
|
|
+ m_timer.setTimerType(Qt::PreciseTimer);
|
|
|
+ connect(&m_timer, &QTimer::timeout, this, &RawVideoPlayer::onTick);
|
|
|
+}
|
|
|
+
|
|
|
+void
|
|
|
+RawVideoPlayer::setVideoSink(QVideoSink *newVideoSink)
|
|
|
+{
|
|
|
+ if(m_videoSink == newVideoSink) return;
|
|
|
+ m_videoSink = newVideoSink;
|
|
|
+ emit videoSinkChanged();
|
|
|
+}
|
|
|
+
|
|
|
+bool
|
|
|
+RawVideoPlayer::isRawVideo(const QString &path) const
|
|
|
+{
|
|
|
+ return path.endsWith(".rawv", Qt::CaseInsensitive);
|
|
|
+}
|
|
|
+
|
|
|
+//*打开原始视频流文件,读取文件头并定位到第一帧*/
|
|
|
+bool
|
|
|
+RawVideoPlayer::open(const QString &path)
|
|
|
+{
|
|
|
+ close();
|
|
|
+ m_file.setFileName(path);
|
|
|
+ if(!m_file.open(QIODevice::ReadOnly)){
|
|
|
+ ERROR("RawVideoPlayer: 打开视频流文件失败:{}",path.toStdString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ const QByteArray header = m_file.read(kRawHeaderSize);
|
|
|
+ if(header.size() != kRawHeaderSize || memcmp(header.constData(), "RAWV", 4) != 0){
|
|
|
+ ERROR("RawVideoPlayer: 不是有效的原始视频流文件:{}",path.toStdString());
|
|
|
+ m_file.close();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ m_width = static_cast<int>(readHeaderValue(header, 8));
|
|
|
+ m_height = static_cast<int>(readHeaderValue(header, 12));
|
|
|
+ m_stride = static_cast<int>(readHeaderValue(header, 16));
|
|
|
+ m_fps = static_cast<int>(readHeaderValue(header, 20));
|
|
|
+ m_frameCount = static_cast<int>(readHeaderValue(header, 24));
|
|
|
+ if(m_width <= 0 || m_height <= 0 || m_stride <= 0 || m_frameCount <= 0){
|
|
|
+ ERROR("RawVideoPlayer: 视频流文件内容为空:{}",path.toStdString());
|
|
|
+ m_file.close();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //未记录到有效帧率时按10fps回放
|
|
|
+ if(m_fps <= 0) m_fps = 10;
|
|
|
+ m_timer.setInterval(qMax(1, qRound(1000.0 / m_fps)));
|
|
|
+ m_source = path;
|
|
|
+ m_position = 0;
|
|
|
+ m_loaded = true;
|
|
|
+ emit sourceChanged();
|
|
|
+ emit loadedChanged();
|
|
|
+ emit frameCountChanged();
|
|
|
+ emit positionChanged();
|
|
|
+ LOG("RawVideoPlayer: 打开视频流成功:{} 尺寸:{}x{} 帧数:{} 帧率:{}",
|
|
|
+ path.toStdString(),m_width,m_height,m_frameCount,m_fps);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+//*播放:从当前帧开始按录制帧率回放*/
|
|
|
+void
|
|
|
+RawVideoPlayer::play()
|
|
|
+{
|
|
|
+ if(!m_loaded) return;
|
|
|
+ if(m_position >= m_frameCount) seek(0);
|
|
|
+ if(!m_timer.isActive()) m_timer.start();
|
|
|
+ if(!m_playing){
|
|
|
+ m_playing = true;
|
|
|
+ emit playingChanged();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void
|
|
|
+RawVideoPlayer::pause()
|
|
|
+{
|
|
|
+ if(m_timer.isActive()) m_timer.stop();
|
|
|
+ if(m_playing){
|
|
|
+ m_playing = false;
|
|
|
+ emit playingChanged();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void
|
|
|
+RawVideoPlayer::stop()
|
|
|
+{
|
|
|
+ pause();
|
|
|
+ seek(0);
|
|
|
+}
|
|
|
+
|
|
|
+//*定位到指定帧并立即刷新画面*/
|
|
|
+void
|
|
|
+RawVideoPlayer::seek(int frameIndex)
|
|
|
+{
|
|
|
+ if(!m_loaded) return;
|
|
|
+ m_position = qBound(0, frameIndex, m_frameCount - 1);
|
|
|
+ const qint64 frameBytes = static_cast<qint64>(m_stride) * m_height;
|
|
|
+ if(m_file.seek(kRawHeaderSize + frameBytes * m_position)){
|
|
|
+ pushFrame();
|
|
|
+ }
|
|
|
+ emit positionChanged();
|
|
|
+}
|
|
|
+
|
|
|
+void
|
|
|
+RawVideoPlayer::close()
|
|
|
+{
|
|
|
+ m_timer.stop();
|
|
|
+ if(m_file.isOpen()) m_file.close();
|
|
|
+ m_loaded = false;
|
|
|
+ m_playing = false;
|
|
|
+ m_source.clear();
|
|
|
+ m_width = 0;
|
|
|
+ m_height = 0;
|
|
|
+ m_stride = 0;
|
|
|
+ m_fps = 0;
|
|
|
+ m_frameCount = 0;
|
|
|
+ m_position = 0;
|
|
|
+ m_resultSerial = 0;
|
|
|
+ emit playingChanged();
|
|
|
+ emit loadedChanged();
|
|
|
+ emit sourceChanged();
|
|
|
+ emit frameCountChanged();
|
|
|
+ emit positionChanged();
|
|
|
+}
|
|
|
+
|
|
|
+void
|
|
|
+RawVideoPlayer::setBusy(bool busy)
|
|
|
+{
|
|
|
+ if(m_busy == busy) return;
|
|
|
+ m_busy = busy;
|
|
|
+ emit busyChanged();
|
|
|
+}
|
|
|
+
|
|
|
+//*结果图片路径:命名与拍照图片一致 <时间戳>z<z>x<x>[_sharp]_<序号>.jpg*/
|
|
|
+QString
|
|
|
+RawVideoPlayer::resultPath(int serial, bool sharp) const
|
|
|
+{
|
|
|
+ const QFileInfo info(m_source);
|
|
|
+ //视频文件名 record_20260915165728.rawv → 时间戳 20260915165728
|
|
|
+ QString ts = info.completeBaseName();
|
|
|
+ const int us = ts.lastIndexOf('_');
|
|
|
+ if(us >= 0) ts = ts.mid(us + 1);
|
|
|
+ //视频不含电机步数,统一以 z0x0 命名
|
|
|
+ return info.absolutePath() + "/" + ts + "z0x0" +
|
|
|
+ (sharp ? "_sharp" : "") + "_" + QString::number(serial) + ".jpg";
|
|
|
+}
|
|
|
+
|
|
|
+//*分析:识别视频帧中最清晰的一帧,锐化+对比度增强后保存为图片*/
|
|
|
+void
|
|
|
+RawVideoPlayer::analyzeSharpestFrame()
|
|
|
+{
|
|
|
+ if(m_busy || !m_loaded) return;
|
|
|
+ //序号在主线程自增,后台任务只使用快照,避免跨线程访问成员
|
|
|
+ const QString outPath = resultPath(++m_resultSerial, false);
|
|
|
+ const RawVideoMeta meta{m_source, m_width, m_height, m_stride, m_frameCount};
|
|
|
+ setBusy(true);
|
|
|
+ LOG("analyzeSharpestFrame: 开始分析最清晰帧:{}",m_source.toStdString());
|
|
|
+ QtConcurrent::run([this, meta, outPath]() {
|
|
|
+ QString result;
|
|
|
+ cv::Mat sharpest;
|
|
|
+ int bestIndex = -1;
|
|
|
+ OpencvFunc func;
|
|
|
+ if(findSharpestFrame(meta, func, sharpest, bestIndex)){
|
|
|
+ if(saveResultImage(outPath, enhanceResultImage(func, sharpest))){
|
|
|
+ result = outPath;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ QMetaObject::invokeMethod(this, [this, result]() {
|
|
|
+ setBusy(false);
|
|
|
+ emit analyzeFinished(result);
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+//*融合:对视频帧做景深融合,锐化+对比度增强后保存为图片*/
|
|
|
+void
|
|
|
+RawVideoPlayer::fuseVideoFrames()
|
|
|
+{
|
|
|
+ if(m_busy || !m_loaded) return;
|
|
|
+ //融合结果与最近一次分析结果成对(_sharp_后缀,便于与原图对比);
|
|
|
+ //若该序号已存在则另开一组,避免覆盖
|
|
|
+ int serial = m_resultSerial > 0 ? m_resultSerial : ++m_resultSerial;
|
|
|
+ QString outPath = resultPath(serial, true);
|
|
|
+ while(QFile::exists(outPath)){
|
|
|
+ serial = ++m_resultSerial;
|
|
|
+ outPath = resultPath(serial, true);
|
|
|
+ }
|
|
|
+ const RawVideoMeta meta{m_source, m_width, m_height, m_stride, m_frameCount};
|
|
|
+ setBusy(true);
|
|
|
+ LOG("fuseVideoFrames: 开始景深融合:{}",m_source.toStdString());
|
|
|
+ QtConcurrent::run([this, meta, outPath]() {
|
|
|
+ QString result;
|
|
|
+ cv::Mat fused;
|
|
|
+ OpencvFunc func;
|
|
|
+ //等间隔抽取帧参与融合:帧数过多时内存与耗时增长明显,取8帧覆盖焦面
|
|
|
+ if(fuseRawVideoFrames(meta, func, fused, 8)){
|
|
|
+ if(saveResultImage(outPath, enhanceResultImage(func, fused))){
|
|
|
+ result = outPath;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ QMetaObject::invokeMethod(this, [this, result]() {
|
|
|
+ setBusy(false);
|
|
|
+ emit fuseFinished(result);
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+void
|
|
|
+RawVideoPlayer::onTick()
|
|
|
+{
|
|
|
+ if(!m_loaded) return;
|
|
|
+ if(m_position >= m_frameCount){
|
|
|
+ //播放到文件末尾,停在最后一帧
|
|
|
+ pause();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(!pushFrame()){
|
|
|
+ ERROR("RawVideoPlayer: 读取视频流帧失败,停止播放:{}",m_source.toStdString());
|
|
|
+ pause();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ m_position++;
|
|
|
+ emit positionChanged();
|
|
|
+}
|
|
|
+
|
|
|
+//*读取当前帧并推送到QVideoSink显示*/
|
|
|
+bool
|
|
|
+RawVideoPlayer::pushFrame()
|
|
|
+{
|
|
|
+ if(!m_file.isOpen()) return false;
|
|
|
+ const qint64 frameBytes = static_cast<qint64>(m_stride) * m_height;
|
|
|
+ const QByteArray buffer = m_file.read(frameBytes);
|
|
|
+ if(buffer.size() < frameBytes) return false;
|
|
|
+ if(!m_videoSink) return true; //没有绑定显示sink时只推进帧序号
|
|
|
+ QVideoFrameFormat vfmt(QSize(m_width, m_height), QVideoFrameFormat::Format_RGBA8888);
|
|
|
+ QVideoFrame frame(vfmt);
|
|
|
+ if(!frame.map(QVideoFrame::WriteOnly)) return false;
|
|
|
+ const int dstStride = frame.bytesPerLine(0);
|
|
|
+ for(int y = 0; y < m_height; y++){
|
|
|
+ memcpy(frame.bits(0) + static_cast<qint64>(y) * dstStride,
|
|
|
+ buffer.constData() + static_cast<qint64>(y) * m_stride,
|
|
|
+ qMin(m_stride, dstStride));
|
|
|
+ }
|
|
|
+ frame.unmap();
|
|
|
+ m_videoSink->setVideoFrame(frame);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
} /* namespace Qt6QCamera */
|