cameramanager.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*!
  2. \author Igor Mironchik (igor dot mironchik at gmail dot com).
  3. MIT License
  4. Copyright (c) 2022 Igor Mironchik
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. #include "cameramanager.h"
  22. // Qt include.
  23. #include <QQmlEngine>
  24. #include <QCameraDevice>
  25. #include <QThreadPool>
  26. #include <QTimer>
  27. #include <QDateTime>
  28. #include <QDebug>
  29. #include <QtConcurrent/QtConcurrent>
  30. #include <m_workthread.h>
  31. #include <mserial.h>
  32. #include <curl/curl.h>
  33. #include <main.h>
  34. #include <m_loger.h>
  35. #include <QMessageBox>
  36. class param;
  37. namespace Qt6QCamera {
  38. // 在途帧处理任务门禁守卫:任务结束/提前返回时自动释放信号量计数
  39. class FrameGateGuard {
  40. public:
  41. explicit FrameGateGuard(QSemaphore* s) : m_s(s) {}
  42. ~FrameGateGuard() { if (m_s) m_s->release(1); }
  43. private:
  44. QSemaphore* m_s;
  45. };
  46. //< hikrobot
  47. void *handle = NULL;
  48. unsigned int nRet = MV_OK;
  49. MVCC_ENUMVALUE current_pixel;
  50. SenderCustom sender_custom;
  51. cv::Mat cv_frame;
  52. FPSTimer::FPSTimer(QObject *parent) : QObject(parent)
  53. {
  54. m_timer = new QTimer(this);
  55. m_elapsed_timer = new QElapsedTimer();
  56. m_timer->setInterval(m_interval);
  57. connect(m_timer, &QTimer::timeout,
  58. this, &FPSTimer::onTimerTimeout);
  59. }
  60. void FPSTimer::onTimerTimeout()
  61. {
  62. double seconds_elapsed = static_cast<double>(m_elapsed_timer->nsecsElapsed()) / 1000000000.0;
  63. m_elapsed_timer->start();
  64. double fps = static_cast<double>(m_frame_count) /seconds_elapsed;
  65. emit fpsUpdated(fps);
  66. m_frame_count = 0;
  67. }
  68. void FPSTimer::setInterval(int interval)
  69. {
  70. m_interval = interval;
  71. m_timer->setInterval(interval);
  72. }
  73. void FPSTimer::setRunning(bool running)
  74. {
  75. if(running) {
  76. m_timer->start(m_interval);
  77. m_elapsed_timer->start();
  78. }
  79. else {
  80. m_timer->stop();
  81. m_elapsed_timer->invalidate();
  82. emit fpsUpdated(0.0);
  83. }
  84. }
  85. bool FPSTimer::isRunning()
  86. {
  87. return m_timer->isActive();
  88. }
  89. //
  90. // Frames
  91. //
  92. Frames::Frames(QObject * parent)
  93. : //QVideoSink(parent),
  94. QObject(parent),
  95. m_frameGate(3),
  96. m_decoding(false),
  97. m_fps( 0 ),
  98. m_1s( new QTimer( this ) )
  99. {
  100. // 全局限制OpenCV内部线程数为1:取流采用QtConcurrent任务级并行(3~4任务),
  101. // OpenCV再开多线程会叠加成多线程抢4核,demosaic耗时恶化并诱发内存问题。
  102. // 拍照/融合路径(m_opencv capOpenCVThreads)会临时改回2,取流任务内会再次钳制。
  103. cv::setNumThreads(1);
  104. QMetaType id = QMetaType::fromName("cv::Mat");
  105. if(id.flags() == QMetaType::UnknownType) {
  106. qRegisterMetaType<cv::Mat>("cv::Mat");
  107. }
  108. image_num = 1;
  109. m_width = 0;
  110. m_height = 0;
  111. connect( m_1s, &QTimer::timeout, this, &Frames::timer);
  112. m_1s->setInterval( 500 ); //1000
  113. m_1s->stop();
  114. m_fps_timer = new FPSTimer(this);
  115. connect(m_fps_timer, &FPSTimer::fpsUpdated,
  116. this, &Frames::onFPSUpdated);
  117. connect(this,&Frames::startGrabbing,
  118. this,&Frames::handleGrab);
  119. // connect(this,&Frames::uvcframeAvailable,
  120. // this,&Frames::parseFrame);
  121. connect(this,&Frames::frameAvailable,
  122. this,&Frames::displayFrame);
  123. connect(this,&Frames::stopGrabbing,
  124. this,&Frames::setCameraSw);
  125. connect(this,&Frames::signal_photo_saved,
  126. this,&Frames::upload_handle);
  127. connect(this,&Frames::signal_sharp_processed,
  128. this,&Frames::onSharpProcessed,Qt::QueuedConnection);
  129. initCam();
  130. }
  131. Frames::~Frames()
  132. {
  133. LOG("=== Frames Destructor Start ===");
  134. // 1. 先停止所有定时器
  135. if (m_1s && m_1s->isActive()) {
  136. m_1s->stop();
  137. }
  138. if (m_fps_timer && m_fps_timer->isRunning()) {
  139. m_fps_timer->setRunning(false);
  140. }
  141. // 2. 设置停止标志,确保相机不再运行
  142. camera_sta = false;
  143. if (handle)
  144. {
  145. // 先停止抓图
  146. if (MV_CC_StopGrabbing(handle) != MV_OK) {
  147. WARN("Warning: Failed to stop grabbing");
  148. }
  149. // 等待抓图线程结束
  150. if (m_grabThread.isRunning()) {
  151. m_grabThread.cancel(); // 先尝试取消
  152. m_grabThread.waitForFinished();
  153. }
  154. // 关闭设备
  155. if (MV_CC_CloseDevice(handle) != MV_OK) {
  156. WARN("Warning: Failed to close device");
  157. }
  158. MV_CC_DestroyHandle(handle);
  159. handle = NULL;
  160. }
  161. // 等待异步图像处理任务全部结束,避免 this 被悬空访问
  162. QThreadPool::globalInstance()->waitForDone();
  163. // 清理JNI资源
  164. if (jClass) {
  165. delete jClass;
  166. jClass = nullptr;
  167. }
  168. if (m_1s) {
  169. delete m_1s;
  170. m_1s = nullptr;
  171. }
  172. if (m_fps_timer) {
  173. delete m_fps_timer;
  174. m_fps_timer = nullptr;
  175. }
  176. if(m_videoSink)
  177. {
  178. delete m_videoSink;
  179. m_videoSink = nullptr;
  180. }
  181. LOG("=== Frames Destructor End ===");
  182. }
  183. void
  184. Frames::setVideoSink( QVideoSink * newVideoSink )
  185. {
  186. LOG("setVideoSink: {}", static_cast<const void*>(newVideoSink));
  187. if( m_videoSink == newVideoSink )
  188. {
  189. LOG("m_videoSink == newVideoSink");
  190. return;
  191. }
  192. m_videoSink = newVideoSink;
  193. emit videoSinkChanged();
  194. }
  195. void
  196. Frames::setChange(bool stat_t)
  197. {
  198. LOG("开始自动对焦,发送指令到控制板,{}!",stat_t);
  199. if(stat_t){
  200. emit serial_data(m_json.Autofocus());
  201. }
  202. }
  203. void
  204. Frames::setCameraSw(bool stat_cam)
  205. {
  206. unsigned int nIP[4] = {192,168,100,100};
  207. unsigned int nMask[4] = {255,255,255,0};
  208. unsigned int nGateway[4] = {192,168,100,1};
  209. unsigned int nIpAddr = 0, nNetWorkMask = 0, nDefaultGateway = 0;
  210. z_step = 0;
  211. x_step = 0;
  212. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context());
  213. jboolean javaState = stat_cam ? JNI_TRUE :JNI_FALSE;
  214. param::camera_tc = 0;
  215. image_num = 1;
  216. camera_sta = stat_cam;
  217. if(camera_sta && m_streaming_active) return; //如果已经是打开状态,退出
  218. MVCC_FLOATVALUE stExposureTime = {0};
  219. static int fd = 0;
  220. if(camera_sta){
  221. if(param::camera_type.contains("hikrobot"))
  222. {
  223. int ret = testJNI();
  224. LOG("testJNI ret:{}",ret);
  225. #if 0
  226. if(qtContext.isValid()&&jClass->isValid()){
  227. jClass->callMethod<void>("EnumDevice","(Landroid/content/Context;)V",qtContext.object());
  228. jClass->callMethod<void>("openDevice","(Landroid/content/Context;)V",qtContext.object());
  229. }
  230. #else
  231. if (handle)
  232. {
  233. MV_CC_StopGrabbing(handle);
  234. MV_CC_CloseDevice(handle);
  235. MV_CC_DestroyHandle(handle);
  236. handle = NULL;
  237. }
  238. unsigned int nIndex = 0;
  239. MV_CC_DEVICE_INFO_LIST stDeviceList;
  240. memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  241. // 枚举设备
  242. nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
  243. if (MV_OK != nRet)
  244. {
  245. emit cameraOpenFailed("MV_CC_EnumDevices fail!");
  246. ERROR("MV_CC_EnumDevices fail! {:x}",nRet);
  247. return ;
  248. }
  249. else
  250. {
  251. LOG("MV_CC_EnumDevices Success!");
  252. }
  253. if (stDeviceList.nDeviceNum > 0)
  254. {
  255. for (int i = 0; i < (int)stDeviceList.nDeviceNum; i++)
  256. {
  257. LOG("[device {}]:", i);
  258. MV_CC_DEVICE_INFO *pDeviceInfo = stDeviceList.pDeviceInfo[i];
  259. if (NULL == pDeviceInfo)
  260. {
  261. emit cameraOpenFailed("pDeviceInfo is NULL!");
  262. ERROR("pDeviceInfo is NULL!");
  263. return;
  264. }
  265. //PrintDeviceInfo(pDeviceInfo);
  266. }
  267. }else{
  268. emit cameraOpenFailed("Find No Devices!");
  269. ERROR("Find No Devices!");
  270. // break;
  271. return;
  272. }
  273. ConvertToHexIp(&nIpAddr,nIP);
  274. ConvertToHexIp(&nNetWorkMask,nMask);
  275. ConvertToHexIp(&nDefaultGateway,nGateway);
  276. // 选择设备并创建句柄
  277. // select device and create handle
  278. nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
  279. if (MV_OK != nRet)
  280. {
  281. emit cameraOpenFailed("MV_CC_CreateHandle fail!");
  282. ERROR("MV_CC_CreateHandle fail! {:x}",nRet);
  283. return ;
  284. }else{
  285. LOG("MV_CC_CreateHandle Success!");
  286. }
  287. //判断设备Ip是否可达
  288. bool bAccessible = MV_CC_IsDeviceAccessible(stDeviceList.pDeviceInfo[nIndex],MV_ACCESS_Exclusive);
  289. if(bAccessible){
  290. //ch:设置IP配置选项 | en:set IP config
  291. nRet = MV_GIGE_SetIpConfig(handle,MV_IP_CFG_STATIC);
  292. if(MV_OK != nRet){
  293. ERROR("MV_GIGE_SetIpConfig failed:{:x}",nRet);
  294. }
  295. nRet = MV_GIGE_ForceIpEx(handle,nIpAddr,nNetWorkMask,nDefaultGateway);
  296. if(MV_OK!=nRet){
  297. ERROR("MV_GIGE_ForceIpEx failed:{:x}",nRet);
  298. }
  299. }else{
  300. nRet = MV_GIGE_ForceIpEx(handle,nIpAddr,nNetWorkMask,nDefaultGateway);
  301. if(MV_OK!=nRet){
  302. ERROR("MV_GIGE_ForceIpEx failed:{:x}",nRet);
  303. }else{
  304. LOG("MV_GIGE_ForceIpEx Success!");
  305. }
  306. if (handle)
  307. {
  308. MV_CC_DestroyHandle(handle);
  309. handle = NULL;
  310. }
  311. MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
  312. if (stDeviceList.nDeviceNum > 0)
  313. {
  314. for (int i = 0; i < (int)stDeviceList.nDeviceNum; i++)
  315. {
  316. LOG("[device {}]:", i);
  317. MV_CC_DEVICE_INFO *pDeviceInfo = stDeviceList.pDeviceInfo[i];
  318. if (NULL == pDeviceInfo)
  319. {
  320. emit cameraOpenFailed("pDeviceInfo is NULL!");
  321. ERROR("pDeviceInfo is NULL!");
  322. return;
  323. }
  324. }
  325. }else{
  326. emit cameraOpenFailed("Find No Devices!");
  327. WARN("Find No Devices!");
  328. return;
  329. }
  330. MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
  331. }
  332. nRet = MV_CC_OpenDevice(handle);
  333. if (MV_OK != nRet)
  334. {
  335. emit cameraOpenFailed("MV_CC_OpenDevice fail!");
  336. MV_CC_DestroyHandle(handle);
  337. handle = NULL;
  338. ERROR("MV_CC_OpenDevice fail! {:x}",nRet);
  339. camera_sta = false;
  340. return;
  341. }else{
  342. LOG("MV_CC_OpenDevice Success!");
  343. }
  344. // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
  345. if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
  346. {
  347. int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
  348. if (nPacketSize > 0)
  349. {
  350. nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
  351. if(nRet != MV_OK)
  352. {
  353. ERROR("Warning: Set Packet Size fail nRet:{:x}",nRet);
  354. }
  355. }
  356. else
  357. {
  358. WARN("Warning: Get Packet Size fail nRet:{}",nPacketSize);
  359. }
  360. LOG("stDeviceList.pDeviceInfo[nIndex]->nTLayerType:{}",stDeviceList.pDeviceInfo[nIndex]->nTLayerType);
  361. }
  362. // ch:设置触发模式为off | eb:Set trigger mode as off
  363. nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
  364. if (MV_OK != nRet)
  365. {
  366. ERROR("Set Trigger Mode fail! {:x}",nRet);
  367. return;
  368. }
  369. #if 0
  370. //注册抓图回调
  371. nRet = MV_CC_RegisterImageCallBackEx(handle,ImageCallBackEx,this);
  372. if (MV_OK != nRet)
  373. {
  374. unsigned int unsignedValue = static_cast<unsigned int>(nRet);
  375. ERROR("MV_CC_RegisterImageCallBackEx fail! nRet: 0x{:X}", unsignedValue);
  376. return ;
  377. }else{
  378. LOG("MV_CC_RegisterImageCallBackEx Success!");
  379. }
  380. #endif
  381. MVCC_INTVALUE_EX width,height;
  382. nRet = MV_CC_GetIntValueEx(handle, "Width", &width);
  383. if(nRet==MV_OK){
  384. LOG("[MV_CC_GetIntValueEx Width]:{}",width.nCurValue);
  385. }else{
  386. ERROR("[MV_CC_GetIntValueEx Width]:{:x}",nRet);
  387. }
  388. nRet = MV_CC_GetIntValueEx(handle, "Height", &height);
  389. if(nRet==MV_OK){
  390. LOG("[MV_CC_GetIntValueEx Height]:{}",height.nCurValue);
  391. }else{
  392. ERROR("[MV_CC_GetIntValueEx Height]:{:x}",nRet);
  393. }
  394. //获取像素格式
  395. nRet = MV_CC_GetPixelFormat(handle,&current_pixel);
  396. if(nRet==MV_OK){
  397. LOG("MV_CC_GetPixelFormat:{}",current_pixel.nCurValue);
  398. }else{
  399. ERROR("[MV_CC_GetPixelFormat Failed]:{:x}",nRet);
  400. }
  401. nRet = MV_CC_GetFloatValue(handle, "ExposureTime", &stExposureTime);
  402. if (MV_OK == nRet)
  403. {
  404. m_fExposureTime = QString::number(stExposureTime.fCurValue);
  405. emit exposureTimeChanged();
  406. LOG("exposure time current value:{}",stExposureTime.fCurValue);
  407. }
  408. else
  409. {
  410. ERROR("get exposure time failed! nRet [{:X}]", nRet);
  411. }
  412. // 设置SDK图像缓存节点个数(需在开始取流前调用),
  413. // 增大节点数避免图像处理不及时导致取流阻塞/丢帧
  414. nRet = MV_CC_SetImageNodeNum(handle, 8);
  415. if (MV_OK != nRet)
  416. {
  417. ERROR("MV_CC_SetImageNodeNum failed! nRet [{:x}]", nRet);
  418. }
  419. else
  420. {
  421. LOG("MV_CC_SetImageNodeNum Success, nNum=8");
  422. }
  423. // USB3.0 传输块大小:默认1MB,增大到2MB(Linux上限 0x200000)可提升USB吞吐,
  424. // 高分辨率大图(5.6MB/帧)时传输块过小会严重限制实际帧率
  425. {
  426. unsigned int nTransferSize = 0;
  427. nRet = MV_USB_GetTransferSize(handle, &nTransferSize);
  428. if (MV_OK == nRet) {
  429. LOG("[MV_USB_GetTransferSize] current:{}", nTransferSize);
  430. }
  431. nRet = MV_USB_SetTransferSize(handle, 0x200000);
  432. if (MV_OK != nRet)
  433. {
  434. ERROR("[MV_USB_SetTransferSize 0x200000 Failed]:{:x}", nRet);
  435. }
  436. else
  437. {
  438. nTransferSize = 0;
  439. MV_USB_GetTransferSize(handle, &nTransferSize);
  440. LOG("[MV_USB_SetTransferSize 0x200000 Success], now:{}", nTransferSize);
  441. }
  442. }
  443. // 解除 SDK 带宽限制:DeviceLinkThroughputLimitMode=0 表示不限流,
  444. // USB3.0 大图高帧率场景下若被默认带宽限制,实际交付帧率会远低于配置帧率
  445. {
  446. MVCC_ENUMVALUE enumVal = {0};
  447. nRet = MV_CC_GetEnumValue(handle, "DeviceLinkThroughputLimitMode", &enumVal);
  448. if (MV_OK == nRet) {
  449. LOG("[DeviceLinkThroughputLimitMode] current:{}", enumVal.nCurValue);
  450. nRet = MV_CC_SetEnumValue(handle, "DeviceLinkThroughputLimitMode", 0);
  451. if (MV_OK != nRet) {
  452. ERROR("[MV_CC_SetEnumValue DeviceLinkThroughputLimitMode=0 Failed]:{:x}", nRet);
  453. } else {
  454. LOG("[MV_CC_SetEnumValue DeviceLinkThroughputLimitMode=0 Success]");
  455. }
  456. } else {
  457. WARN("[DeviceLinkThroughputLimitMode 节点不存在]:{:x}", nRet);
  458. }
  459. MVCC_INTVALUE_EX intVal = {0};
  460. nRet = MV_CC_GetIntValueEx(handle, "DeviceLinkThroughputLimit", &intVal);
  461. if (MV_OK == nRet) {
  462. LOG("[DeviceLinkThroughputLimit] current:{}", intVal.nCurValue);
  463. } else {
  464. WARN("[DeviceLinkThroughputLimit 节点不存在]:{:x}", nRet);
  465. }
  466. }
  467. // 尝试提高相机采集帧率,验证相机端帧率上限
  468. // (若超出传感器能力,SDK会自动钳制到最大值,无副作用)
  469. nRet = MV_CC_SetFloatValue(handle, "AcquisitionFrameRate", 30.0f);
  470. if (MV_OK != nRet)
  471. {
  472. ERROR("[MV_CC_SetFloatValue AcquisitionFrameRate=30 Failed]:{:x}", nRet);
  473. }
  474. else
  475. {
  476. LOG("[MV_CC_SetFloatValue AcquisitionFrameRate=30 Success]");
  477. }
  478. // 开始取流
  479. // start grab image
  480. nRet = MV_CC_StartGrabbing(handle);
  481. if (MV_OK != nRet)
  482. {
  483. emit cameraOpenFailed("MV_CC_StartGrabbing fail!");
  484. camera_sta = false;
  485. ERROR("[MV_CC_StartGrabbing Failed]:{:x}",nRet);
  486. return ;
  487. }else{
  488. //创建归档路径
  489. QDateTime currentDateTime = QDateTime::currentDateTime();
  490. QString currentDateTimeString = currentDateTime.toString("yyyyMMddhhmmss");
  491. QString standPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
  492. //创建年目录
  493. QString YeayPath = standPath + "/" + currentDateTime.toString("yyyy");
  494. //createFolder(YeayPath);
  495. //创建月目录
  496. QString MonthPath = standPath + "/" + currentDateTime.toString("MM");
  497. //createFolder(YeayPath);
  498. //创建日目录
  499. QString DayPath = standPath + "/" + currentDateTime.toString("dd");
  500. //createFolder(DayPath);
  501. //创建图片归档目录
  502. picture_path_floader = standPath + "/" +
  503. currentDateTime.toString("yyyy") + "/" +
  504. currentDateTime.toString("MM") + "/" +
  505. currentDateTime.toString("dd") + "/" +
  506. currentDateTimeString;
  507. createFolder(picture_path_floader);
  508. m_fps_timer->setRunning(true);
  509. m_1s->start();
  510. m_streaming_active = true;
  511. // 读取相机实际输出帧率,确认帧率是否被相机硬件限制
  512. MVCC_FLOATVALUE stFrameRate = {0};
  513. nRet = MV_CC_GetFloatValue(handle, "AcquisitionFrameRate", &stFrameRate);
  514. if (MV_OK == nRet) {
  515. LOG("[AcquisitionFrameRate]:{}", stFrameRate.fCurValue);
  516. } else {
  517. ERROR("[MV_CC_GetFloatValue AcquisitionFrameRate Failed]:{:x}", nRet);
  518. }
  519. nRet = MV_CC_GetFloatValue(handle, "ResultingFrameRate", &stFrameRate);
  520. if (MV_OK == nRet) {
  521. LOG("[ResultingFrameRate]:{}", stFrameRate.fCurValue);
  522. } else {
  523. ERROR("[MV_CC_GetFloatValue ResultingFrameRate Failed]:{:x}", nRet);
  524. }
  525. emit startGrabbing(true);
  526. }
  527. #endif
  528. }
  529. }else{ //关闭相机
  530. if(param::camera_type.contains("qcamera")){
  531. }else if(param::camera_type.contains("uvc")){
  532. #if 1
  533. if(qtContext.isValid()&&jClass->isValid()){
  534. jClass->callMethod<void>("SetCamera_State","(Landroid/content/Context;Z)V",qtContext.object(),javaState);
  535. }
  536. #endif
  537. }else if(param::camera_type.contains("hikrobot")){
  538. nRet = MV_CC_StopGrabbing(handle);
  539. if (MV_OK != nRet)
  540. {
  541. ERROR("[MV_CC_StopGrabbing Failed]:{:x}",nRet);
  542. }
  543. #if 0
  544. // ch:注销抓图回调 | en:Unregister image callback
  545. nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
  546. if (MV_OK != nRet)
  547. {
  548. unsigned int unsignedValue = static_cast<unsigned int>(nRet);
  549. ERROR("MV_CC_RegisterImageCallBackEx fail! nRet: 0x{:X}", unsignedValue);
  550. }
  551. #endif
  552. // close device
  553. nRet = MV_CC_CloseDevice(handle);
  554. if (MV_OK != nRet)
  555. {
  556. ERROR("[MV_CC_CloseDevice Failed]:{:x}",nRet);
  557. }
  558. // 销毁句柄
  559. // destroy handle
  560. if(handle){
  561. nRet = MV_CC_DestroyHandle(handle);
  562. if (MV_OK != nRet)
  563. {
  564. ERROR("[MV_CC_DestroyHandle Failed]:{:x}",nRet);
  565. }
  566. }
  567. }
  568. m_fps_timer->setRunning(false);
  569. m_1s->stop(); //视频信息发送定时器停止
  570. m_streaming_active = false;
  571. }
  572. LOG("设置相机状态:{}",camera_sta);
  573. emit cameraStaChanged(camera_sta); //同步状态到qml
  574. emit serial_data(m_json.setLedState(camera_sta)); //设置补光灯状态
  575. if(!param::ptlist.empty())
  576. {
  577. emit signal_photo_saved();
  578. }
  579. }
  580. void
  581. Frames::setExposureTime(const QString &exptime)
  582. {
  583. bool ok = false;
  584. float exposure_time = exptime.toFloat(&ok);
  585. if(handle&&ok){
  586. //关闭自动曝光
  587. nRet = MV_CC_SetExposureAutoMode(handle,MV_EXPOSURE_AUTO_MODE_OFF);
  588. if(nRet==MV_OK){
  589. LOG("MV_CC_SetExposureAutoMode OFF Success");
  590. }else{
  591. ERROR("MV_CC_SetExposureAutoMode OFF Failed");
  592. }
  593. nRet = MV_CC_SetExposureTime(handle,exposure_time);
  594. if(nRet==MV_OK){
  595. LOG("MV_CC_SetExposureTime Success");
  596. m_fExposureTime = exptime;
  597. }else{
  598. ERROR("MV_CC_SetExposureTime Failed");
  599. }
  600. //大部分参数修改完成后,调用此命令进行保存,不建议频繁调用
  601. nRet = MV_CC_SetCommandValue(handle,"UserSetSave");
  602. if (MV_OK != nRet)
  603. {
  604. ERROR("MV_CC_SetCommandValue UserSetSave fail! nRet [{:x}]", nRet);
  605. }else{
  606. LOG("save successful!");
  607. }
  608. }
  609. emit exposureTimeChanged();
  610. }
  611. void
  612. Frames::upload_handle()
  613. {
  614. if(m_isUploading) return; //当前有上传在进行,等完成后继续下一个
  615. if(m_uploadQueue.isEmpty()) return; //上传队列为空
  616. QString first = m_uploadQueue.takeFirst();
  617. m_isUploading = true;
  618. emit signal_wait_upload(first);
  619. }
  620. void
  621. Frames::handle_reult_upload(bool success, const QString &error)
  622. {
  623. m_isUploading = false;
  624. if(success)
  625. {
  626. LOG("上传成功:{}",error.toStdString());
  627. //匹配数据库进行删除
  628. }else
  629. {
  630. WARN("上传失败:{}",error.toStdString());
  631. }
  632. upload_handle();
  633. }
  634. void
  635. Frames::mov_x_z_run(){
  636. /*z3x7*/
  637. if(z_step==3&&x_step==7){
  638. emit stopGrabbing(false);
  639. }
  640. if(z_step<=2&&x_step==7){
  641. z_step++;
  642. emit serial_data(m_json.MotorMove("movz",movX_b,"50"));
  643. }
  644. /*z3x6*/
  645. if(z_step==3&&x_step==6)
  646. {
  647. //emit stopGrabbing(false);
  648. x_step++;
  649. z_step = 0;
  650. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  651. }
  652. if(z_step<=2&&x_step==6){
  653. z_step++;
  654. emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
  655. }
  656. /*z3x5*/
  657. if(z_step==3&&x_step==5)
  658. {
  659. //emit stopGrabbing(false);
  660. x_step++;
  661. z_step = 0;
  662. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  663. }
  664. if(z_step<=2&&x_step==5){
  665. z_step++;
  666. emit serial_data(m_json.MotorMove("movz",movZ_b,"50"));
  667. }
  668. /*z3x4*/
  669. if(z_step==3&&x_step==4)
  670. {
  671. x_step++;
  672. z_step = 0;
  673. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  674. }
  675. if(z_step<=2&&x_step==4){
  676. z_step++;
  677. emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
  678. }
  679. /*z3x3*/
  680. if(z_step==3&&x_step==3)
  681. {
  682. x_step++;
  683. z_step = 0;
  684. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  685. }
  686. if(z_step<=2&&x_step==3){
  687. z_step++;
  688. emit serial_data(m_json.MotorMove("movz",movZ_b,"50"));
  689. }
  690. /**/
  691. /*z3x2*/
  692. if(z_step==3&&x_step==2)
  693. {
  694. x_step++;
  695. z_step = 0;
  696. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  697. }
  698. if(z_step<=2&&x_step==2){
  699. z_step++;
  700. emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
  701. }
  702. /**/
  703. /*z3x1*/
  704. if(z_step==3&&x_step==1)
  705. {
  706. x_step++;
  707. z_step = 0;
  708. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  709. }
  710. if(z_step<=2&&x_step==1){
  711. z_step++;
  712. emit serial_data(m_json.MotorMove("movz",movZ_b,"50"));
  713. }
  714. /*z3x0*/
  715. if(z_step==3&&x_step==0)
  716. {
  717. x_step++;
  718. z_step = 0;
  719. emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
  720. }
  721. if(z_step<=2&&x_step==0)
  722. {
  723. z_step++;
  724. emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
  725. }
  726. }
  727. void
  728. Frames::saveMatAsJpeg(const QVideoFrame &frame){
  729. // 拍照处理是低频重计算,允许OpenCV使用多线程(取流任务会自行钳制回1)
  730. cv::setNumThreads(2);
  731. QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
  732. if (frame.isValid()) {
  733. //并发互斥:saveMatAsJpeg 由 QtConcurrent 线程池多线程并发执行,
  734. //"取号-建名-存图-自增-移轴"必须整段原子化,否则两个线程会取到同一序号,
  735. //导致同名覆盖、image_num 跳号、mov_x_z_run 重复发轴指令
  736. QMutexLocker locker(&m_saveMutex);
  737. QString name = picture_path_floader + "/" + ts +
  738. "z" + QString::number(z_step) +
  739. "x" + QString::number(x_step) +
  740. "_" + QString::number(image_num) + ".jpg";
  741. QString name_sharp = picture_path_floader + "/" + ts +
  742. "z" + QString::number(z_step) +
  743. "x" + QString::number(x_step) +
  744. "_sharp" +
  745. "_" + QString::number(image_num) + ".jpg";
  746. if (frame.toImage().save(name,"jpg",90)) {
  747. //原图保存成功立即占用并递增序号,保证文件名序号连续不重复
  748. //(若等锐化任务完成后再递增,锐化未完成时下一张已拍照会复用旧序号,导致缺号)
  749. image_num++;
  750. mov_x_z_run(); //原图已保存,立即移动电机,不被锐化/对比度阻塞
  751. locker.unlock(); //锐化在后台执行,无需继续持锁
  752. //图像锐化+对比度增强+保存 sharp,异步后台处理,不阻塞抓图与电机移动
  753. QtConcurrent::run([=](){
  754. //图像锐化
  755. Mat image = imread(name.toStdString());
  756. cv::Mat fused_sharp = m_opencvfunc.unsharpMask(image, 2.0, 3.0);
  757. //对比度
  758. cv::Mat Contrast_Image = m_opencvfunc.enhanceContrast(fused_sharp,0);
  759. //左上角水印:白色字体,文字为图片名称(不含目录)
  760. cv::putText(Contrast_Image,
  761. QFileInfo(name_sharp).fileName().toStdString(),
  762. cv::Point(15, 40),
  763. cv::FONT_HERSHEY_SIMPLEX,
  764. 1.2,
  765. cv::Scalar(144,238,144), //浅绿色 lightgreen
  766. 2);
  767. std::vector<int> jpegParams = {cv::IMWRITE_JPEG_QUALITY, 95};
  768. if (!cv::imwrite(name_sharp.toStdString(), Contrast_Image, jpegParams)) {
  769. ERROR("Failed to save fused image:{}",name_sharp.toStdString());
  770. return;
  771. }
  772. LOG("[saveMatAsJpeg] sharp image path:{}", name_sharp.toStdString());
  773. //回主线程统一处理(入队/入库),避免线程池线程直接操作共享队列
  774. emit signal_sharp_processed(name_sharp);
  775. });
  776. } else {
  777. WARN("[saveMatAsJpeg] write failed:{}",name.toStdString());
  778. }
  779. }else{
  780. ERROR("[saveMatAsJpeg] empty image!");
  781. }
  782. }
  783. void
  784. Frames::onSharpProcessed(const QString &name_sharp){
  785. //主线程执行,安全操作共享队列
  786. param::ptlist.append(name_sharp);
  787. emit signal_cache_image(name_sharp); //路径入库
  788. //每处理一张锐化/对比度增强图片,立即加入上传队列异步逐张上传
  789. m_uploadQueue.append(name_sharp);
  790. QMetaObject::invokeMethod(this, "upload_handle", Qt::QueuedConnection);
  791. }
  792. void
  793. Frames::processFrameAsync(const RawFrame& frame){
  794. // ✅ 异步化:取帧线程不再阻塞在图像处理上,帧按值传入
  795. // (QByteArray 引用计数保证原始数据在任务执行期间存活),
  796. // 转换结果使用局部变量,避免多线程同时访问共享成员 m_frame。
  797. // ✅ 任务节流:取流速率(30fps)快于处理速率时限制在途任务数,
  798. // 防止线程池任务无限积压(每任务持5.6MB输入+22.5MB输出)导致内存暴涨崩溃
  799. if (!m_frameGate.tryAcquire(1))
  800. return;
  801. QtConcurrent::run([this, frame]() {
  802. FrameGateGuard gateGuard(&m_frameGate);
  803. // 限制OpenCV内部线程数:QtConcurrent任务本身已4线程并行,OpenCV再开多线程会
  804. // 叠加成4×4=16线程抢4核,demosaic耗时从24ms恶化到100ms并诱发内存访问崩溃
  805. cv::setNumThreads(1);
  806. if (frame.data.isEmpty() ||
  807. frame.data.size() < frame.width * frame.height) {
  808. return;
  809. }
  810. // [diag] 处理管线阶段耗时统计(每线程每30帧打印一次),
  811. // 用于定位 cvtColor / QVideoFrame 映射 / memcpy 各自的耗时占比
  812. thread_local int diagProcN = 0;
  813. thread_local qint64 diagCvtUs = 0;
  814. thread_local qint64 diagMapUs = 0;
  815. thread_local qint64 diagCopyUs = 0;
  816. QElapsedTimer diagT;
  817. cv::Mat raw;
  818. cv::Mat converted;
  819. diagT.start();
  820. switch (frame.pixelType) {
  821. case PixelType_Gvsp_BayerRG8:
  822. raw = cv::Mat(frame.height, frame.width, CV_8UC1,
  823. (void*)frame.data.constData());
  824. cv::cvtColor(raw, converted, cv::COLOR_BayerRG2BGRA);
  825. break;
  826. case PixelType_Gvsp_BayerGR8:
  827. raw = cv::Mat(frame.height, frame.width, CV_8UC1,
  828. (void*)frame.data.constData());
  829. cv::cvtColor(raw, converted, cv::COLOR_BayerGR2BGRA);
  830. break;
  831. case PixelType_Gvsp_BayerBG8:
  832. raw = cv::Mat(frame.height, frame.width, CV_8UC1,
  833. (void*)frame.data.constData());
  834. cv::cvtColor(raw, converted, cv::COLOR_BayerBG2BGRA);
  835. break;
  836. case PixelType_Gvsp_YUV422_YUYV_Packed:
  837. raw = cv::Mat(frame.height, frame.width, CV_8UC2,
  838. (void*)frame.data.constData());
  839. cv::cvtColor(raw, converted, cv::COLOR_YUV2RGBA_YUYV);
  840. break;
  841. default:
  842. return;
  843. }
  844. qint64 cvtUs = diagT.nsecsElapsed() / 1000;
  845. {
  846. // 加锁保存最新帧,供 saveImage / onMovDone 等读取
  847. QMutexLocker locker(&m_frameMutex);
  848. m_frame = converted;
  849. }
  850. QVideoFrameFormat vfmt(
  851. QSize(converted.cols, converted.rows),
  852. QVideoFrameFormat::Format_RGBA8888
  853. );
  854. QVideoFrame vf(vfmt);
  855. diagT.restart();
  856. if (!vf.map(QVideoFrame::WriteOnly))
  857. return;
  858. qint64 mapUs = diagT.nsecsElapsed() / 1000;
  859. diagT.restart();
  860. memcpy(vf.bits(0), converted.data,
  861. converted.total() * converted.elemSize());
  862. vf.unmap();
  863. qint64 copyUs = diagT.nsecsElapsed() / 1000;
  864. // [diag] 节流统计:每30帧打印一次平均耗时
  865. diagProcN++;
  866. diagCvtUs += cvtUs;
  867. diagMapUs += mapUs;
  868. diagCopyUs += copyUs;
  869. #if 0
  870. if (diagProcN >= 30) {
  871. LOG("[diag] proc: avg cvt={}us avg map={}us avg copy={}us", diagCvtUs/diagProcN, diagMapUs/diagProcN, diagCopyUs/diagProcN);
  872. diagProcN = 0;
  873. diagCvtUs = 0;
  874. diagMapUs = 0;
  875. diagCopyUs = 0;
  876. }
  877. #endif
  878. // ✅ 投递回 UI 线程(节流:UI线程未消费完上一帧时丢弃本帧,避免事件队列积压导致内存暴涨)
  879. if (m_displayPending.testAndSetRelaxed(0, 1)) {
  880. QMetaObject::invokeMethod(m_videoSink, [this, vf]() {
  881. m_displayPending.storeRelaxed(0);
  882. if (m_videoSink) {
  883. m_videoSink->setVideoFrame(vf);
  884. }
  885. }, Qt::QueuedConnection);
  886. }
  887. // 拍照触发:多帧可能同时在途,用锁串行化判断与清零,避免重复触发拍照
  888. bool captureShot = false;
  889. bool stopAfterShot = false;
  890. {
  891. QMutexLocker locker(&m_frameMutex);
  892. if(param::camera_tc==1||param::camera_tc==100){
  893. captureShot = true;
  894. stopAfterShot = (param::camera_tc == 100);
  895. param::camera_tc = 0;
  896. }
  897. }
  898. if(captureShot){
  899. QVideoFrame snapshot = vf;
  900. QtConcurrent::run([=](){
  901. saveMatAsJpeg(snapshot);
  902. });
  903. if (stopAfterShot) {
  904. emit stopGrabbing(false);
  905. }
  906. }
  907. });
  908. }
  909. void
  910. Frames::handleGrab(bool sta_cam){
  911. if (m_grabThread.isRunning()) return;
  912. if(m_grabThread.isFinished()){
  913. m_grabThread = QtConcurrent::run([=]() {
  914. // ch:获取数据包大小 | en:Get payload size
  915. MVCC_INTVALUE_EX stParam;
  916. memset(&stParam, 0, sizeof(MVCC_INTVALUE_EX));
  917. // cout << pUser << endl;
  918. if(sta_cam){
  919. nRet = MV_CC_GetIntValueEx(handle, "PayloadSize", &stParam);
  920. if (MV_OK != nRet)
  921. {
  922. ERROR("[MV_CC_GetIntValueEx Failed]:{:x}",nRet);
  923. return;
  924. }
  925. }
  926. if(!handle) return;
  927. const uint32_t payload = stParam.nCurValue;
  928. LOG("payload size:{}",payload);
  929. MV_FRAME_OUT stImageInfo = {0};
  930. bool flag_cam = sta_cam;
  931. std::vector<unsigned char> rgbBuf;
  932. // [diag] 取帧等待/拷贝耗时统计(每30帧打印一次),用于定位帧率瓶颈在相机还是管线
  933. thread_local int diagGrabN = 0;
  934. thread_local qint64 diagGrabWaitUs = 0;
  935. thread_local qint64 diagGrabCopyUs = 0;
  936. QElapsedTimer diagT;
  937. while(flag_cam){
  938. diagT.restart();
  939. nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 10000);
  940. qint64 waitUs = diagT.nsecsElapsed() / 1000;
  941. if (nRet != MV_OK) {
  942. ERROR("GetOneFrameTimeout:{:x}",nRet);
  943. emit stopGrabbing(false);
  944. break;
  945. }
  946. RawFrame frame;
  947. frame.width = stImageInfo.stFrameInfo.nWidth;
  948. frame.height = stImageInfo.stFrameInfo.nHeight;
  949. frame.pixelType = stImageInfo.stFrameInfo.enPixelType;
  950. m_width = frame.width;
  951. m_height = frame.height;
  952. const int dataSize = stImageInfo.stFrameInfo.nFrameLen;
  953. diagT.restart();
  954. // 尾部预留128字节padding:OpenCV demosaic(Bayer2RGB)的SIMD行尾存在越界读取,
  955. // Android(scudo分配器)大块内存后紧邻guard page,越界读会触发SEGV_ACCERR崩溃
  956. frame.data = QByteArray(dataSize + 128, Qt::Uninitialized);
  957. memcpy(frame.data.data(), stImageInfo.pBufAddr, dataSize);
  958. qint64 copyUs = diagT.nsecsElapsed() / 1000;
  959. MV_CC_FreeImageBuffer(handle, &stImageInfo);
  960. // ✅ 像素处理放到新的 QFuture
  961. processFrameAsync(frame);
  962. // ----- FPS -----
  963. m_fps_timer->incrementFrame();
  964. #if 0
  965. // [diag] 节流统计:每30帧打印一次平均耗时
  966. diagGrabN++;
  967. diagGrabWaitUs += waitUs;
  968. diagGrabCopyUs += copyUs;
  969. if (diagGrabN >= 30) {
  970. // payload/waitUs = MB/s(每帧字节数 ÷ 每帧等待微秒数)
  971. qint64 avgWaitUs = diagGrabWaitUs / diagGrabN;
  972. LOG("[diag] grab: avg wait={}us avg copy={}us avg bw={}MB/s",
  973. avgWaitUs, diagGrabCopyUs / diagGrabN, (int)((qint64)payload / avgWaitUs));
  974. diagGrabN = 0;
  975. diagGrabWaitUs = 0;
  976. diagGrabCopyUs = 0;
  977. }
  978. #endif
  979. #if 1
  980. if (param::camera_tc == -1) {
  981. emit stopGrabbing(false);
  982. flag_cam = false;
  983. break;
  984. }
  985. #endif
  986. }
  987. });
  988. }
  989. }
  990. QString
  991. Frames::cam_ExposureTime() const
  992. {
  993. return m_fExposureTime;
  994. }
  995. void
  996. Frames::registerQmlType()
  997. {
  998. qmlRegisterType< Qt6QCamera::Frames > ( "Frames", 0, 1, "Frames" );
  999. }
  1000. int
  1001. Frames::testJNI(){
  1002. return QJniObject::callStaticMethod<jint>("GetUsbPermission",
  1003. "testfunc",
  1004. "()I");
  1005. }
  1006. void
  1007. Frames::initCam()
  1008. {
  1009. int ret = testJNI();
  1010. LOG("initCam:{}",ret);
  1011. }
  1012. QString
  1013. Frames::formatString() const
  1014. {
  1015. return m_formatString;
  1016. }
  1017. QString
  1018. Frames::fpsString() const
  1019. {
  1020. return m_fpsString;
  1021. }
  1022. QString
  1023. Frames::qulityString() const
  1024. {
  1025. return m_qulityString;
  1026. }
  1027. bool
  1028. Frames:: auto_clicked() const
  1029. {
  1030. return true;
  1031. }
  1032. bool
  1033. Frames:: camera_sw() const
  1034. {
  1035. return camera_sta;
  1036. }
  1037. void
  1038. Frames::onFPSUpdated(double fps){
  1039. m_fpsString = QString( "宽:%1高:%2 图像数量:%3 帧率:%4 曝光:%5" ).arg(QString::number(m_width),
  1040. QString::number(m_height),
  1041. QString::number(param::ptlist.size()),
  1042. QString::number(fps,'f',2),
  1043. cam_ExposureTime());
  1044. emit fpsStringChanged();
  1045. }
  1046. void
  1047. Frames::timer()
  1048. {
  1049. if(param::camera_tc == 10){
  1050. emit signal_stitch();
  1051. setCameraSw(false);
  1052. }
  1053. #ifdef focus_board
  1054. emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
  1055. #endif
  1056. }
  1057. void
  1058. Frames::update_qulstr(QString q_str)
  1059. {
  1060. LOG("update_qulstr:{}",q_str.toStdString());
  1061. m_qulityString = q_str;
  1062. emit qulityStringChanged();
  1063. }
  1064. void
  1065. Frames::saveImage()
  1066. {
  1067. //停止清晰度发送,关闭定时器m1s
  1068. LOG("saveImage start!");
  1069. cv::Mat snapshot;
  1070. {
  1071. // 锁内只做浅拷贝(引用计数保护数据),快速释放锁,避免阻塞取流线程写入 m_frame
  1072. QMutexLocker locker(&m_frameMutex);
  1073. if (m_frame.empty()) {
  1074. // 取流尚未写入首帧时触发(如移动完成先于取流),不更新清晰度
  1075. LOG("saveImage: m_frame empty, skip");
  1076. return;
  1077. }
  1078. snapshot = m_frame;
  1079. }
  1080. Mat image_gray;
  1081. cv::cvtColor(snapshot,image_gray,cv::COLOR_RGBA2GRAY);
  1082. double grayvalue = cv::mean(image_gray)[0];
  1083. m_qulityString = m_opencvfunc.Tenengrad(snapshot);
  1084. emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
  1085. emit qulityStringChanged();
  1086. LOG("image_gray:{}",grayvalue);
  1087. LOG("Tenengrad:{}",m_qulityString.toStdString());
  1088. if(grayvalue<10.0){
  1089. emit dataUpload(m_json.MakeJsonWarn("Photo ERROR",QString::number(grayvalue)));
  1090. setCameraSw(false); //关闭相机
  1091. emit signal_photoerr();
  1092. return;
  1093. }
  1094. //});
  1095. }
  1096. void Frames::displayFrame(const QVideoFrame &frame)
  1097. {
  1098. if(m_videoSink){
  1099. m_videoSink->setVideoFrame(frame);
  1100. }else{
  1101. ERROR("m_videoSink Error!");
  1102. }
  1103. }
  1104. //*使用capture捕获照片*/
  1105. void
  1106. Frames::captureImage(){
  1107. setCameraSw(true);
  1108. // 延时10S后触发拍照
  1109. QTimer::singleShot(10000, [](){
  1110. LOG("立即拍照!");
  1111. param::camera_tc = 100;
  1112. //创建文件夹
  1113. });
  1114. }
  1115. //*抓图按钮:模拟MCU拍照指令,下一帧触发按序号保存/锐化/上传*/
  1116. void
  1117. Frames::manualCapture(){
  1118. if(!camera_sw()){
  1119. WARN("manualCapture: 相机未打开,忽略抓图!");
  1120. return;
  1121. }
  1122. param::camera_tc = 1; //与MCU拍照指令一致:processFrameAsync检测到后保存一帧
  1123. LOG("manualCapture: 收到抓图指令!");
  1124. }
  1125. } /* namespace Qt6QCamera */