cameramanager.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 <m_opencv.h>
  15. #include <m_workthread.h>
  16. #include <mqtthandler.h>
  17. #include <mserial.h>
  18. #include <ymodem.h>
  19. #include <m_json.h>
  20. #include <m_loger.h>
  21. #include <QFuture>
  22. #include <ImageStitcher.h>
  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. //抓图按钮:模拟MCU拍照指令(camera_tc=1),下一帧按序号保存/锐化/上传
  139. Q_INVOKABLE void manualCapture();
  140. //inline ImageProvider *getImageProvider() { return m_pImgProvider; }
  141. //ImageProvider* imageProvider() const;
  142. static void ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
  143. bool createFolder(const QString &path) {
  144. QDir dir(path);
  145. if (dir.exists()) {
  146. LOG("文件夹已存在:{}",path.toStdString());
  147. return true;
  148. }
  149. if (dir.mkpath(".")) {
  150. LOG("文件夹创建成功:{}",path.toStdString());
  151. return true;
  152. } else {
  153. ERROR("文件夹创建失败:{}",path.toStdString());
  154. return false;
  155. }
  156. }
  157. signals:
  158. //! Video sink changed.
  159. void videoSinkChanged();
  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_cache_image(const QString &path);
  183. void signal_sharp_processed(const QString &name_sharp); //锐化/对比度后台处理完成,回主线程统一入队入库
  184. void stepCountChanged(const QString step);
  185. public slots:
  186. void onMovDone(const QString stepcount){
  187. if(!camera_sw()){
  188. ERROR("Camera error!");
  189. setCameraSw(false);
  190. return;
  191. }
  192. cv::Mat snapshot;
  193. {
  194. // 锁内只做浅拷贝(引用计数保护数据),快速释放锁,避免阻塞取流线程写入 m_frame
  195. QMutexLocker locker(&m_frameMutex);
  196. if (m_frame.empty()) {
  197. // 取流尚未写入首帧时触发(如移动完成先于取流),不更新清晰度
  198. LOG("onMovDone: m_frame empty, skip");
  199. emit stepCountChanged(stepcount);
  200. return;
  201. }
  202. snapshot = m_frame;
  203. }
  204. // Tenengrad 重计算放到锁外执行(BGRA4通道,Tenengrad 内部已支持)
  205. m_qulityString = m_opencvfunc.Tenengrad(snapshot);
  206. LOG("onMovDone:{}",m_qulityString.toStdString());
  207. emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
  208. emit qulityStringChanged();
  209. emit stepCountChanged(stepcount);
  210. }
  211. // void parseFrame(const cv::Mat& frame);
  212. void upload_handle(void);
  213. void handle_reult_upload(bool success, const QString &error);
  214. void onFPSUpdated(double fps);
  215. private slots:
  216. //! Init camera.
  217. void initCam();
  218. void handleGrab(bool sta_cam);
  219. //! 1s timer.
  220. void timer();
  221. //!
  222. void update_qulstr(QString q_str);
  223. //
  224. void saveMatAsJpeg(const QVideoFrame &frame);
  225. void onSharpProcessed(const QString &name_sharp);
  226. //void sendData(QByteArray m_byte);
  227. void displayFrame(const QVideoFrame &frame);
  228. void mov_x_z_run();
  229. void processFrameAsync(const RawFrame& frame);
  230. private:
  231. Q_DISABLE_COPY( Frames )
  232. //声明相机捕获对象
  233. //! Video sink of video output.
  234. //QPointer< QVideoSink > m_videoSink;
  235. QVideoSink *m_videoSink;
  236. //! Format string.
  237. QString m_formatString;
  238. //! FPS string.
  239. QString m_fpsString;
  240. //! FPS.
  241. int m_fps;
  242. //! 清晰度
  243. QString m_qulityString;
  244. //! Timer.
  245. QTimer *m_1s;
  246. //! QVideoFrame
  247. OpencvFunc m_opencvfunc;
  248. JsonFunc m_json;
  249. QFuture<void> m_grabThread;
  250. //独立上传队列:与param::ptlist(拼接输入)隔离,每保存一张锐化/对比度图立即入队逐张上传
  251. QStringList m_uploadQueue;
  252. bool m_isUploading = false; //是否正在上传,防止并发启动多个上传线程
  253. QJniObject *jClass;
  254. bool camera_sta;
  255. bool m_decoding;
  256. bool m_streaming_active = false;
  257. QImage m_image;
  258. int m_width;
  259. int m_height;
  260. QString m_fExposureTime;
  261. //ImageProvider *m_pImgProvider;
  262. //libuvc
  263. mutable QMutex m_frameMutex; // 互斥锁,用于对帧队列的访问进行同步
  264. FPSTimer* m_fps_timer = nullptr;
  265. cv::Mat m_frame;
  266. QSemaphore m_frameGate; // 在途帧处理任务门禁(构造初值=3),防止任务积压导致内存暴涨
  267. QAtomicInt m_displayPending = 0; // 显示节流:UI线程未消费完上一帧时丢弃当前帧,只保留最新帧
  268. int image_num = 1;
  269. int z_step = 0;
  270. int x_step = 0;
  271. //保护 image_num/z_step/x_step 及"取号-建名-存图-自增-移轴"整段流程,
  272. //saveMatAsJpeg 由 QtConcurrent 线程池并发调用,无锁会导致重号覆盖、序号跳变、轴重复移动
  273. mutable QMutex m_saveMutex;
  274. //图片归档路径
  275. QString picture_path_floader;
  276. QVector<cv::Mat> m_frame_buffer;
  277. int testJNI();
  278. int checkPermission(void)
  279. {
  280. int ret = 0;
  281. const static char* MY_JAVA_CLASS = "GetUsbPermission";
  282. ret = QJniObject::callStaticMethod<jint>(MY_JAVA_CLASS,"checkPermission","()I");
  283. return ret;
  284. }
  285. static inline QImage cvMatToQImage( const cv::Mat &inMat )
  286. {
  287. switch ( inMat.type() )
  288. {
  289. // 8-bit, 4 channel
  290. case CV_8UC4:
  291. {
  292. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB32 );
  293. return image;
  294. }
  295. // 8-bit, 3 channel
  296. case CV_8UC3:
  297. {
  298. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB888 );
  299. return image.rgbSwapped();
  300. }
  301. // 8-bit, 1 channel
  302. case CV_8UC1:
  303. {
  304. static QVector<QRgb> sColorTable;
  305. // only create our color table once
  306. if ( sColorTable.isEmpty() )
  307. {
  308. for ( int i = 0; i < 256; ++i )
  309. sColorTable.push_back( qRgb( i, i, i ) );
  310. }
  311. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_Indexed8 );
  312. image.setColorTable( sColorTable );
  313. return image;
  314. }
  315. default:
  316. WARN("ASM::cvMatToQImage() - cv::Mat image type not handled in switch: {}", static_cast<int>(inMat.type()));
  317. break;
  318. }
  319. return QImage();
  320. }
  321. static cv::Mat QImageToCvMat(QImage inImage, bool inCloneImageData = true)
  322. {
  323. switch (inImage.format())
  324. {
  325. // 8-bit, 4 channel
  326. case QImage::Format_ARGB32:
  327. case QImage::Format_ARGB32_Premultiplied:
  328. {
  329. cv::Mat mat(inImage.height(), inImage.width(),
  330. CV_8UC4,
  331. const_cast<uchar*>(inImage.bits()),
  332. static_cast<size_t>(inImage.bytesPerLine())
  333. );
  334. return inCloneImageData ? mat.clone() : mat;
  335. }
  336. // 8-bit, 3 channel
  337. case QImage::Format_RGB32:
  338. {
  339. if (!inCloneImageData) {
  340. WARN("QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data");
  341. }
  342. cv::Mat mat(inImage.height(), inImage.width(),
  343. CV_8UC4,
  344. const_cast<uchar*>(inImage.bits()),
  345. static_cast<size_t>(inImage.bytesPerLine())
  346. );
  347. cv::Mat matNoAlpha;
  348. cv::cvtColor(mat, matNoAlpha, cv::COLOR_BGRA2BGR); // drop the all-white alpha channel
  349. return matNoAlpha;
  350. }
  351. // 8-bit, 3 channel
  352. case QImage::Format_RGB888:
  353. {
  354. if (!inCloneImageData) {
  355. WARN("QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data");
  356. }
  357. QImage swapped = inImage.rgbSwapped();
  358. cv::Mat mat(swapped.height(), swapped.width(),
  359. CV_8UC3,
  360. const_cast<uchar*>(swapped.bits()),
  361. static_cast<size_t>(swapped.bytesPerLine())
  362. );
  363. return inCloneImageData ? mat.clone() : mat;
  364. }
  365. // 8-bit, 1 channel
  366. case QImage::Format_Grayscale8:
  367. case QImage::Format_Indexed8:
  368. {
  369. cv::Mat mat(inImage.height(), inImage.width(),
  370. CV_8UC1,
  371. const_cast<uchar*>(inImage.bits()),
  372. static_cast<size_t>(inImage.bytesPerLine())
  373. );
  374. return inCloneImageData ? mat.clone() : mat;
  375. }
  376. #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
  377. case QImage::Format_Grayscale16:
  378. {
  379. cv::Mat mat(cv::Size(inImage.width(),inImage.height()),CV_16U,const_cast<uchar*>(inImage.bits()),cv::Mat::AUTO_STEP);
  380. return inCloneImageData ? mat.clone() : mat;
  381. }
  382. #endif
  383. default:
  384. WARN("QImageToCvMat() - QImage format not handled in switch: {}", static_cast<int>(inImage.format()));
  385. break;
  386. }
  387. return cv::Mat();
  388. }
  389. bool IsColor(MvGvspPixelType enType)
  390. {
  391. switch(enType)
  392. {
  393. case PixelType_Gvsp_BGR8_Packed:
  394. case PixelType_Gvsp_YUV422_Packed:
  395. case PixelType_Gvsp_YUV422_YUYV_Packed:
  396. case PixelType_Gvsp_BayerGR8:
  397. case PixelType_Gvsp_BayerRG8:
  398. case PixelType_Gvsp_BayerGB8:
  399. case PixelType_Gvsp_BayerBG8:
  400. case PixelType_Gvsp_BayerGB10:
  401. case PixelType_Gvsp_BayerGB10_Packed:
  402. case PixelType_Gvsp_BayerBG10:
  403. case PixelType_Gvsp_BayerBG10_Packed:
  404. case PixelType_Gvsp_BayerRG10:
  405. case PixelType_Gvsp_BayerRG10_Packed:
  406. case PixelType_Gvsp_BayerGR10:
  407. case PixelType_Gvsp_BayerGR10_Packed:
  408. case PixelType_Gvsp_BayerGB12:
  409. case PixelType_Gvsp_BayerGB12_Packed:
  410. case PixelType_Gvsp_BayerBG12:
  411. case PixelType_Gvsp_BayerBG12_Packed:
  412. case PixelType_Gvsp_BayerRG12:
  413. case PixelType_Gvsp_BayerRG12_Packed:
  414. case PixelType_Gvsp_BayerGR12:
  415. case PixelType_Gvsp_BayerGR12_Packed:
  416. return true;
  417. default:
  418. return false;
  419. }
  420. }
  421. bool IsMono(MvGvspPixelType enType)
  422. {
  423. switch(enType)
  424. {
  425. case PixelType_Gvsp_Mono10:
  426. case PixelType_Gvsp_Mono10_Packed:
  427. case PixelType_Gvsp_Mono12:
  428. case PixelType_Gvsp_Mono12_Packed:
  429. return true;
  430. default:
  431. return false;
  432. }
  433. }
  434. //地址的转换函数
  435. bool ConvertToHexIp(unsigned int *nHexIP, unsigned int *nDecIP)
  436. {
  437. if ( nDecIP[0] < 0 || nDecIP[0] > 255
  438. || nDecIP[1] < 0 || nDecIP[1] > 255
  439. || nDecIP[2] < 0 || nDecIP[2] > 255
  440. || nDecIP[3] < 0 || nDecIP[3] > 255)
  441. {
  442. return false;
  443. }
  444. *nHexIP = (nDecIP[0] << 24) + (nDecIP[1] << 16) + (nDecIP[2] << 8) + nDecIP[3];
  445. return true;
  446. }
  447. }; // class Frames
  448. } /* namespace Qt6QCamera */
  449. Q_DECLARE_METATYPE(cv::Mat)
  450. #endif // M_FRAMES_HPP