cameramanager.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <QSemaphore>
  23. #include <QQuickImageProvider>
  24. #include <QQmlImageProviderBase>
  25. #include <QImage>
  26. #include <pthread.h>
  27. //海康
  28. // #pragma pack(push, 4)
  29. // #include "MvCameraControl.h"
  30. // #pragma pack(pop)
  31. #include <MvCameraControl.h>
  32. #include <PixelType.h>
  33. //https://github.com/saki4510t/UVCCamera/pulls
  34. //https://gitee.com/dengly/UVCCamera
  35. //USBCamera: https://blog.csdn.net/hanshiying007/article/details/124118486
  36. #define CameraType 2 //0-QCamera , 1-OPENCV, 2-libuvc
  37. #define movZ_f "f80" //多图移动,正向80
  38. #define movZ_b "b80" //多图移动,反向80
  39. #define movX_f "f80" //多图移动,正向
  40. #define movX_b "b80" //多图移动,反向
  41. #define focux_f "f4" //单步对焦,正向
  42. #define focus_b "b4" //单步对焦,反向
  43. #define m_foucus_step_max 5
  44. #define focus_step_max 30
  45. //性能提升
  46. //https://blog.csdn.net/u012915226/article/details/115733359
  47. //https://blog.csdn.net/u012915226/article/details/121745861?spm=1001.2014.3001.5501
  48. //https://blog.csdn.net/aggs1990/article/details/125694175
  49. //自定义QVideoSink
  50. //https://blog.csdn.net/aggs1990/article/details/125361203?spm=1001.2014.3001.5501
  51. //github: 模块
  52. //https://github.com/erikmwerner/QUVCView/blob/master/uvccapture.cpp
  53. namespace Qt6QCamera {
  54. // 定义一个包含信号的发送类
  55. class SenderCustom : public QObject {
  56. Q_OBJECT
  57. public:
  58. // 定义一个信号,无参数
  59. signals:
  60. void signalToSend(const cv::Mat& frame);
  61. void signalQStr(const QString &qst);
  62. public:
  63. void sendSignal(const cv::Mat& frame) {
  64. emit signalToSend(frame); // 发送信号
  65. }
  66. void sendQStr(const QString &qst){
  67. signalQStr(qst);
  68. }
  69. };
  70. class FPSTimer : public QObject
  71. {
  72. Q_OBJECT
  73. public:
  74. explicit FPSTimer(QObject *parent = nullptr);
  75. public slots:
  76. void incrementFrame() { m_frame_count++; }
  77. void setInterval(int interval);
  78. void setRunning(bool running);
  79. bool isRunning();
  80. signals:
  81. void fpsUpdated(double);
  82. private slots:
  83. void onTimerTimeout();
  84. private:
  85. QTimer* m_timer = nullptr;
  86. QElapsedTimer* m_elapsed_timer = nullptr;
  87. int m_interval = 1000;
  88. int m_frame_count = 0;
  89. };
  90. //
  91. // Frames
  92. //
  93. //! Frames listener.
  94. class Frames :public QObject//, public QQuickImageProvider //QObject //
  95. {
  96. Q_OBJECT
  97. QML_ELEMENT
  98. Q_PROPERTY( QVideoSink* videoSink READ videoSink WRITE setVideoSink NOTIFY videoSinkChanged )
  99. Q_PROPERTY( qreal angle READ angle NOTIFY angleChanged )
  100. Q_PROPERTY( qreal xScale READ xScale NOTIFY xScaleChanged )
  101. Q_PROPERTY( qreal yScale READ yScale NOTIFY yScaleChanged )
  102. Q_PROPERTY( QString formatString READ formatString NOTIFY formatStringChanged )
  103. Q_PROPERTY( QString fpsString READ fpsString NOTIFY fpsStringChanged )
  104. Q_PROPERTY( QString qulityString READ qulityString NOTIFY qulityStringChanged )
  105. Q_PROPERTY( bool auto_clicked READ auto_clicked WRITE setChange NOTIFY btnStatusChanged )
  106. Q_PROPERTY( bool camera_sw READ camera_sw WRITE setCameraSw NOTIFY cameraStaChanged )
  107. public:
  108. static void registerQmlType();
  109. explicit Frames(QObject * parent = nullptr);
  110. ~Frames() override;
  111. //! Set sink of video output.
  112. void setVideoSink( QVideoSink * newVideoSink );
  113. bool isDecoding() { return m_decoding; }
  114. //! \return Sink of video output.
  115. QVideoSink* videoSink() const { return m_videoSink; }
  116. //! set btn
  117. void setChange(bool stat_t);
  118. //! 相机状态
  119. void setCameraSw(bool stat_cam);
  120. //! \return Rotation angle.
  121. qreal angle() const;
  122. //! \return X scale factor.
  123. qreal xScale() const;
  124. //! \return Y scale factor
  125. qreal yScale() const;
  126. //! \return Format of video frame as string.
  127. QString formatString() const;
  128. //! \return FPS as string.
  129. QString fpsString() const;
  130. QString qulityString() const;
  131. bool auto_clicked() const;
  132. bool camera_sw() const;
  133. void saveImage();
  134. Q_INVOKABLE void captureImage();
  135. //inline ImageProvider *getImageProvider() { return m_pImgProvider; }
  136. //ImageProvider* imageProvider() const;
  137. static void ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser);
  138. bool createFolder(const QString &path) {
  139. QDir dir(path);
  140. if (dir.exists()) {
  141. LOG("文件夹已存在:{}",path.toStdString());
  142. return true;
  143. }
  144. if (dir.mkpath(".")) {
  145. LOG("文件夹创建成功:{}",path.toStdString());
  146. return true;
  147. } else {
  148. ERROR("文件夹创建失败:{}",path.toStdString());
  149. return false;
  150. }
  151. }
  152. signals:
  153. //! Video sink changed.
  154. void videoSinkChanged();
  155. void cameraOpenFailed(const QString &err);
  156. void angleChanged();
  157. void xScaleChanged();
  158. void yScaleChanged();
  159. void formatStringChanged();
  160. void fpsStringChanged();
  161. void qulityStringChanged();
  162. void frameAvailable(const QVideoFrame &frame);
  163. void frameReady(QImage image);
  164. void btnStatusChanged();
  165. void sendFocusFlag(QString flag_sta);
  166. void cameraStaChanged(bool sta_cam);
  167. void serial_data(const QByteArray &m_byte);
  168. void signal_stitch();
  169. //hikrobot
  170. void startGrabbing(bool sta_cam);
  171. void signalGrabing(unsigned char * nData);
  172. void callQmlRefeshImg();
  173. void signal_m1s();
  174. void dataUpload(const QByteArray &data); //到mqtt
  175. void signal_photoerr();
  176. void stopGrabbing(bool sta_cam);
  177. void signal_photo_saved();
  178. void signal_wait_upload(const QString &path);
  179. void stepCountChanged(const QString step);
  180. public slots:
  181. void onMovDone(const QString stepcount){
  182. if(!camera_sw()){
  183. ERROR("Camera error!");
  184. setCameraSw(false);
  185. return;
  186. }
  187. m_qulityString = m_opencvfunc.Tenengrad(m_frame);
  188. LOG("onMovDone:{}",m_qulityString.toStdString());
  189. emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
  190. emit qulityStringChanged();
  191. emit stepCountChanged(stepcount);
  192. }
  193. // void parseFrame(const cv::Mat& frame);
  194. void upload_handle(void);
  195. void handle_reult_upload(bool success, const QString &error);
  196. void onFPSUpdated(double fps);
  197. private slots:
  198. //! Init camera.
  199. void initCam();
  200. //! Video frame changed.
  201. void newFrame( const QVideoFrame & frame );
  202. //void parseFrame();
  203. void handleGrab(bool sta_cam);
  204. void processLoop(bool sta);
  205. //! 1s timer.
  206. void timer();
  207. //!
  208. void update_qulstr(QString q_str);
  209. //
  210. void readyForCapture(bool ready);
  211. void processCapturedImage(int requestId, const QImage &img);
  212. void imageSaved(int id, const QString &fileName);
  213. void displayCaptureError(int id, const QImageCapture::Error error, const QString &errorString);
  214. void displayCameraError();
  215. //void sendData(QByteArray m_byte);
  216. void displayFrame(const QVideoFrame &frame);
  217. void mov_x_z_run();
  218. private:
  219. Q_DISABLE_COPY( Frames )
  220. //! Camera.
  221. QCamera * m_cam;
  222. //声明相机捕获对象
  223. //! Capture.
  224. QMediaCaptureSession m_capture;
  225. QImageCapture *m_imageCapture;
  226. //! Video sink of video output.
  227. //QPointer< QVideoSink > m_videoSink;
  228. QVideoSink *m_videoSink;
  229. //! Format string.
  230. QString m_formatString;
  231. //! FPS string.
  232. QString m_fpsString;
  233. //! FPS.
  234. int m_fps;
  235. //! 清晰度
  236. QString m_qulityString;
  237. //! Timer.
  238. QTimer *m_1s;
  239. //! QVideoFrame
  240. OpencvFunc m_opencvfunc;
  241. JsonFunc m_json;
  242. QFuture<void> m_processThread;
  243. QFuture<void> m_grabThread;
  244. QJniObject *jClass;
  245. bool camera_sta;
  246. bool m_decoding;
  247. bool m_streaming_active = false;
  248. QSharedMemory sharedMemory;
  249. QImage m_image;
  250. int stitch_des;
  251. //ImageProvider *m_pImgProvider;
  252. //libuvc
  253. mutable QMutex frameMutex; // 互斥锁,用于对帧队列的访问进行同步
  254. FPSTimer* m_fps_timer = nullptr;
  255. cv::Mat m_frame;
  256. int MAX_STEP = 30;
  257. QVector<double> image_qulity;
  258. int image_qulity_num;
  259. int z_step = 0;
  260. int x_step = 0;
  261. QMutex m_frameMutex;
  262. std::unique_ptr<QVideoFrame> m_latestFrame;
  263. //图片归档路径
  264. QString picture_path_floader;
  265. QVector<cv::Mat> m_frame_buffer;
  266. int testJNI();
  267. int checkPermission(void)
  268. {
  269. int ret = 0;
  270. const static char* MY_JAVA_CLASS = "GetUsbPermission";
  271. ret = QJniObject::callStaticMethod<jint>(MY_JAVA_CLASS,"checkPermission","()I");
  272. return ret;
  273. }
  274. int requestPermission(int vid, int pid);
  275. static inline QImage cvMatToQImage( const cv::Mat &inMat )
  276. {
  277. switch ( inMat.type() )
  278. {
  279. // 8-bit, 4 channel
  280. case CV_8UC4:
  281. {
  282. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB32 );
  283. return image;
  284. }
  285. // 8-bit, 3 channel
  286. case CV_8UC3:
  287. {
  288. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB888 );
  289. return image.rgbSwapped();
  290. }
  291. // 8-bit, 1 channel
  292. case CV_8UC1:
  293. {
  294. static QVector<QRgb> sColorTable;
  295. // only create our color table once
  296. if ( sColorTable.isEmpty() )
  297. {
  298. for ( int i = 0; i < 256; ++i )
  299. sColorTable.push_back( qRgb( i, i, i ) );
  300. }
  301. QImage image( inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_Indexed8 );
  302. image.setColorTable( sColorTable );
  303. return image;
  304. }
  305. default:
  306. qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
  307. break;
  308. }
  309. return QImage();
  310. }
  311. static cv::Mat QImageToCvMat(QImage inImage, bool inCloneImageData = true)
  312. {
  313. switch (inImage.format())
  314. {
  315. // 8-bit, 4 channel
  316. case QImage::Format_ARGB32:
  317. case QImage::Format_ARGB32_Premultiplied:
  318. {
  319. cv::Mat mat(inImage.height(), inImage.width(),
  320. CV_8UC4,
  321. const_cast<uchar*>(inImage.bits()),
  322. static_cast<size_t>(inImage.bytesPerLine())
  323. );
  324. return inCloneImageData ? mat.clone() : mat;
  325. }
  326. // 8-bit, 3 channel
  327. case QImage::Format_RGB32:
  328. {
  329. if (!inCloneImageData) {
  330. qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
  331. }
  332. cv::Mat mat(inImage.height(), inImage.width(),
  333. CV_8UC4,
  334. const_cast<uchar*>(inImage.bits()),
  335. static_cast<size_t>(inImage.bytesPerLine())
  336. );
  337. cv::Mat matNoAlpha;
  338. cv::cvtColor(mat, matNoAlpha, cv::COLOR_BGRA2BGR); // drop the all-white alpha channel
  339. return matNoAlpha;
  340. }
  341. // 8-bit, 3 channel
  342. case QImage::Format_RGB888:
  343. {
  344. if (!inCloneImageData) {
  345. qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
  346. }
  347. QImage swapped = inImage.rgbSwapped();
  348. cv::Mat mat(swapped.height(), swapped.width(),
  349. CV_8UC3,
  350. const_cast<uchar*>(swapped.bits()),
  351. static_cast<size_t>(swapped.bytesPerLine())
  352. );
  353. return inCloneImageData ? mat.clone() : mat;
  354. }
  355. // 8-bit, 1 channel
  356. case QImage::Format_Grayscale8:
  357. case QImage::Format_Indexed8:
  358. {
  359. cv::Mat mat(inImage.height(), inImage.width(),
  360. CV_8UC1,
  361. const_cast<uchar*>(inImage.bits()),
  362. static_cast<size_t>(inImage.bytesPerLine())
  363. );
  364. return inCloneImageData ? mat.clone() : mat;
  365. }
  366. #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
  367. case QImage::Format_Grayscale16:
  368. {
  369. cv::Mat mat(cv::Size(inImage.width(),inImage.height()),CV_16U,const_cast<uchar*>(inImage.bits()),cv::Mat::AUTO_STEP);
  370. return inCloneImageData ? mat.clone() : mat;
  371. }
  372. #endif
  373. default:
  374. qWarning() << "QImageToCvMat() - QImage format not handled in switch:" << inImage.format();
  375. break;
  376. }
  377. return cv::Mat();
  378. }
  379. bool IsColor(MvGvspPixelType enType)
  380. {
  381. switch(enType)
  382. {
  383. case PixelType_Gvsp_BGR8_Packed:
  384. case PixelType_Gvsp_YUV422_Packed:
  385. case PixelType_Gvsp_YUV422_YUYV_Packed:
  386. case PixelType_Gvsp_BayerGR8:
  387. case PixelType_Gvsp_BayerRG8:
  388. case PixelType_Gvsp_BayerGB8:
  389. case PixelType_Gvsp_BayerBG8:
  390. case PixelType_Gvsp_BayerGB10:
  391. case PixelType_Gvsp_BayerGB10_Packed:
  392. case PixelType_Gvsp_BayerBG10:
  393. case PixelType_Gvsp_BayerBG10_Packed:
  394. case PixelType_Gvsp_BayerRG10:
  395. case PixelType_Gvsp_BayerRG10_Packed:
  396. case PixelType_Gvsp_BayerGR10:
  397. case PixelType_Gvsp_BayerGR10_Packed:
  398. case PixelType_Gvsp_BayerGB12:
  399. case PixelType_Gvsp_BayerGB12_Packed:
  400. case PixelType_Gvsp_BayerBG12:
  401. case PixelType_Gvsp_BayerBG12_Packed:
  402. case PixelType_Gvsp_BayerRG12:
  403. case PixelType_Gvsp_BayerRG12_Packed:
  404. case PixelType_Gvsp_BayerGR12:
  405. case PixelType_Gvsp_BayerGR12_Packed:
  406. return true;
  407. default:
  408. return false;
  409. }
  410. }
  411. bool IsMono(MvGvspPixelType enType)
  412. {
  413. switch(enType)
  414. {
  415. case PixelType_Gvsp_Mono10:
  416. case PixelType_Gvsp_Mono10_Packed:
  417. case PixelType_Gvsp_Mono12:
  418. case PixelType_Gvsp_Mono12_Packed:
  419. return true;
  420. default:
  421. return false;
  422. }
  423. }
  424. struct FramePacket {
  425. uint8_t* data;
  426. uint32_t width;
  427. uint32_t height;
  428. uint32_t pixelType;
  429. uint64_t timestamp;
  430. };
  431. static constexpr int QUEUE_SIZE = 6;
  432. FramePacket frameQueue[QUEUE_SIZE];
  433. std::atomic<int> writeIndex{0};
  434. std::atomic<int> readIndex{0};
  435. bool pushFrame(const FramePacket& pkt)
  436. {
  437. int next = (writeIndex + 1) % QUEUE_SIZE;
  438. if (next == readIndex.load(std::memory_order_acquire)) {
  439. return false; // 丢帧
  440. }
  441. frameQueue[writeIndex] = pkt;
  442. writeIndex.store(next, std::memory_order_release);
  443. return true;
  444. }
  445. void convertAndSend(const FramePacket& pkt);
  446. }; // class Frames
  447. } /* namespace Qt6QCamera */
  448. Q_DECLARE_METATYPE(cv::Mat)
  449. #endif // M_FRAMES_HPP