cameramanager.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. // #include "im2d.hpp"
  37. // #include "im2d_common.h"
  38. // #include "RgaUtils.h"
  39. class param;
  40. namespace Qt6QCamera {
  41. //< hikrobot
  42. void *handle = NULL;
  43. unsigned int nRet = MV_OK;
  44. MVCC_ENUMVALUE current_pixel;
  45. SenderCustom sender_custom;
  46. cv::Mat cv_frame;
  47. FPSTimer::FPSTimer(QObject *parent) : QObject(parent)
  48. {
  49. m_timer = new QTimer(this);
  50. m_elapsed_timer = new QElapsedTimer();
  51. m_timer->setInterval(m_interval);
  52. connect(m_timer, &QTimer::timeout,
  53. this, &FPSTimer::onTimerTimeout);
  54. }
  55. void FPSTimer::onTimerTimeout()
  56. {
  57. double seconds_elapsed = static_cast<double>(m_elapsed_timer->nsecsElapsed()) / 1000000000.0;
  58. m_elapsed_timer->start();
  59. double fps = static_cast<double>(m_frame_count) /seconds_elapsed;
  60. emit fpsUpdated(fps);
  61. m_frame_count = 0;
  62. }
  63. void FPSTimer::setInterval(int interval)
  64. {
  65. m_interval = interval;
  66. m_timer->setInterval(interval);
  67. }
  68. void FPSTimer::setRunning(bool running)
  69. {
  70. if(running) {
  71. m_timer->start(m_interval);
  72. m_elapsed_timer->start();
  73. }
  74. else {
  75. m_timer->stop();
  76. m_elapsed_timer->invalidate();
  77. emit fpsUpdated(0.0);
  78. }
  79. }
  80. bool FPSTimer::isRunning()
  81. {
  82. return m_timer->isActive();
  83. }
  84. //
  85. // Frames
  86. //
  87. Frames::Frames(QObject * parent)
  88. : //QVideoSink(parent),
  89. QObject(parent),
  90. m_decoding(false),
  91. m_fps( 0 ),
  92. m_1s( new QTimer( this ) )
  93. {
  94. QMetaType id = QMetaType::fromName("cv::Mat");
  95. if(id.flags() == QMetaType::UnknownType) {
  96. qRegisterMetaType<cv::Mat>("cv::Mat");
  97. }
  98. stitch_des = 0;
  99. connect( m_1s, &QTimer::timeout, this, &Frames::timer);
  100. m_1s->setInterval( 500 ); //1000
  101. m_1s->stop();
  102. m_fps_timer = new FPSTimer(this);
  103. connect(m_fps_timer, &FPSTimer::fpsUpdated,
  104. this, &Frames::onFPSUpdated);
  105. connect(this,&Frames::startGrabbing,
  106. this,&Frames::handleGrab);
  107. // connect(this,&Frames::uvcframeAvailable,
  108. // this,&Frames::parseFrame);
  109. connect(this,&Frames::frameAvailable,
  110. this,&Frames::displayFrame);
  111. connect(this,&Frames::stopGrabbing,
  112. this,&Frames::setCameraSw);
  113. connect(this,&Frames::signal_photo_saved,
  114. this,&Frames::upload_handle);
  115. initCam();
  116. }
  117. Frames::~Frames()
  118. {
  119. qDebug() << "=== Frames Destructor Start ===";
  120. // 1. 先停止所有定时器
  121. if (m_1s && m_1s->isActive()) {
  122. m_1s->stop();
  123. }
  124. if (m_fps_timer && m_fps_timer->isRunning()) {
  125. m_fps_timer->setRunning(false);
  126. }
  127. // 2. 设置停止标志,确保相机不再运行
  128. camera_sta = false;
  129. if (handle)
  130. {
  131. // 先停止抓图
  132. if (MV_CC_StopGrabbing(handle) != MV_OK) {
  133. qDebug() << "Warning: Failed to stop grabbing";
  134. }
  135. // 等待抓图线程结束
  136. if (m_grabThread.isRunning()) {
  137. m_grabThread.cancel(); // 先尝试取消
  138. m_grabThread.waitForFinished();
  139. }
  140. // 关闭设备
  141. if (MV_CC_CloseDevice(handle) != MV_OK) {
  142. qDebug() << "Warning: Failed to close device";
  143. }
  144. MV_CC_DestroyHandle(handle);
  145. handle = NULL;
  146. }
  147. // 清理JNI资源
  148. if (jClass) {
  149. delete jClass;
  150. jClass = nullptr;
  151. }
  152. if (m_1s) {
  153. delete m_1s;
  154. m_1s = nullptr;
  155. }
  156. if (m_fps_timer) {
  157. delete m_fps_timer;
  158. m_fps_timer = nullptr;
  159. }
  160. if(m_videoSink)
  161. {
  162. delete m_videoSink;
  163. m_videoSink = nullptr;
  164. }
  165. qDebug() << "=== Frames Destructor End ===";
  166. }
  167. void
  168. Frames::setVideoSink( QVideoSink * newVideoSink )
  169. {
  170. qDebug() << "setVideoSink:"<<newVideoSink;
  171. if( m_videoSink == newVideoSink )
  172. {
  173. qDebug() << "m_videoSink == newVideoSink";
  174. return;
  175. }
  176. m_videoSink = newVideoSink;
  177. emit videoSinkChanged();
  178. }
  179. void
  180. Frames::setChange(bool stat_t)
  181. {
  182. LOG("开始自动对焦,发送指令到控制板,{}!",stat_t);
  183. if(stat_t){
  184. emit serial_data(m_json.Autofocus());
  185. }
  186. }
  187. void
  188. Frames::setCameraSw(bool stat_cam)
  189. {
  190. unsigned int nIP[4] = {192,168,100,100};
  191. unsigned int nMask[4] = {255,255,255,0};
  192. unsigned int nGateway[4] = {192,168,100,1};
  193. unsigned int nIpAddr = 0, nNetWorkMask = 0, nDefaultGateway = 0;
  194. z_step = 0;
  195. x_step = 0;
  196. auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context());
  197. jboolean javaState = stat_cam ? JNI_TRUE :JNI_FALSE;
  198. param::camera_tc = 0;
  199. stitch_des = 0;
  200. camera_sta = stat_cam;
  201. if(camera_sta && m_streaming_active) return; //如果已经是打开状态,退出
  202. MVCC_FLOATVALUE stExposureTime = {0};
  203. static int fd = 0;
  204. if(camera_sta){
  205. if(param::camera_type.contains("hikrobot"))
  206. {
  207. int ret = testJNI();
  208. LOG("testJNI ret:{}",ret);
  209. #if 0
  210. if(qtContext.isValid()&&jClass->isValid()){
  211. jClass->callMethod<void>("EnumDevice","(Landroid/content/Context;)V",qtContext.object());
  212. jClass->callMethod<void>("openDevice","(Landroid/content/Context;)V",qtContext.object());
  213. }
  214. #else
  215. if (handle)
  216. {
  217. MV_CC_StopGrabbing(handle);
  218. MV_CC_CloseDevice(handle);
  219. MV_CC_DestroyHandle(handle);
  220. handle = NULL;
  221. }
  222. unsigned int nIndex = 0;
  223. MV_CC_DEVICE_INFO_LIST stDeviceList;
  224. memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  225. // 枚举设备
  226. nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
  227. if (MV_OK != nRet)
  228. {
  229. emit cameraOpenFailed("MV_CC_EnumDevices fail!");
  230. ERROR("MV_CC_EnumDevices fail! {:x}",nRet);
  231. return ;
  232. }
  233. else
  234. {
  235. LOG("MV_CC_EnumDevices Success!");
  236. }
  237. if (stDeviceList.nDeviceNum > 0)
  238. {
  239. for (int i = 0; i < (int)stDeviceList.nDeviceNum; i++)
  240. {
  241. LOG("[device {}]:", i);
  242. MV_CC_DEVICE_INFO *pDeviceInfo = stDeviceList.pDeviceInfo[i];
  243. if (NULL == pDeviceInfo)
  244. {
  245. emit cameraOpenFailed("pDeviceInfo is NULL!");
  246. ERROR("pDeviceInfo is NULL!");
  247. return;
  248. }
  249. //PrintDeviceInfo(pDeviceInfo);
  250. }
  251. }else{
  252. emit cameraOpenFailed("Find No Devices!");
  253. ERROR("Find No Devices!");
  254. // break;
  255. return;
  256. }
  257. ConvertToHexIp(&nIpAddr,nIP);
  258. ConvertToHexIp(&nNetWorkMask,nMask);
  259. ConvertToHexIp(&nDefaultGateway,nGateway);
  260. // 选择设备并创建句柄
  261. // select device and create handle
  262. nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
  263. if (MV_OK != nRet)
  264. {
  265. emit cameraOpenFailed("MV_CC_CreateHandle fail!");
  266. ERROR("MV_CC_CreateHandle fail! {:x}",nRet);
  267. return ;
  268. }else{
  269. LOG("MV_CC_CreateHandle Success!");
  270. }
  271. //判断设备Ip是否可达
  272. bool bAccessible = MV_CC_IsDeviceAccessible(stDeviceList.pDeviceInfo[nIndex],MV_ACCESS_Exclusive);
  273. if(bAccessible){
  274. //ch:设置IP配置选项 | en:set IP config
  275. nRet = MV_GIGE_SetIpConfig(handle,MV_IP_CFG_STATIC);
  276. if(MV_OK != nRet){
  277. ERROR("MV_GIGE_SetIpConfig failed:{:x}",nRet);
  278. }
  279. nRet = MV_GIGE_ForceIpEx(handle,nIpAddr,nNetWorkMask,nDefaultGateway);
  280. if(MV_OK!=nRet){
  281. ERROR("MV_GIGE_ForceIpEx failed:{:x}",nRet);
  282. }
  283. }else{
  284. nRet = MV_GIGE_ForceIpEx(handle,nIpAddr,nNetWorkMask,nDefaultGateway);
  285. if(MV_OK!=nRet){
  286. ERROR("MV_GIGE_ForceIpEx failed:{:x}",nRet);
  287. }else{
  288. LOG("MV_GIGE_ForceIpEx Success!");
  289. }
  290. if (handle)
  291. {
  292. MV_CC_DestroyHandle(handle);
  293. handle = NULL;
  294. }
  295. MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
  296. if (stDeviceList.nDeviceNum > 0)
  297. {
  298. for (int i = 0; i < (int)stDeviceList.nDeviceNum; i++)
  299. {
  300. LOG("[device {}]:", i);
  301. MV_CC_DEVICE_INFO *pDeviceInfo = stDeviceList.pDeviceInfo[i];
  302. if (NULL == pDeviceInfo)
  303. {
  304. emit cameraOpenFailed("pDeviceInfo is NULL!");
  305. qDebug() << "pDeviceInfo is NULL!";
  306. return;
  307. }
  308. }
  309. }else{
  310. emit cameraOpenFailed("Find No Devices!");
  311. WARN("Find No Devices!");
  312. return;
  313. }
  314. MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
  315. }
  316. nRet = MV_CC_OpenDevice(handle);
  317. if (MV_OK != nRet)
  318. {
  319. emit cameraOpenFailed("MV_CC_OpenDevice fail!");
  320. MV_CC_DestroyHandle(handle);
  321. handle = NULL;
  322. ERROR("MV_CC_OpenDevice fail! {:x}",nRet);
  323. camera_sta = false;
  324. return;
  325. }else{
  326. LOG("MV_CC_OpenDevice Success!");
  327. }
  328. // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
  329. if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
  330. {
  331. int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
  332. if (nPacketSize > 0)
  333. {
  334. nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
  335. if(nRet != MV_OK)
  336. {
  337. ERROR("Warning: Set Packet Size fail nRet:{:x}",nRet);
  338. }
  339. }
  340. else
  341. {
  342. WARN("Warning: Get Packet Size fail nRet:{}",nPacketSize);
  343. }
  344. LOG("stDeviceList.pDeviceInfo[nIndex]->nTLayerType:{}",stDeviceList.pDeviceInfo[nIndex]->nTLayerType);
  345. }
  346. // ch:设置触发模式为off | eb:Set trigger mode as off
  347. nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
  348. if (MV_OK != nRet)
  349. {
  350. ERROR("Set Trigger Mode fail! {:x}",nRet);
  351. return;
  352. }
  353. #if 0
  354. //注册抓图回调
  355. nRet = MV_CC_RegisterImageCallBackEx(handle,ImageCallBackEx,this);
  356. if (MV_OK != nRet)
  357. {
  358. unsigned int unsignedValue = static_cast<unsigned int>(nRet);
  359. qDebug() << "MV_CC_RegisterImageCallBackEx fail! nRet" << QString::number(unsignedValue, 16).toUpper();
  360. return ;
  361. }else{
  362. qDebug() << "MV_CC_RegisterImageCallBackEx Success!";
  363. }
  364. #endif
  365. MVCC_INTVALUE_EX width,height;
  366. nRet = MV_CC_GetIntValueEx(handle, "Width", &width);
  367. if(nRet==MV_OK){
  368. LOG("[MV_CC_GetIntValueEx Width]:{}",width.nCurValue);
  369. }else{
  370. ERROR("[MV_CC_GetIntValueEx Width]:{:x}",nRet);
  371. }
  372. nRet = MV_CC_GetIntValueEx(handle, "Height", &height);
  373. if(nRet==MV_OK){
  374. LOG("[MV_CC_GetIntValueEx Height]:{}",height.nCurValue);
  375. }else{
  376. ERROR("[MV_CC_GetIntValueEx Height]:{:x}",nRet);
  377. }
  378. //获取像素格式
  379. nRet = MV_CC_GetPixelFormat(handle,&current_pixel);
  380. if(nRet==MV_OK){
  381. LOG("MV_CC_GetPixelFormat:{}",current_pixel.nCurValue);
  382. }else{
  383. ERROR("[MV_CC_GetPixelFormat Failed]:{:x}",nRet);
  384. }
  385. nRet = MV_CC_GetFloatValue(handle, "ExposureTime", &stExposureTime);
  386. if (MV_OK == nRet)
  387. {
  388. m_fExposureTime = QString::number(stExposureTime.fCurValue);
  389. emit exposureTimeChanged();
  390. LOG("exposure time current value:{}",stExposureTime.fCurValue);
  391. }
  392. else
  393. {
  394. ERROR("get exposure time failed! nRet [{:X}]", nRet);
  395. }
  396. // 开始取流
  397. // start grab image
  398. nRet = MV_CC_StartGrabbing(handle);
  399. if (MV_OK != nRet)
  400. {
  401. emit cameraOpenFailed("MV_CC_StartGrabbing fail!");
  402. camera_sta = false;
  403. ERROR("[MV_CC_StartGrabbing Failed]:{:x}",nRet);
  404. return ;
  405. }else{
  406. //创建归档路径
  407. QDateTime currentDateTime = QDateTime::currentDateTime();
  408. QString currentDateTimeString = currentDateTime.toString("yyyyMMddhhmmss");
  409. QString standPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+ "/Pictures";
  410. //创建年目录
  411. QString YeayPath = standPath + "/" + currentDateTime.toString("yyyy");
  412. //createFolder(YeayPath);
  413. //创建月目录
  414. QString MonthPath = standPath + "/" + currentDateTime.toString("MM");
  415. //createFolder(YeayPath);
  416. //创建日目录
  417. QString DayPath = standPath + "/" + currentDateTime.toString("dd");
  418. //createFolder(DayPath);
  419. //创建图片归档目录
  420. picture_path_floader = standPath + "/" +
  421. currentDateTime.toString("yyyy") + "/" +
  422. currentDateTime.toString("MM") + "/" +
  423. currentDateTime.toString("dd") + "/" +
  424. currentDateTimeString;
  425. createFolder(picture_path_floader);
  426. m_fps_timer->setRunning(true);
  427. m_1s->start();
  428. m_streaming_active = true;
  429. emit startGrabbing(true);
  430. }
  431. #endif
  432. }
  433. }else{ //关闭相机
  434. if(param::camera_type.contains("qcamera")){
  435. }else if(param::camera_type.contains("uvc")){
  436. #if 1
  437. if(qtContext.isValid()&&jClass->isValid()){
  438. jClass->callMethod<void>("SetCamera_State","(Landroid/content/Context;Z)V",qtContext.object(),javaState);
  439. }
  440. #endif
  441. }else if(param::camera_type.contains("hikrobot")){
  442. nRet = MV_CC_StopGrabbing(handle);
  443. if (MV_OK != nRet)
  444. {
  445. ERROR("[MV_CC_StopGrabbing Failed]:{:x}",nRet);
  446. }
  447. #if 0
  448. // ch:注销抓图回调 | en:Unregister image callback
  449. nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
  450. if (MV_OK != nRet)
  451. {
  452. unsigned int unsignedValue = static_cast<unsigned int>(nRet);
  453. qDebug() << "MV_CC_RegisterImageCallBackEx fail! nRet:" << QString::number(unsignedValue, 16).toUpper();
  454. }
  455. #endif
  456. // close device
  457. nRet = MV_CC_CloseDevice(handle);
  458. if (MV_OK != nRet)
  459. {
  460. ERROR("[MV_CC_CloseDevice Failed]:{:x}",nRet);
  461. }
  462. // 销毁句柄
  463. // destroy handle
  464. if(handle){
  465. nRet = MV_CC_DestroyHandle(handle);
  466. if (MV_OK != nRet)
  467. {
  468. ERROR("[MV_CC_DestroyHandle Failed]:{:x}",nRet);
  469. }
  470. }
  471. }
  472. m_fps_timer->setRunning(false);
  473. m_1s->stop(); //视频信息发送定时器停止
  474. m_streaming_active = false;
  475. }
  476. LOG("设置相机状态:{}",camera_sta);
  477. emit cameraStaChanged(camera_sta); //同步状态到qml
  478. emit serial_data(m_json.setLedState(camera_sta)); //设置补光灯状态
  479. if(!param::ptlist.empty())
  480. {
  481. //emit signal_photo_saved();
  482. //开始景深融合
  483. }
  484. }
  485. void
  486. Frames::setExposureTime(const QString &exptime)
  487. {
  488. bool ok = false;
  489. float exposure_time = exptime.toFloat(&ok);
  490. if(handle&&ok){
  491. //关闭自动曝光
  492. nRet = MV_CC_SetExposureAutoMode(handle,MV_EXPOSURE_AUTO_MODE_OFF);
  493. if(nRet==MV_OK){
  494. LOG("MV_CC_SetExposureAutoMode OFF Success");
  495. }else{
  496. ERROR("MV_CC_SetExposureAutoMode OFF Failed");
  497. }
  498. nRet = MV_CC_SetExposureTime(handle,exposure_time);
  499. if(nRet==MV_OK){
  500. LOG("MV_CC_SetExposureTime Success");
  501. m_fExposureTime = exptime;
  502. }else{
  503. ERROR("MV_CC_SetExposureTime Failed");
  504. }
  505. //大部分参数修改完成后,调用此命令进行保存,不建议频繁调用
  506. nRet = MV_CC_SetCommandValue(handle,"UserSetSave");
  507. if (MV_OK != nRet)
  508. {
  509. ERROR("MV_CC_SetCommandValue UserSetSave fail! nRet [{:x}]", nRet);
  510. }else{
  511. LOG("save successful!");
  512. }
  513. }
  514. emit exposureTimeChanged();
  515. }
  516. void
  517. Frames::upload_handle()
  518. {
  519. if(!param::ptlist.empty()){
  520. emit signal_wait_upload(param::ptlist.first());
  521. param::ptlist.removeFirst();
  522. }
  523. }
  524. void
  525. Frames::handle_reult_upload(bool success, const QString &error)
  526. {
  527. if(success)
  528. {
  529. LOG("上传成功:{}",error.toStdString());
  530. //匹配数据库进行删除
  531. }else
  532. {
  533. WARN("上传失败:{}",error.toStdString());
  534. }
  535. upload_handle();
  536. }
  537. void
  538. Frames::saveMatAsJpeg(const QVideoFrame &frame){
  539. QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
  540. if (frame.isValid()) {
  541. QString name = picture_path_floader + "/" + ts +
  542. "z" + QString::number(z_step) +
  543. "x" + QString::number(x_step) + ".jpg";
  544. QString name_sharp = picture_path_floader + "/" + ts +
  545. "z" + QString::number(z_step) +
  546. "x" + QString::number(x_step) + "_sharp.jpg";
  547. if (frame.toImage().save(name,"jpg",100)) {
  548. //param::ptlist.append(name);
  549. LOG("[saveMatAsJpeg] saved:{}",name.toStdString());
  550. #if 1
  551. emit stopGrabbing(false);
  552. //图像锐化
  553. Mat image = imread(name.toStdString());
  554. cv::Mat fused_sharp = m_opencvfunc.unsharpMask(image, 2.0, 3.0);
  555. //对比度
  556. cv::Mat Contrast_Image = m_opencvfunc.enhanceContrast(fused_sharp,0);
  557. std::vector<int> params = {cv::IMWRITE_PNG_COMPRESSION, 3};
  558. if (!cv::imwrite(name_sharp.toStdString(), Contrast_Image, params)) {
  559. ERROR("Failed to save fused image:{}",name_sharp.toStdString());
  560. }else{
  561. param::ptlist.append(name_sharp);
  562. emit signal_photo_saved();
  563. }
  564. #else
  565. param::ptlist.append(name);
  566. emit stopGrabbing(false);
  567. #endif
  568. } else {
  569. WARN("[saveMatAsJpeg] write failed:{}",name.toStdString());
  570. }
  571. }else{
  572. ERROR("[saveMatAsJpeg] empty image!");
  573. }
  574. }
  575. void
  576. Frames::processFrameAsync(const RawFrame& frame){
  577. //QtConcurrent::run([=]() {
  578. cv::Mat raw;
  579. switch (frame.pixelType) {
  580. case PixelType_Gvsp_BayerRG8:
  581. raw = cv::Mat(frame.height, frame.width, CV_8UC1,
  582. (void*)frame.data.constData());
  583. cv::cvtColor(raw, m_frame, cv::COLOR_BayerRG2BGRA);
  584. break;
  585. case PixelType_Gvsp_BayerGR8:
  586. raw = cv::Mat(frame.height, frame.width, CV_8UC1,
  587. (void*)frame.data.constData());
  588. cv::cvtColor(raw, m_frame, cv::COLOR_BayerGR2BGRA);
  589. break;
  590. case PixelType_Gvsp_BayerBG8:
  591. raw = cv::Mat(frame.height, frame.width, CV_8UC1,
  592. (void*)frame.data.constData());
  593. cv::cvtColor(raw, m_frame, cv::COLOR_BayerBG2BGRA);
  594. break;
  595. case PixelType_Gvsp_YUV422_YUYV_Packed:
  596. raw = cv::Mat(frame.height, frame.width, CV_8UC2,
  597. (void*)frame.data.constData());
  598. cv::cvtColor(raw, m_frame, cv::COLOR_YUV2RGBA_YUYV);
  599. break;
  600. default:
  601. return;
  602. }
  603. QVideoFrameFormat vfmt(
  604. QSize(m_frame.cols, m_frame.rows),
  605. QVideoFrameFormat::Format_RGBA8888
  606. );
  607. QVideoFrame vf(vfmt);
  608. if (!vf.map(QVideoFrame::WriteOnly))
  609. return;
  610. memcpy(vf.bits(0), m_frame.data,
  611. m_frame.total() * m_frame.elemSize());
  612. vf.unmap();
  613. // ✅ 投递回 UI 线程
  614. QMetaObject::invokeMethod(m_videoSink, [this, vf]() {
  615. if (m_videoSink) {
  616. m_videoSink->setVideoFrame(vf);
  617. }
  618. }, Qt::QueuedConnection);
  619. int tc = param::camera_tc;
  620. if(tc==1||tc==100){
  621. QVideoFrame snapshot = vf;
  622. QtConcurrent::run([=](){
  623. saveMatAsJpeg(snapshot);
  624. });
  625. param::camera_tc = 0; // 保存后立即重置,确保只保存一张
  626. if (tc == 100) {
  627. emit stopGrabbing(false);
  628. return;
  629. }
  630. }
  631. //});
  632. }
  633. void
  634. Frames::handleGrab(bool sta_cam){
  635. if (m_grabThread.isRunning()) return;
  636. if(m_grabThread.isFinished()){
  637. m_grabThread = QtConcurrent::run([=]() {
  638. // ch:获取数据包大小 | en:Get payload size
  639. MVCC_INTVALUE_EX stParam;
  640. memset(&stParam, 0, sizeof(MVCC_INTVALUE_EX));
  641. // cout << pUser << endl;
  642. if(sta_cam){
  643. nRet = MV_CC_GetIntValueEx(handle, "PayloadSize", &stParam);
  644. if (MV_OK != nRet)
  645. {
  646. ERROR("[MV_CC_GetIntValueEx Failed]:{:x}",nRet);
  647. return;
  648. }
  649. }
  650. if(!handle) return;
  651. const uint32_t payload = stParam.nCurValue;
  652. LOG("payload size:{}",payload);
  653. MV_FRAME_OUT stImageInfo = {0};
  654. bool flag_cam = sta_cam;
  655. std::vector<unsigned char> rgbBuf;
  656. while(flag_cam){
  657. nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 10000);
  658. if (nRet != MV_OK) {
  659. ERROR("GetOneFrameTimeout:{:x}",nRet);
  660. emit stopGrabbing(false);
  661. break;
  662. }
  663. RawFrame frame;
  664. frame.width = stImageInfo.stFrameInfo.nWidth;
  665. frame.height = stImageInfo.stFrameInfo.nHeight;
  666. frame.pixelType = stImageInfo.stFrameInfo.enPixelType;
  667. m_width = frame.width;
  668. m_height = frame.height;
  669. const int dataSize = stImageInfo.stFrameInfo.nFrameLen;
  670. frame.data = QByteArray(
  671. (const char*)stImageInfo.pBufAddr,
  672. dataSize
  673. );
  674. MV_CC_FreeImageBuffer(handle, &stImageInfo);
  675. // ✅ 像素处理放到新的 QFuture
  676. processFrameAsync(frame);
  677. // ----- FPS -----
  678. m_fps_timer->incrementFrame();
  679. #if 1
  680. if (param::camera_tc == -1) {
  681. emit stopGrabbing(false);
  682. flag_cam = false;
  683. break;
  684. }
  685. #endif
  686. }
  687. });
  688. }
  689. }
  690. QString
  691. Frames::cam_ExposureTime() const
  692. {
  693. return m_fExposureTime;
  694. }
  695. void
  696. Frames::registerQmlType()
  697. {
  698. qmlRegisterType< Qt6QCamera::Frames > ( "Frames", 0, 1, "Frames" );
  699. }
  700. int
  701. Frames::testJNI(){
  702. return QJniObject::callStaticMethod<jint>("GetUsbPermission",
  703. "testfunc",
  704. "()I");
  705. }
  706. void
  707. Frames::initCam()
  708. {
  709. int ret = testJNI();
  710. LOG("initCam:{}",ret);
  711. }
  712. QString
  713. Frames::formatString() const
  714. {
  715. return m_formatString;
  716. }
  717. QString
  718. Frames::fpsString() const
  719. {
  720. return m_fpsString;
  721. }
  722. QString
  723. Frames::qulityString() const
  724. {
  725. return m_qulityString;
  726. }
  727. bool
  728. Frames:: auto_clicked() const
  729. {
  730. return true;
  731. }
  732. bool
  733. Frames:: camera_sw() const
  734. {
  735. return camera_sta;
  736. }
  737. void
  738. Frames::onFPSUpdated(double fps){
  739. m_fpsString = QString( "%1x%2 at %3 %4 %5" ).arg(QString::number(m_width),
  740. QString::number(m_height),
  741. QString::number(param::ptlist.size()),
  742. QString::number(fps,'f',2) + " (FPS) ",
  743. cam_ExposureTime());
  744. emit fpsStringChanged();
  745. }
  746. void
  747. Frames::timer()
  748. {
  749. if(param::camera_tc == 10){
  750. emit signal_stitch();
  751. setCameraSw(false);
  752. }
  753. #ifdef focus_board
  754. emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
  755. #endif
  756. }
  757. void
  758. Frames::update_qulstr(QString q_str)
  759. {
  760. LOG("update_qulstr:{}",q_str.toStdString());
  761. m_qulityString = q_str;
  762. emit qulityStringChanged();
  763. }
  764. void
  765. Frames::saveImage()
  766. {
  767. //停止清晰度发送,关闭定时器m1s
  768. LOG("saveImage start!");
  769. //灰度值检测,全黑则不再往下进行,上报预警消息
  770. if(m_frame.empty()) return;
  771. //QtConcurrent::run([=](){
  772. Mat image_gray;
  773. cv::cvtColor(m_frame,image_gray,cv::COLOR_RGBA2GRAY);
  774. double grayvalue = cv::mean(image_gray)[0];
  775. m_qulityString = m_opencvfunc.Tenengrad(m_frame);
  776. emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
  777. emit qulityStringChanged();
  778. LOG("image_gray:{}",grayvalue);
  779. LOG("Tenengrad:{}",m_qulityString.toStdString());
  780. if(grayvalue<5.0){
  781. emit dataUpload(m_json.MakeJsonWarn("Photo ERROR",QString::number(grayvalue)));
  782. setCameraSw(false); //关闭相机
  783. emit signal_photoerr();
  784. return;
  785. }
  786. //});
  787. }
  788. void Frames::displayFrame(const QVideoFrame &frame)
  789. {
  790. if(m_videoSink){
  791. m_videoSink->setVideoFrame(frame);
  792. }else{
  793. ERROR("m_videoSink Error!");
  794. }
  795. }
  796. //*使用capture捕获照片*/
  797. void
  798. Frames::captureImage(){
  799. setCameraSw(true);
  800. // 延时10S后触发拍照
  801. QTimer::singleShot(10000, [](){
  802. LOG("立即拍照!");
  803. param::camera_tc = 100;
  804. //创建文件夹
  805. });
  806. }
  807. } /* namespace Qt6QCamera */