| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632 |
- #ifndef M_FRAMES_HPP
- #define M_FRAMES_HPP
- // Qt include.
- #include <QObject>
- #include <QVector>
- #include <QMetaType>
- #include <QVideoSink>
- #include <QCamera>
- #include <QMediaRecorder>
- #include <QMediaCaptureSession>
- #include <QImageCapture>
- #include <QPointer>
- #include <QThread>
- #include <m_opencv.h>
- #include <m_workthread.h>
- #include <mqtthandler.h>
- #include <mserial.h>
- #include <ymodem.h>
- #include <m_json.h>
- #include <m_loger.h>
- #include <QFuture>
- #include <QThreadPool>
- #include <ImageStitcher.h>
- #include <QSemaphore>
- #include <QQuickImageProvider>
- #include <QQmlImageProviderBase>
- #include <QImage>
- #include <QFile>
- #include <QTimer>
- #include <QElapsedTimer>
- #include <QVideoFrame>
- #include <pthread.h>
- //海康
- // #pragma pack(push, 4)
- // #include "MvCameraControl.h"
- // #pragma pack(pop)
- #include <MvCameraControl.h>
- #include <PixelType.h>
- //https://github.com/saki4510t/UVCCamera/pulls
- //https://gitee.com/dengly/UVCCamera
- //USBCamera: https://blog.csdn.net/hanshiying007/article/details/124118486
- #define CameraType 2 //0-QCamera , 1-OPENCV, 2-libuvc
- #define movZ_f "f80" //多图移动,正向80
- #define movZ_b "b80" //多图移动,反向80
- #define movX_f "f80" //多图移动,正向
- #define movX_b "b80" //多图移动,反向
- #define focux_f "f4" //单步对焦,正向
- #define focus_b "b4" //单步对焦,反向
- #define m_foucus_step_max 5
- #define focus_step_max 30
- //性能提升
- //https://blog.csdn.net/u012915226/article/details/115733359
- //https://blog.csdn.net/u012915226/article/details/121745861?spm=1001.2014.3001.5501
- //https://blog.csdn.net/aggs1990/article/details/125694175
- //自定义QVideoSink
- //https://blog.csdn.net/aggs1990/article/details/125361203?spm=1001.2014.3001.5501
- //github: 模块
- //https://github.com/erikmwerner/QUVCView/blob/master/uvccapture.cpp
- namespace Qt6QCamera {
- // 定义一个包含信号的发送类
- class SenderCustom : public QObject {
- Q_OBJECT
- public:
- // 定义一个信号,无参数
- signals:
- void signalToSend(const cv::Mat& frame);
- void signalQStr(const QString &qst);
- public:
- void sendSignal(const cv::Mat& frame) {
- emit signalToSend(frame); // 发送信号
- }
- void sendQStr(const QString &qst){
- signalQStr(qst);
- }
- };
- class FPSTimer : public QObject
- {
- Q_OBJECT
- public:
- explicit FPSTimer(QObject *parent = nullptr);
- public slots:
- void incrementFrame() { m_frame_count++; }
- void setInterval(int interval);
- void setRunning(bool running);
- bool isRunning();
- signals:
- void fpsUpdated(double);
- private slots:
- void onTimerTimeout();
- private:
- QTimer* m_timer = nullptr;
- QElapsedTimer* m_elapsed_timer = nullptr;
- int m_interval = 1000;
- int m_frame_count = 0;
- };
- //
- // Frames
- //
- struct RawFrame
- {
- int width;
- int height;
- MvGvspPixelType pixelType;
- QByteArray data; // 原始 buffer 拷贝
- };
- //! Frames listener.
- class Frames :public QObject//, public QQuickImageProvider //QObject //
- {
- Q_OBJECT
- QML_ELEMENT
- Q_PROPERTY( QVideoSink* videoSink READ videoSink WRITE setVideoSink NOTIFY videoSinkChanged )
- Q_PROPERTY( QString formatString READ formatString NOTIFY formatStringChanged )
- Q_PROPERTY( QString fpsString READ fpsString NOTIFY fpsStringChanged )
- Q_PROPERTY( QString qulityString READ qulityString NOTIFY qulityStringChanged )
- Q_PROPERTY( bool auto_clicked READ auto_clicked WRITE setChange NOTIFY btnStatusChanged )
- Q_PROPERTY( bool camera_sw READ camera_sw WRITE setCameraSw NOTIFY cameraStaChanged )
- Q_PROPERTY(QString cam_ExposureTime READ cam_ExposureTime WRITE setExposureTime NOTIFY exposureTimeChanged)
- Q_PROPERTY( bool recording READ recording NOTIFY recordingChanged )
- public:
- static void registerQmlType();
- explicit Frames(QObject * parent = nullptr);
- ~Frames() override;
- //! Set sink of video output.
- void setVideoSink( QVideoSink * newVideoSink );
- bool isDecoding() { return m_decoding; }
- //! \return Sink of video output.
- QVideoSink* videoSink() const { return m_videoSink; }
- //! set btn
- void setChange(bool stat_t);
- //! 相机状态
- void setCameraSw(bool stat_cam);
- void setExposureTime(const QString &exptime);
- //! \return Rotation angle.
- //! \return Format of video frame as string.
- QString formatString() const;
- //! \return FPS as string.
- QString fpsString() const;
- QString qulityString() const;
- QString cam_ExposureTime() const;
- bool auto_clicked() const;
- bool camera_sw() const;
- void saveImage();
- Q_INVOKABLE void captureImage();
- //抓图按钮:模拟MCU拍照指令(camera_tc=1),下一帧按序号保存/锐化/上传
- Q_INVOKABLE void manualCapture();
- //录制原始(未压缩)视频流:开始录制;停止时回填文件头并把视频流保存到文件系统
- Q_INVOKABLE bool startRecord();
- Q_INVOKABLE void stopRecord();
- bool recording() const { return m_recording; }
- //inline ImageProvider *getImageProvider() { return m_pImgProvider; }
- //ImageProvider* imageProvider() const;
- static void ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
- bool createFolder(const QString &path) {
- QDir dir(path);
- if (dir.exists()) {
- LOG("文件夹已存在:{}",path.toStdString());
- return true;
- }
- if (dir.mkpath(".")) {
- LOG("文件夹创建成功:{}",path.toStdString());
- return true;
- } else {
- ERROR("文件夹创建失败:{}",path.toStdString());
- return false;
- }
- }
- signals:
- //! Video sink changed.
- void videoSinkChanged();
- void cameraOpenFailed(const QString &err);
- void formatStringChanged();
- void fpsStringChanged();
- void qulityStringChanged();
- void exposureTimeChanged();
- void frameAvailable(const QVideoFrame &frame);
- void frameReady(QImage image);
- void btnStatusChanged();
- void sendFocusFlag(QString flag_sta);
- void cameraStaChanged(bool sta_cam);
- void serial_data(const QByteArray &m_byte);
- void signal_stitch();
- //hikrobot
- void startGrabbing(bool sta_cam);
- void signalGrabing(unsigned char * nData);
- void callQmlRefeshImg();
- void signal_m1s();
- void dataUpload(const QByteArray &data); //到mqtt
- void signal_photoerr();
- void stopGrabbing(bool sta_cam);
- void signal_photo_saved();
- void signal_wait_upload(const QString &path);
- void signal_cache_image(const QString &path);
- void signal_sharp_processed(const QString &name_sharp); //锐化/对比度后台处理完成,回主线程统一入队入库
- void stepCountChanged(const QString step);
- void recordingChanged();
- //录制结束,视频流已保存到path
- void recordFileSaved(const QString &path);
- public slots:
- void onMovDone(const QString stepcount){
- if(!camera_sw()){
- ERROR("Camera error!");
- setCameraSw(false);
- return;
- }
- m_qulityString = m_opencvfunc.Tenengrad(m_frame);
- LOG("onMovDone:{}",m_qulityString.toStdString());
- emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
- emit qulityStringChanged();
- emit stepCountChanged(stepcount);
- }
- // void parseFrame(const cv::Mat& frame);
- void upload_handle(void);
- void handle_reult_upload(bool success, const QString &error);
- void onFPSUpdated(double fps);
- private slots:
- //! Init camera.
- void initCam();
- void handleGrab(bool sta_cam);
- //! 1s timer.
- void timer();
- //!
- void update_qulstr(QString q_str);
- //
- void saveMatAsJpeg(const QVideoFrame &frame);
- void onSharpProcessed(const QString &name_sharp);
- //void sendData(QByteArray m_byte);
- void displayFrame(const QVideoFrame &frame);
- void mov_x_z_run();
- void processFrameAsync(const RawFrame& frame);
- //录制中:把当前未压缩原始帧写入视频流文件(取流线程调用)
- void writeRecordFrame(const cv::Mat &frame);
- private:
- Q_DISABLE_COPY( Frames )
- //声明相机捕获对象
- //! Video sink of video output.
- //QPointer< QVideoSink > m_videoSink;
- QVideoSink *m_videoSink;
- //! Format string.
- QString m_formatString;
- //! FPS string.
- QString m_fpsString;
- //! FPS.
- int m_fps;
- //! 清晰度
- QString m_qulityString;
- //! Timer.
- QTimer *m_1s;
- //! QVideoFrame
- OpencvFunc m_opencvfunc;
- JsonFunc m_json;
- QFuture<void> m_grabThread;
- //独立上传队列:与param::ptlist(拼接输入)隔离,每保存一张锐化/对比度图立即入队逐张上传
- QStringList m_uploadQueue;
- bool m_isUploading = false; //是否正在上传,防止并发启动多个上传线程
- QJniObject *jClass;
- bool camera_sta;
- bool m_decoding;
- bool m_streaming_active = false;
- QImage m_image;
- int stitch_des;
- int m_width;
- int m_height;
- QString m_fExposureTime;
- //ImageProvider *m_pImgProvider;
- //libuvc
- mutable QMutex m_frameMutex; // 互斥锁,用于对帧队列的访问进行同步
- FPSTimer* m_fps_timer = nullptr;
- cv::Mat m_frame;
- int image_num = 1;
- int z_step = 0;
- int x_step = 0;
- //保护 image_num/z_step/x_step 及"取号-建名-存图-自增-移轴"整段流程,
- //saveMatAsJpeg 由 QtConcurrent 线程池并发调用,无锁会导致重号覆盖、序号跳变、轴重复移动
- mutable QMutex m_saveMutex;
- //锐化/对比度专用串行线程池(单线程):避免多个重CPU锐化任务同时抢占全局线程池,
- //导致取流线程(handleGrab)被挤占、MV_CC_GetImageBuffer 取流超时
- QThreadPool m_sharpPool;
- //原始视频流录制:录制期间由取流线程同步写入(内存占用恒定,不缓存帧),停止时回填文件头
- bool m_recording = false;
- QFile *m_recordFile = nullptr;
- QString m_recordPath;
- int m_recordWidth = 0;
- int m_recordHeight = 0;
- int m_recordStride = 0;
- int m_recordFrameCount = 0;
- QElapsedTimer m_recordElapsed;
- //保护录制状态与文件写入,startRecord/stopRecord(UI线程)与writeRecordFrame(取流线程)互斥
- mutable QMutex m_recordMutex;
- //图片归档路径
- QString picture_path_floader;
- QVector<cv::Mat> m_frame_buffer;
- int testJNI();
- int checkPermission(void)
- {
- int ret = 0;
- const static char* MY_JAVA_CLASS = "GetUsbPermission";
- ret = QJniObject::callStaticMethod<jint>(MY_JAVA_CLASS,"checkPermission","()I");
- return ret;
- }
- static inline QImage cvMatToQImage( const cv::Mat &inMat )
- {
- switch ( inMat.type() )
- {
- // 8-bit, 4 channel
- case CV_8UC4:
- {
- QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB32 );
- return image;
- }
- // 8-bit, 3 channel
- case CV_8UC3:
- {
- QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB888 );
- return image.rgbSwapped();
- }
- // 8-bit, 1 channel
- case CV_8UC1:
- {
- static QVector<QRgb> sColorTable;
- // only create our color table once
- if ( sColorTable.isEmpty() )
- {
- for ( int i = 0; i < 256; ++i )
- sColorTable.push_back( qRgb( i, i, i ) );
- }
- QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_Indexed8 );
- image.setColorTable( sColorTable );
- return image;
- }
- default:
- qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
- break;
- }
- return QImage();
- }
- static cv::Mat QImageToCvMat(QImage inImage, bool inCloneImageData = true)
- {
- switch (inImage.format())
- {
- // 8-bit, 4 channel
- case QImage::Format_ARGB32:
- case QImage::Format_ARGB32_Premultiplied:
- {
- cv::Mat mat(inImage.height(), inImage.width(),
- CV_8UC4,
- const_cast<uchar*>(inImage.bits()),
- static_cast<size_t>(inImage.bytesPerLine())
- );
- return inCloneImageData ? mat.clone() : mat;
- }
- // 8-bit, 3 channel
- case QImage::Format_RGB32:
- {
- if (!inCloneImageData) {
- qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
- }
- cv::Mat mat(inImage.height(), inImage.width(),
- CV_8UC4,
- const_cast<uchar*>(inImage.bits()),
- static_cast<size_t>(inImage.bytesPerLine())
- );
- cv::Mat matNoAlpha;
- cv::cvtColor(mat, matNoAlpha, cv::COLOR_BGRA2BGR); // drop the all-white alpha channel
- return matNoAlpha;
- }
- // 8-bit, 3 channel
- case QImage::Format_RGB888:
- {
- if (!inCloneImageData) {
- qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
- }
- QImage swapped = inImage.rgbSwapped();
- cv::Mat mat(swapped.height(), swapped.width(),
- CV_8UC3,
- const_cast<uchar*>(swapped.bits()),
- static_cast<size_t>(swapped.bytesPerLine())
- );
- return inCloneImageData ? mat.clone() : mat;
- }
- // 8-bit, 1 channel
- case QImage::Format_Grayscale8:
- case QImage::Format_Indexed8:
- {
- cv::Mat mat(inImage.height(), inImage.width(),
- CV_8UC1,
- const_cast<uchar*>(inImage.bits()),
- static_cast<size_t>(inImage.bytesPerLine())
- );
- return inCloneImageData ? mat.clone() : mat;
- }
- #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
- case QImage::Format_Grayscale16:
- {
- cv::Mat mat(cv::Size(inImage.width(),inImage.height()),CV_16U,const_cast<uchar*>(inImage.bits()),cv::Mat::AUTO_STEP);
- return inCloneImageData ? mat.clone() : mat;
- }
- #endif
- default:
- qWarning() << "QImageToCvMat() - QImage format not handled in switch:" << inImage.format();
- break;
- }
- return cv::Mat();
- }
- bool IsColor(MvGvspPixelType enType)
- {
- switch(enType)
- {
- case PixelType_Gvsp_BGR8_Packed:
- case PixelType_Gvsp_YUV422_Packed:
- case PixelType_Gvsp_YUV422_YUYV_Packed:
- case PixelType_Gvsp_BayerGR8:
- case PixelType_Gvsp_BayerRG8:
- case PixelType_Gvsp_BayerGB8:
- case PixelType_Gvsp_BayerBG8:
- case PixelType_Gvsp_BayerGB10:
- case PixelType_Gvsp_BayerGB10_Packed:
- case PixelType_Gvsp_BayerBG10:
- case PixelType_Gvsp_BayerBG10_Packed:
- case PixelType_Gvsp_BayerRG10:
- case PixelType_Gvsp_BayerRG10_Packed:
- case PixelType_Gvsp_BayerGR10:
- case PixelType_Gvsp_BayerGR10_Packed:
- case PixelType_Gvsp_BayerGB12:
- case PixelType_Gvsp_BayerGB12_Packed:
- case PixelType_Gvsp_BayerBG12:
- case PixelType_Gvsp_BayerBG12_Packed:
- case PixelType_Gvsp_BayerRG12:
- case PixelType_Gvsp_BayerRG12_Packed:
- case PixelType_Gvsp_BayerGR12:
- case PixelType_Gvsp_BayerGR12_Packed:
- return true;
- default:
- return false;
- }
- }
- bool IsMono(MvGvspPixelType enType)
- {
- switch(enType)
- {
- case PixelType_Gvsp_Mono10:
- case PixelType_Gvsp_Mono10_Packed:
- case PixelType_Gvsp_Mono12:
- case PixelType_Gvsp_Mono12_Packed:
- return true;
- default:
- return false;
- }
- }
- //地址的转换函数
- bool ConvertToHexIp(unsigned int *nHexIP, unsigned int *nDecIP)
- {
- if ( nDecIP[0] < 0 || nDecIP[0] > 255
- || nDecIP[1] < 0 || nDecIP[1] > 255
- || nDecIP[2] < 0 || nDecIP[2] > 255
- || nDecIP[3] < 0 || nDecIP[3] > 255)
- {
- return false;
- }
- *nHexIP = (nDecIP[0] << 24) + (nDecIP[1] << 16) + (nDecIP[2] << 8) + nDecIP[3];
- return true;
- }
- }; // class Frames
- //! 原始视频流(.rawv)播放器:逐帧读取未压缩的原始帧并推送到 QVideoSink 播放
- class RawVideoPlayer : public QObject
- {
- Q_OBJECT
- QML_ELEMENT
- Q_PROPERTY( QVideoSink* videoSink READ videoSink WRITE setVideoSink NOTIFY videoSinkChanged )
- Q_PROPERTY( bool playing READ playing NOTIFY playingChanged )
- Q_PROPERTY( bool loaded READ loaded NOTIFY loadedChanged )
- Q_PROPERTY( int position READ position NOTIFY positionChanged )
- Q_PROPERTY( int frameCount READ frameCount NOTIFY frameCountChanged )
- Q_PROPERTY( QString source READ source NOTIFY sourceChanged )
- Q_PROPERTY( bool busy READ busy NOTIFY busyChanged )
- public:
- explicit RawVideoPlayer(QObject *parent = nullptr);
- QVideoSink* videoSink() const { return m_videoSink; }
- void setVideoSink(QVideoSink *newVideoSink);
- bool playing() const { return m_playing; }
- bool loaded() const { return m_loaded; }
- int position() const { return m_position; }
- int frameCount() const { return m_frameCount; }
- QString source() const { return m_source; }
- bool busy() const { return m_busy; }
- //打开原始视频流文件,成功后可调用 play()
- Q_INVOKABLE bool open(const QString &path);
- Q_INVOKABLE void play();
- //分析:识别视频帧中最清晰的一帧,锐化+对比度增强后保存为图片(与视频同目录)
- Q_INVOKABLE void analyzeSharpestFrame();
- //融合:对视频帧做景深融合,锐化+对比度增强后保存为图片(与视频同目录)
- Q_INVOKABLE void fuseVideoFrames();
- Q_INVOKABLE void pause();
- Q_INVOKABLE void stop();
- //定位到指定帧(立即刷新画面)
- Q_INVOKABLE void seek(int frameIndex);
- Q_INVOKABLE void close();
- //判断文件是否为原始视频流(.rawv)
- Q_INVOKABLE bool isRawVideo(const QString &path) const;
- signals:
- void videoSinkChanged();
- void playingChanged();
- void loadedChanged();
- void positionChanged();
- void frameCountChanged();
- void sourceChanged();
- void busyChanged();
- //分析结束:path为空表示失败,成功时图片已保存到path
- void analyzeFinished(const QString &path);
- //融合结束:path为空表示失败,成功时图片已保存到path
- void fuseFinished(const QString &path);
- private slots:
- void onTick();
- private:
- //读取并推送当前帧,失败返回false
- bool pushFrame();
- void setBusy(bool busy);
- //结果图片路径,命名与拍照图片一致:<视频时间戳>z0x0[_sharp]_<序号>.jpg
- QString resultPath(int serial, bool sharp) const;
- QFile m_file;
- QTimer m_timer;
- QVideoSink *m_videoSink = nullptr;
- QString m_source;
- int m_width = 0;
- int m_height = 0;
- int m_stride = 0;
- int m_fps = 0;
- int m_frameCount = 0;
- int m_position = 0;
- bool m_loaded = false;
- bool m_playing = false;
- //分析/融合任务进行中(进行中时不重复触发)
- bool m_busy = false;
- //同一视频内结果序号:分析结果与融合结果成对(_sharp_后缀)
- int m_resultSerial = 0;
- };
- } /* namespace Qt6QCamera */
- Q_DECLARE_METATYPE(cv::Mat)
- #endif // M_FRAMES_HPP
|