cameramanager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #ifndef M_FRAMES_HPP
  2. #define M_FRAMES_HPP
  3. // Qt include.
  4. #include <QObject>
  5. #include <QVector>
  6. #include <QMetaType>
  7. #include <QVideoSink>
  8. #include <QCamera>
  9. #include <QMediaRecorder>
  10. #include <QMediaCaptureSession>
  11. #include <QImageCapture>
  12. #include <QPointer>
  13. #include <QThread>
  14. #include <QThreadPool>
  15. #include <m_opencv.h>
  16. #include <m_workthread.h>
  17. #include <mqtthandler.h>
  18. #include <mserial.h>
  19. #include <ymodem.h>
  20. #include <m_json.h>
  21. #include <m_loger.h>
  22. #include <QFuture>
  23. #include <QSemaphore>
  24. #include <QQuickImageProvider>
  25. #include <QQmlImageProviderBase>
  26. #include <QImage>
  27. #include <pthread.h>
  28. //海康
  29. // #pragma pack(push, 4)
  30. // #include "MvCameraControl.h"
  31. // #pragma pack(pop)
  32. #include <MvCameraControl.h>
  33. #include <PixelType.h>
  34. //https://github.com/saki4510t/UVCCamera/pulls
  35. //https://gitee.com/dengly/UVCCamera
  36. //USBCamera: https://blog.csdn.net/hanshiying007/article/details/124118486
  37. #define CameraType 2 //0-QCamera , 1-OPENCV, 2-libuvc
  38. #define movZ_f "f80" //多图移动,正向80
  39. #define movZ_b "b80" //多图移动,反向80
  40. #define movX_f "f80" //多图移动,正向
  41. #define movX_b "b80" //多图移动,反向
  42. #define focux_f "f4" //单步对焦,正向
  43. #define focus_b "b4" //单步对焦,反向
  44. #define m_foucus_step_max 5
  45. #define focus_step_max 30
  46. //性能提升
  47. //https://blog.csdn.net/u012915226/article/details/115733359
  48. //https://blog.csdn.net/u012915226/article/details/121745861?spm=1001.2014.3001.5501
  49. //https://blog.csdn.net/aggs1990/article/details/125694175
  50. //自定义QVideoSink
  51. //https://blog.csdn.net/aggs1990/article/details/125361203?spm=1001.2014.3001.5501
  52. //github: 模块
  53. //https://github.com/erikmwerner/QUVCView/blob/master/uvccapture.cpp
  54. namespace Qt6QCamera {
  55. // 定义一个包含信号的发送类
  56. class SenderCustom : public QObject {
  57. Q_OBJECT
  58. public:
  59. // 定义一个信号,无参数
  60. signals:
  61. void signalToSend(const cv::Mat& frame);
  62. void signalQStr(const QString &qst);
  63. public:
  64. void sendSignal(const cv::Mat& frame) {
  65. emit signalToSend(frame); // 发送信号
  66. }
  67. void sendQStr(const QString &qst){
  68. signalQStr(qst);
  69. }
  70. };
  71. class FPSTimer : public QObject
  72. {
  73. Q_OBJECT
  74. public:
  75. explicit FPSTimer(QObject *parent = nullptr);
  76. public slots:
  77. void incrementFrame() { m_frame_count++; }
  78. void setInterval(int interval);
  79. void setRunning(bool running);
  80. bool isRunning();
  81. signals:
  82. void fpsUpdated(double);
  83. private slots:
  84. void onTimerTimeout();
  85. private:
  86. QTimer* m_timer = nullptr;
  87. QElapsedTimer* m_elapsed_timer = nullptr;
  88. int m_interval = 1000;
  89. int m_frame_count = 0;
  90. };
  91. //
  92. // Frames
  93. //
  94. struct RawFrame
  95. {
  96. int width;
  97. int height;
  98. MvGvspPixelType pixelType;
  99. QByteArray data; // 原始 buffer 拷贝
  100. };
  101. //! Frames listener.
  102. class Frames :public QObject//, public QQuickImageProvider //QObject //
  103. {
  104. Q_OBJECT
  105. QML_ELEMENT
  106. Q_PROPERTY( QVideoSink* videoSink READ videoSink WRITE setVideoSink NOTIFY videoSinkChanged )
  107. Q_PROPERTY( QString formatString READ formatString NOTIFY formatStringChanged )
  108. Q_PROPERTY( QString fpsString READ fpsString NOTIFY fpsStringChanged )
  109. Q_PROPERTY( QString qulityString READ qulityString NOTIFY qulityStringChanged )
  110. Q_PROPERTY( bool auto_clicked READ auto_clicked WRITE setChange NOTIFY btnStatusChanged )
  111. Q_PROPERTY( bool camera_sw READ camera_sw WRITE setCameraSw NOTIFY cameraStaChanged )
  112. Q_PROPERTY(QString cam_ExposureTime READ cam_ExposureTime WRITE setExposureTime NOTIFY exposureTimeChanged)
  113. public:
  114. static void registerQmlType();
  115. explicit Frames(QObject * parent = nullptr);
  116. ~Frames() override;
  117. //! Set sink of video output.
  118. void setVideoSink( QVideoSink * newVideoSink );
  119. bool isDecoding() { return m_decoding; }
  120. //! \return Sink of video output.
  121. QVideoSink* videoSink() const { return m_videoSink; }
  122. //! set btn
  123. void setChange(bool stat_t);
  124. //! 相机状态
  125. void setCameraSw(bool stat_cam);
  126. void setExposureTime(const QString &exptime);
  127. //! \return Rotation angle.
  128. //! \return Format of video frame as string.
  129. QString formatString() const;
  130. //! \return FPS as string.
  131. QString fpsString() const;
  132. QString qulityString() const;
  133. QString cam_ExposureTime() const;
  134. bool auto_clicked() const;
  135. bool camera_sw() const;
  136. void saveImage();
  137. Q_INVOKABLE void captureImage();
  138. //inline ImageProvider *getImageProvider() { return m_pImgProvider; }
  139. //ImageProvider* imageProvider() const;
  140. static void ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
  141. bool createFolder(const QString &path) {
  142. QDir dir(path);
  143. if (dir.exists()) {
  144. LOG("文件夹已存在:{}",path.toStdString());
  145. return true;
  146. }
  147. if (dir.mkpath(".")) {
  148. LOG("文件夹创建成功:{}",path.toStdString());
  149. return true;
  150. } else {
  151. ERROR("文件夹创建失败:{}",path.toStdString());
  152. return false;
  153. }
  154. }
  155. signals:
  156. //! Video sink changed.
  157. void videoSinkChanged();
  158. void fusionVideoSinkChanged();
  159. void fusionPreviewVisibleChanged();
  160. void cameraOpenFailed(const QString &err);
  161. void formatStringChanged();
  162. void fpsStringChanged();
  163. void qulityStringChanged();
  164. void exposureTimeChanged();
  165. void frameAvailable(const QVideoFrame &frame);
  166. void frameReady(QImage image);
  167. void btnStatusChanged();
  168. void sendFocusFlag(QString flag_sta);
  169. void cameraStaChanged(bool sta_cam);
  170. void serial_data(const QByteArray &m_byte);
  171. void signal_stitch();
  172. //hikrobot
  173. void startGrabbing(bool sta_cam);
  174. void signalGrabing(unsigned char * nData);
  175. void callQmlRefeshImg();
  176. void signal_m1s();
  177. void dataUpload(const QByteArray &data); //到mqtt
  178. void signal_photoerr();
  179. void stopGrabbing(bool sta_cam);
  180. void signal_photo_saved();
  181. void signal_wait_upload(const QString &path);
  182. void signal_fusion_progress(const QString &message, int percent);
  183. void stepCountChanged(const QString step);
  184. public slots:
  185. void onMovDone(const QString stepcount);
  186. void finishDynamicFusion();
  187. // void parseFrame(const cv::Mat& frame);
  188. void upload_handle(void);
  189. void handle_reult_upload(bool success, const QString &error);
  190. void onFPSUpdated(double fps);
  191. private slots:
  192. //! Init camera.
  193. void initCam();
  194. void handleGrab(bool sta_cam);
  195. //! 1s timer.
  196. void timer();
  197. //
  198. void saveMatAsJpeg(const QVideoFrame &frame);
  199. //void sendData(QByteArray m_byte);
  200. void displayFrame(const QVideoFrame &frame);
  201. void processFrameAsync(const RawFrame& frame);
  202. private:
  203. // 图片质量检测辅助函数
  204. // 返回: 0=正常, 1=黑图, 2=白图, 4=过曝, 5=欠曝, 6=低纹理, 7=模糊/低清晰度, 8=镜头遮挡
  205. int detectImageQuality(const cv::Mat& img) const;
  206. // 过滤图片列表:去除黑图、白图、无内容图和重复图
  207. // 返回过滤后的图片列表
  208. QStringList filterImages(const QStringList& paths,
  209. std::function<void(const QString&, int)> progressCallback) const;
  210. Q_DISABLE_COPY( Frames )
  211. //声明相机捕获对象
  212. //! Video sink of video output.
  213. //QPointer< QVideoSink > m_videoSink;
  214. QVideoSink *m_videoSink = nullptr;
  215. //! Format string.
  216. QString m_formatString;
  217. //! FPS string.
  218. QString m_fpsString;
  219. //! FPS.
  220. int m_fps;
  221. //! 清晰度
  222. QString m_qulityString;
  223. //! Timer.
  224. QTimer *m_1s;
  225. //! QVideoFrame
  226. OpencvFunc m_opencvfunc;
  227. JsonFunc m_json;
  228. QFuture<void> m_grabThread;
  229. QJniObject *jClass;
  230. bool camera_sta;
  231. bool m_decoding;
  232. bool m_streaming_active = false;
  233. QImage m_image;
  234. int fusion_num; //
  235. int m_width;
  236. int m_height;
  237. QString m_fExposureTime;
  238. //ImageProvider *m_pImgProvider;
  239. //libuvc
  240. mutable QMutex m_frameMutex; // 互斥锁,用于对帧队列的访问进行同步
  241. bool m_fusionFinishing = false;
  242. bool m_fusionActive = false;
  243. bool m_fusionFirstSaved = false; // 首次保存后触发融合
  244. FPSTimer* m_fps_timer = nullptr;
  245. cv::Mat m_frame;
  246. int MAX_STEP = 30;
  247. QVector<double> image_qulity;
  248. int image_qulity_num;
  249. int z_step = 0;
  250. int x_step = 0;
  251. //图片归档路径
  252. QString picture_path_floader;
  253. QVector<cv::Mat> m_frame_buffer;
  254. // 焦点清晰图路径(景深融合后清晰度对比用)
  255. QString m_sharpPath;
  256. // 参考清晰度(Laplacian方差),由 ptlist[0] 初始化,后续图片以此为基准判断模糊程度
  257. double m_refSharpness = 0.0;
  258. int testJNI();
  259. static inline QImage cvMatToQImage( const cv::Mat &inMat )
  260. {
  261. switch ( inMat.type() )
  262. {
  263. // 8-bit, 4 channel
  264. case CV_8UC4:
  265. {
  266. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB32 );
  267. return image;
  268. }
  269. // 8-bit, 3 channel
  270. case CV_8UC3:
  271. {
  272. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB888 );
  273. return image.rgbSwapped();
  274. }
  275. // 8-bit, 1 channel
  276. case CV_8UC1:
  277. {
  278. static QVector<QRgb> sColorTable;
  279. // only create our color table once
  280. if ( sColorTable.isEmpty() )
  281. {
  282. for ( int i = 0; i < 256; ++i )
  283. sColorTable.push_back( qRgb( i, i, i ) );
  284. }
  285. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_Indexed8 );
  286. image.setColorTable( sColorTable );
  287. return image;
  288. }
  289. default:
  290. qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
  291. break;
  292. }
  293. return QImage();
  294. }
  295. static cv::Mat QImageToCvMat(QImage inImage, bool inCloneImageData = true)
  296. {
  297. switch (inImage.format())
  298. {
  299. // 8-bit, 4 channel
  300. case QImage::Format_ARGB32:
  301. case QImage::Format_ARGB32_Premultiplied:
  302. {
  303. cv::Mat mat(inImage.height(), inImage.width(),
  304. CV_8UC4,
  305. const_cast<uchar*>(inImage.bits()),
  306. static_cast<size_t>(inImage.bytesPerLine())
  307. );
  308. return inCloneImageData ? mat.clone() : mat;
  309. }
  310. // 8-bit, 3 channel
  311. case QImage::Format_RGB32:
  312. {
  313. if (!inCloneImageData) {
  314. qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
  315. }
  316. cv::Mat mat(inImage.height(), inImage.width(),
  317. CV_8UC4,
  318. const_cast<uchar*>(inImage.bits()),
  319. static_cast<size_t>(inImage.bytesPerLine())
  320. );
  321. cv::Mat matNoAlpha;
  322. cv::cvtColor(mat, matNoAlpha, cv::COLOR_BGRA2BGR); // drop the all-white alpha channel
  323. return matNoAlpha;
  324. }
  325. // 8-bit, 3 channel
  326. case QImage::Format_RGB888:
  327. {
  328. if (!inCloneImageData) {
  329. qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
  330. }
  331. QImage swapped = inImage.rgbSwapped();
  332. cv::Mat mat(swapped.height(), swapped.width(),
  333. CV_8UC3,
  334. const_cast<uchar*>(swapped.bits()),
  335. static_cast<size_t>(swapped.bytesPerLine())
  336. );
  337. return inCloneImageData ? mat.clone() : mat;
  338. }
  339. // 8-bit, 1 channel
  340. case QImage::Format_Grayscale8:
  341. case QImage::Format_Indexed8:
  342. {
  343. cv::Mat mat(inImage.height(), inImage.width(),
  344. CV_8UC1,
  345. const_cast<uchar*>(inImage.bits()),
  346. static_cast<size_t>(inImage.bytesPerLine())
  347. );
  348. return inCloneImageData ? mat.clone() : mat;
  349. }
  350. #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
  351. case QImage::Format_Grayscale16:
  352. {
  353. cv::Mat mat(cv::Size(inImage.width(),inImage.height()),CV_16U,const_cast<uchar*>(inImage.bits()),cv::Mat::AUTO_STEP);
  354. return inCloneImageData ? mat.clone() : mat;
  355. }
  356. #endif
  357. default:
  358. qWarning() << "QImageToCvMat() - QImage format not handled in switch:" << inImage.format();
  359. break;
  360. }
  361. return cv::Mat();
  362. }
  363. bool IsColor(MvGvspPixelType enType)
  364. {
  365. switch(enType)
  366. {
  367. case PixelType_Gvsp_BGR8_Packed:
  368. case PixelType_Gvsp_YUV422_Packed:
  369. case PixelType_Gvsp_YUV422_YUYV_Packed:
  370. case PixelType_Gvsp_BayerGR8:
  371. case PixelType_Gvsp_BayerRG8:
  372. case PixelType_Gvsp_BayerGB8:
  373. case PixelType_Gvsp_BayerBG8:
  374. case PixelType_Gvsp_BayerGB10:
  375. case PixelType_Gvsp_BayerGB10_Packed:
  376. case PixelType_Gvsp_BayerBG10:
  377. case PixelType_Gvsp_BayerBG10_Packed:
  378. case PixelType_Gvsp_BayerRG10:
  379. case PixelType_Gvsp_BayerRG10_Packed:
  380. case PixelType_Gvsp_BayerGR10:
  381. case PixelType_Gvsp_BayerGR10_Packed:
  382. case PixelType_Gvsp_BayerGB12:
  383. case PixelType_Gvsp_BayerGB12_Packed:
  384. case PixelType_Gvsp_BayerBG12:
  385. case PixelType_Gvsp_BayerBG12_Packed:
  386. case PixelType_Gvsp_BayerRG12:
  387. case PixelType_Gvsp_BayerRG12_Packed:
  388. case PixelType_Gvsp_BayerGR12:
  389. case PixelType_Gvsp_BayerGR12_Packed:
  390. return true;
  391. default:
  392. return false;
  393. }
  394. }
  395. bool IsMono(MvGvspPixelType enType)
  396. {
  397. switch(enType)
  398. {
  399. case PixelType_Gvsp_Mono10:
  400. case PixelType_Gvsp_Mono10_Packed:
  401. case PixelType_Gvsp_Mono12:
  402. case PixelType_Gvsp_Mono12_Packed:
  403. return true;
  404. default:
  405. return false;
  406. }
  407. }
  408. //地址的转换函数
  409. bool ConvertToHexIp(unsigned int *nHexIP, unsigned int *nDecIP)
  410. {
  411. if ( nDecIP[0] < 0 || nDecIP[0] > 255
  412. || nDecIP[1] < 0 || nDecIP[1] > 255
  413. || nDecIP[2] < 0 || nDecIP[2] > 255
  414. || nDecIP[3] < 0 || nDecIP[3] > 255)
  415. {
  416. return false;
  417. }
  418. *nHexIP = (nDecIP[0] << 24) + (nDecIP[1] << 16) + (nDecIP[2] << 8) + nDecIP[3];
  419. return true;
  420. }
  421. }; // class Frames
  422. } /* namespace Qt6QCamera */
  423. Q_DECLARE_METATYPE(cv::Mat)
  424. #endif // M_FRAMES_HPP