#ifndef M_FRAMES_HPP #define M_FRAMES_HPP // Qt include. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //海康 // #pragma pack(push, 4) // #include "MvCameraControl.h" // #pragma pack(pop) #include #include //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 // //! 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( qreal angle READ angle NOTIFY angleChanged ) Q_PROPERTY( qreal xScale READ xScale NOTIFY xScaleChanged ) Q_PROPERTY( qreal yScale READ yScale NOTIFY yScaleChanged ) 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 ) 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); //! \return Rotation angle. qreal angle() const; //! \return X scale factor. qreal xScale() const; //! \return Y scale factor qreal yScale() const; //! \return Format of video frame as string. QString formatString() const; //! \return FPS as string. QString fpsString() const; QString qulityString() const; bool auto_clicked() const; bool camera_sw() const; void saveImage(); Q_INVOKABLE void captureImage(); //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 angleChanged(); void xScaleChanged(); void yScaleChanged(); void formatStringChanged(); void fpsStringChanged(); void qulityStringChanged(); 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 stepCountChanged(const QString step); 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(); //! Video frame changed. void newFrame( const QVideoFrame & frame ); //void parseFrame(); void handleGrab(bool sta_cam); void processLoop(bool sta); //! 1s timer. void timer(); //! void update_qulstr(QString q_str); // void readyForCapture(bool ready); void processCapturedImage(int requestId, const QImage &img); void imageSaved(int id, const QString &fileName); void displayCaptureError(int id, const QImageCapture::Error error, const QString &errorString); void displayCameraError(); //void sendData(QByteArray m_byte); void displayFrame(const QVideoFrame &frame); void mov_x_z_run(); private: Q_DISABLE_COPY( Frames ) //! Camera. QCamera * m_cam; //声明相机捕获对象 //! Capture. QMediaCaptureSession m_capture; QImageCapture *m_imageCapture; //! 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 m_processThread; QFuture m_grabThread; QJniObject *jClass; bool camera_sta; bool m_decoding; bool m_streaming_active = false; QSharedMemory sharedMemory; QImage m_image; int stitch_des; //ImageProvider *m_pImgProvider; //libuvc mutable QMutex frameMutex; // 互斥锁,用于对帧队列的访问进行同步 FPSTimer* m_fps_timer = nullptr; cv::Mat m_frame; int MAX_STEP = 30; QVector image_qulity; int image_qulity_num; int z_step = 0; int x_step = 0; QMutex m_frameMutex; std::unique_ptr m_latestFrame; //图片归档路径 QString picture_path_floader; QVector m_frame_buffer; int testJNI(); int checkPermission(void) { int ret = 0; const static char* MY_JAVA_CLASS = "GetUsbPermission"; ret = QJniObject::callStaticMethod(MY_JAVA_CLASS,"checkPermission","()I"); return ret; } int requestPermission(int vid, int pid); 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 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(inImage.bits()), static_cast(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(inImage.bits()), static_cast(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(swapped.bits()), static_cast(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(inImage.bits()), static_cast(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(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; } } struct FramePacket { uint8_t* data; uint32_t width; uint32_t height; uint32_t pixelType; uint64_t timestamp; }; static constexpr int QUEUE_SIZE = 6; FramePacket frameQueue[QUEUE_SIZE]; std::atomic writeIndex{0}; std::atomic readIndex{0}; bool pushFrame(const FramePacket& pkt) { int next = (writeIndex + 1) % QUEUE_SIZE; if (next == readIndex.load(std::memory_order_acquire)) { return false; // 丢帧 } frameQueue[writeIndex] = pkt; writeIndex.store(next, std::memory_order_release); return true; } void convertAndSend(const FramePacket& pkt); }; // class Frames } /* namespace Qt6QCamera */ Q_DECLARE_METATYPE(cv::Mat) #endif // M_FRAMES_HPP