| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 |
- /*!
- \author Igor Mironchik (igor dot mironchik at gmail dot com).
- MIT License
- Copyright (c) 2022 Igor Mironchik
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #include "cameramanager.h"
- // Qt include.
- #include <QQmlEngine>
- #include <QCameraDevice>
- #include <QThreadPool>
- #include <QTimer>
- #include <QDateTime>
- #include <QDebug>
- #include <QtConcurrent/QtConcurrent>
- #include <m_workthread.h>
- #include <mserial.h>
- #include <curl/curl.h>
- #include <main.h>
- #include <m_loger.h>
- #include <QMessageBox>
- class param;
- namespace Qt6QCamera {
- // 在途帧处理任务门禁守卫:任务结束/提前返回时自动释放信号量计数
- class FrameGateGuard {
- public:
- explicit FrameGateGuard(QSemaphore* s) : m_s(s) {}
- ~FrameGateGuard() { if (m_s) m_s->release(1); }
- private:
- QSemaphore* m_s;
- };
- //< hikrobot
- void *handle = NULL;
- unsigned int nRet = MV_OK;
- MVCC_ENUMVALUE current_pixel;
- SenderCustom sender_custom;
- cv::Mat cv_frame;
- FPSTimer::FPSTimer(QObject *parent) : QObject(parent)
- {
- m_timer = new QTimer(this);
- m_elapsed_timer = new QElapsedTimer();
- m_timer->setInterval(m_interval);
- connect(m_timer, &QTimer::timeout,
- this, &FPSTimer::onTimerTimeout);
- }
- void FPSTimer::onTimerTimeout()
- {
- double seconds_elapsed = static_cast<double>(m_elapsed_timer->nsecsElapsed()) / 1000000000.0;
- m_elapsed_timer->start();
- double fps = static_cast<double>(m_frame_count) /seconds_elapsed;
- emit fpsUpdated(fps);
- m_frame_count = 0;
- }
- void FPSTimer::setInterval(int interval)
- {
- m_interval = interval;
- m_timer->setInterval(interval);
- }
- void FPSTimer::setRunning(bool running)
- {
- if(running) {
- m_timer->start(m_interval);
- m_elapsed_timer->start();
- }
- else {
- m_timer->stop();
- m_elapsed_timer->invalidate();
- emit fpsUpdated(0.0);
- }
- }
- bool FPSTimer::isRunning()
- {
- return m_timer->isActive();
- }
- //
- // Frames
- //
- Frames::Frames(QObject * parent)
- : //QVideoSink(parent),
- QObject(parent),
- m_frameGate(3),
- m_decoding(false),
- m_fps( 0 ),
- m_1s( new QTimer( this ) )
- {
- // 全局限制OpenCV内部线程数为1:取流采用QtConcurrent任务级并行(3~4任务),
- // OpenCV再开多线程会叠加成多线程抢4核,demosaic耗时恶化并诱发内存问题。
- // 拍照/融合路径(m_opencv capOpenCVThreads)会临时改回2,取流任务内会再次钳制。
- cv::setNumThreads(1);
- QMetaType id = QMetaType::fromName("cv::Mat");
- if(id.flags() == QMetaType::UnknownType) {
- qRegisterMetaType<cv::Mat>("cv::Mat");
- }
- image_num = 1;
- m_width = 0;
- m_height = 0;
- connect( m_1s, &QTimer::timeout, this, &Frames::timer);
- m_1s->setInterval( 500 ); //1000
- m_1s->stop();
- m_fps_timer = new FPSTimer(this);
- connect(m_fps_timer, &FPSTimer::fpsUpdated,
- this, &Frames::onFPSUpdated);
- connect(this,&Frames::startGrabbing,
- this,&Frames::handleGrab);
- // connect(this,&Frames::uvcframeAvailable,
- // this,&Frames::parseFrame);
- connect(this,&Frames::frameAvailable,
- this,&Frames::displayFrame);
- connect(this,&Frames::stopGrabbing,
- this,&Frames::setCameraSw);
- connect(this,&Frames::signal_photo_saved,
- this,&Frames::upload_handle);
- connect(this,&Frames::signal_sharp_processed,
- this,&Frames::onSharpProcessed,Qt::QueuedConnection);
- initCam();
- }
- Frames::~Frames()
- {
- LOG("=== Frames Destructor Start ===");
- // 1. 先停止所有定时器
- if (m_1s && m_1s->isActive()) {
- m_1s->stop();
- }
- if (m_fps_timer && m_fps_timer->isRunning()) {
- m_fps_timer->setRunning(false);
- }
- // 2. 设置停止标志,确保相机不再运行
- camera_sta = false;
- if (handle)
- {
- // 先停止抓图
- if (MV_CC_StopGrabbing(handle) != MV_OK) {
- WARN("Warning: Failed to stop grabbing");
- }
- // 等待抓图线程结束
- if (m_grabThread.isRunning()) {
- m_grabThread.cancel(); // 先尝试取消
- m_grabThread.waitForFinished();
- }
- // 关闭设备
- if (MV_CC_CloseDevice(handle) != MV_OK) {
- WARN("Warning: Failed to close device");
- }
- MV_CC_DestroyHandle(handle);
- handle = NULL;
- }
- // 等待异步图像处理任务全部结束,避免 this 被悬空访问
- QThreadPool::globalInstance()->waitForDone();
- // 清理JNI资源
- if (jClass) {
- delete jClass;
- jClass = nullptr;
- }
- if (m_1s) {
- delete m_1s;
- m_1s = nullptr;
- }
- if (m_fps_timer) {
- delete m_fps_timer;
- m_fps_timer = nullptr;
- }
- if(m_videoSink)
- {
- delete m_videoSink;
- m_videoSink = nullptr;
- }
- LOG("=== Frames Destructor End ===");
- }
- void
- Frames::setVideoSink( QVideoSink * newVideoSink )
- {
- LOG("setVideoSink: {}", static_cast<const void*>(newVideoSink));
- if( m_videoSink == newVideoSink )
- {
- LOG("m_videoSink == newVideoSink");
- return;
- }
- m_videoSink = newVideoSink;
- emit videoSinkChanged();
- }
- void
- Frames::setChange(bool stat_t)
- {
- LOG("开始自动对焦,发送指令到控制板,{}!",stat_t);
- if(stat_t){
- emit serial_data(m_json.Autofocus());
- }
- }
- void
- Frames::setCameraSw(bool stat_cam)
- {
- unsigned int nIP[4] = {192,168,100,100};
- unsigned int nMask[4] = {255,255,255,0};
- unsigned int nGateway[4] = {192,168,100,1};
- unsigned int nIpAddr = 0, nNetWorkMask = 0, nDefaultGateway = 0;
- z_step = 0;
- x_step = 0;
- auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context());
- jboolean javaState = stat_cam ? JNI_TRUE :JNI_FALSE;
- param::camera_tc = 0;
- image_num = 1;
- camera_sta = stat_cam;
- if(camera_sta && m_streaming_active) return; //如果已经是打开状态,退出
- MVCC_FLOATVALUE stExposureTime = {0};
- static int fd = 0;
- if(camera_sta){
- if(param::camera_type.contains("hikrobot"))
- {
- int ret = testJNI();
- LOG("testJNI ret:{}",ret);
- #if 0
- if(qtContext.isValid()&&jClass->isValid()){
- jClass->callMethod<void>("EnumDevice","(Landroid/content/Context;)V",qtContext.object());
- jClass->callMethod<void>("openDevice","(Landroid/content/Context;)V",qtContext.object());
- }
- #else
- if (handle)
- {
- MV_CC_StopGrabbing(handle);
- MV_CC_CloseDevice(handle);
- MV_CC_DestroyHandle(handle);
- handle = NULL;
- }
- unsigned int nIndex = 0;
- MV_CC_DEVICE_INFO_LIST stDeviceList;
- memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
- // 枚举设备
- nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
- if (MV_OK != nRet)
- {
- emit cameraOpenFailed("MV_CC_EnumDevices fail!");
- ERROR("MV_CC_EnumDevices fail! {:x}",nRet);
- return ;
- }
- else
- {
- LOG("MV_CC_EnumDevices Success!");
- }
- if (stDeviceList.nDeviceNum > 0)
- {
- for (int i = 0; i < (int)stDeviceList.nDeviceNum; i++)
- {
- LOG("[device {}]:", i);
- MV_CC_DEVICE_INFO *pDeviceInfo = stDeviceList.pDeviceInfo[i];
- if (NULL == pDeviceInfo)
- {
- emit cameraOpenFailed("pDeviceInfo is NULL!");
- ERROR("pDeviceInfo is NULL!");
- return;
- }
- //PrintDeviceInfo(pDeviceInfo);
- }
- }else{
- emit cameraOpenFailed("Find No Devices!");
- ERROR("Find No Devices!");
- // break;
- return;
- }
- ConvertToHexIp(&nIpAddr,nIP);
- ConvertToHexIp(&nNetWorkMask,nMask);
- ConvertToHexIp(&nDefaultGateway,nGateway);
- // 选择设备并创建句柄
- // select device and create handle
- nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
- if (MV_OK != nRet)
- {
- emit cameraOpenFailed("MV_CC_CreateHandle fail!");
- ERROR("MV_CC_CreateHandle fail! {:x}",nRet);
- return ;
- }else{
- LOG("MV_CC_CreateHandle Success!");
- }
- //判断设备Ip是否可达
- bool bAccessible = MV_CC_IsDeviceAccessible(stDeviceList.pDeviceInfo[nIndex],MV_ACCESS_Exclusive);
- if(bAccessible){
- //ch:设置IP配置选项 | en:set IP config
- nRet = MV_GIGE_SetIpConfig(handle,MV_IP_CFG_STATIC);
- if(MV_OK != nRet){
- ERROR("MV_GIGE_SetIpConfig failed:{:x}",nRet);
- }
- nRet = MV_GIGE_ForceIpEx(handle,nIpAddr,nNetWorkMask,nDefaultGateway);
- if(MV_OK!=nRet){
- ERROR("MV_GIGE_ForceIpEx failed:{:x}",nRet);
- }
- }else{
- nRet = MV_GIGE_ForceIpEx(handle,nIpAddr,nNetWorkMask,nDefaultGateway);
- if(MV_OK!=nRet){
- ERROR("MV_GIGE_ForceIpEx failed:{:x}",nRet);
- }else{
- LOG("MV_GIGE_ForceIpEx Success!");
- }
- if (handle)
- {
- MV_CC_DestroyHandle(handle);
- handle = NULL;
- }
- MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
- if (stDeviceList.nDeviceNum > 0)
- {
- for (int i = 0; i < (int)stDeviceList.nDeviceNum; i++)
- {
- LOG("[device {}]:", i);
- MV_CC_DEVICE_INFO *pDeviceInfo = stDeviceList.pDeviceInfo[i];
- if (NULL == pDeviceInfo)
- {
- emit cameraOpenFailed("pDeviceInfo is NULL!");
- ERROR("pDeviceInfo is NULL!");
- return;
- }
- }
- }else{
- emit cameraOpenFailed("Find No Devices!");
- WARN("Find No Devices!");
- return;
- }
- MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
- }
- nRet = MV_CC_OpenDevice(handle);
- if (MV_OK != nRet)
- {
- emit cameraOpenFailed("MV_CC_OpenDevice fail!");
- MV_CC_DestroyHandle(handle);
- handle = NULL;
- ERROR("MV_CC_OpenDevice fail! {:x}",nRet);
- camera_sta = false;
- return;
- }else{
- LOG("MV_CC_OpenDevice Success!");
- }
- // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
- if (stDeviceList.pDeviceInfo[nIndex]->nTLayerType == MV_GIGE_DEVICE)
- {
- int nPacketSize = MV_CC_GetOptimalPacketSize(handle);
- if (nPacketSize > 0)
- {
- nRet = MV_CC_SetIntValue(handle,"GevSCPSPacketSize",nPacketSize);
- if(nRet != MV_OK)
- {
- ERROR("Warning: Set Packet Size fail nRet:{:x}",nRet);
- }
- }
- else
- {
- WARN("Warning: Get Packet Size fail nRet:{}",nPacketSize);
- }
- LOG("stDeviceList.pDeviceInfo[nIndex]->nTLayerType:{}",stDeviceList.pDeviceInfo[nIndex]->nTLayerType);
- }
- // ch:设置触发模式为off | eb:Set trigger mode as off
- nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
- if (MV_OK != nRet)
- {
- ERROR("Set Trigger Mode fail! {:x}",nRet);
- return;
- }
- #if 0
- //注册抓图回调
- nRet = MV_CC_RegisterImageCallBackEx(handle,ImageCallBackEx,this);
- if (MV_OK != nRet)
- {
- unsigned int unsignedValue = static_cast<unsigned int>(nRet);
- ERROR("MV_CC_RegisterImageCallBackEx fail! nRet: 0x{:X}", unsignedValue);
- return ;
- }else{
- LOG("MV_CC_RegisterImageCallBackEx Success!");
- }
- #endif
- MVCC_INTVALUE_EX width,height;
- nRet = MV_CC_GetIntValueEx(handle, "Width", &width);
- if(nRet==MV_OK){
- LOG("[MV_CC_GetIntValueEx Width]:{}",width.nCurValue);
- }else{
- ERROR("[MV_CC_GetIntValueEx Width]:{:x}",nRet);
- }
- nRet = MV_CC_GetIntValueEx(handle, "Height", &height);
- if(nRet==MV_OK){
- LOG("[MV_CC_GetIntValueEx Height]:{}",height.nCurValue);
- }else{
- ERROR("[MV_CC_GetIntValueEx Height]:{:x}",nRet);
- }
- //获取像素格式
- nRet = MV_CC_GetPixelFormat(handle,¤t_pixel);
- if(nRet==MV_OK){
- LOG("MV_CC_GetPixelFormat:{}",current_pixel.nCurValue);
- }else{
- ERROR("[MV_CC_GetPixelFormat Failed]:{:x}",nRet);
- }
- nRet = MV_CC_GetFloatValue(handle, "ExposureTime", &stExposureTime);
- if (MV_OK == nRet)
- {
- m_fExposureTime = QString::number(stExposureTime.fCurValue);
- emit exposureTimeChanged();
- LOG("exposure time current value:{}",stExposureTime.fCurValue);
- }
- else
- {
- ERROR("get exposure time failed! nRet [{:X}]", nRet);
- }
- // 设置SDK图像缓存节点个数(需在开始取流前调用),
- // 增大节点数避免图像处理不及时导致取流阻塞/丢帧
- nRet = MV_CC_SetImageNodeNum(handle, 8);
- if (MV_OK != nRet)
- {
- ERROR("MV_CC_SetImageNodeNum failed! nRet [{:x}]", nRet);
- }
- else
- {
- LOG("MV_CC_SetImageNodeNum Success, nNum=8");
- }
- // USB3.0 传输块大小:默认1MB,增大到2MB(Linux上限 0x200000)可提升USB吞吐,
- // 高分辨率大图(5.6MB/帧)时传输块过小会严重限制实际帧率
- {
- unsigned int nTransferSize = 0;
- nRet = MV_USB_GetTransferSize(handle, &nTransferSize);
- if (MV_OK == nRet) {
- LOG("[MV_USB_GetTransferSize] current:{}", nTransferSize);
- }
- nRet = MV_USB_SetTransferSize(handle, 0x200000);
- if (MV_OK != nRet)
- {
- ERROR("[MV_USB_SetTransferSize 0x200000 Failed]:{:x}", nRet);
- }
- else
- {
- nTransferSize = 0;
- MV_USB_GetTransferSize(handle, &nTransferSize);
- LOG("[MV_USB_SetTransferSize 0x200000 Success], now:{}", nTransferSize);
- }
- }
- // 解除 SDK 带宽限制:DeviceLinkThroughputLimitMode=0 表示不限流,
- // USB3.0 大图高帧率场景下若被默认带宽限制,实际交付帧率会远低于配置帧率
- {
- MVCC_ENUMVALUE enumVal = {0};
- nRet = MV_CC_GetEnumValue(handle, "DeviceLinkThroughputLimitMode", &enumVal);
- if (MV_OK == nRet) {
- LOG("[DeviceLinkThroughputLimitMode] current:{}", enumVal.nCurValue);
- nRet = MV_CC_SetEnumValue(handle, "DeviceLinkThroughputLimitMode", 0);
- if (MV_OK != nRet) {
- ERROR("[MV_CC_SetEnumValue DeviceLinkThroughputLimitMode=0 Failed]:{:x}", nRet);
- } else {
- LOG("[MV_CC_SetEnumValue DeviceLinkThroughputLimitMode=0 Success]");
- }
- } else {
- WARN("[DeviceLinkThroughputLimitMode 节点不存在]:{:x}", nRet);
- }
- MVCC_INTVALUE_EX intVal = {0};
- nRet = MV_CC_GetIntValueEx(handle, "DeviceLinkThroughputLimit", &intVal);
- if (MV_OK == nRet) {
- LOG("[DeviceLinkThroughputLimit] current:{}", intVal.nCurValue);
- } else {
- WARN("[DeviceLinkThroughputLimit 节点不存在]:{:x}", nRet);
- }
- }
- // 尝试提高相机采集帧率,验证相机端帧率上限
- // (若超出传感器能力,SDK会自动钳制到最大值,无副作用)
- nRet = MV_CC_SetFloatValue(handle, "AcquisitionFrameRate", 30.0f);
- if (MV_OK != nRet)
- {
- ERROR("[MV_CC_SetFloatValue AcquisitionFrameRate=30 Failed]:{:x}", nRet);
- }
- else
- {
- LOG("[MV_CC_SetFloatValue AcquisitionFrameRate=30 Success]");
- }
- // 开始取流
- // start grab image
- nRet = MV_CC_StartGrabbing(handle);
- if (MV_OK != nRet)
- {
- emit cameraOpenFailed("MV_CC_StartGrabbing fail!");
- camera_sta = false;
- ERROR("[MV_CC_StartGrabbing Failed]:{:x}",nRet);
- return ;
- }else{
- //创建归档路径
- QDateTime currentDateTime = QDateTime::currentDateTime();
- QString currentDateTimeString = currentDateTime.toString("yyyyMMddhhmmss");
- QString standPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
- //创建年目录
- QString YeayPath = standPath + "/" + currentDateTime.toString("yyyy");
- //createFolder(YeayPath);
- //创建月目录
- QString MonthPath = standPath + "/" + currentDateTime.toString("MM");
- //createFolder(YeayPath);
- //创建日目录
- QString DayPath = standPath + "/" + currentDateTime.toString("dd");
- //createFolder(DayPath);
- //创建图片归档目录
- picture_path_floader = standPath + "/" +
- currentDateTime.toString("yyyy") + "/" +
- currentDateTime.toString("MM") + "/" +
- currentDateTime.toString("dd") + "/" +
- currentDateTimeString;
- createFolder(picture_path_floader);
- m_fps_timer->setRunning(true);
- m_1s->start();
- m_streaming_active = true;
- // 读取相机实际输出帧率,确认帧率是否被相机硬件限制
- MVCC_FLOATVALUE stFrameRate = {0};
- nRet = MV_CC_GetFloatValue(handle, "AcquisitionFrameRate", &stFrameRate);
- if (MV_OK == nRet) {
- LOG("[AcquisitionFrameRate]:{}", stFrameRate.fCurValue);
- } else {
- ERROR("[MV_CC_GetFloatValue AcquisitionFrameRate Failed]:{:x}", nRet);
- }
- nRet = MV_CC_GetFloatValue(handle, "ResultingFrameRate", &stFrameRate);
- if (MV_OK == nRet) {
- LOG("[ResultingFrameRate]:{}", stFrameRate.fCurValue);
- } else {
- ERROR("[MV_CC_GetFloatValue ResultingFrameRate Failed]:{:x}", nRet);
- }
- emit startGrabbing(true);
- }
- #endif
- }
- }else{ //关闭相机
- if(param::camera_type.contains("qcamera")){
- }else if(param::camera_type.contains("uvc")){
- #if 1
- if(qtContext.isValid()&&jClass->isValid()){
- jClass->callMethod<void>("SetCamera_State","(Landroid/content/Context;Z)V",qtContext.object(),javaState);
- }
- #endif
- }else if(param::camera_type.contains("hikrobot")){
- nRet = MV_CC_StopGrabbing(handle);
- if (MV_OK != nRet)
- {
- ERROR("[MV_CC_StopGrabbing Failed]:{:x}",nRet);
- }
- #if 0
- // ch:注销抓图回调 | en:Unregister image callback
- nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
- if (MV_OK != nRet)
- {
- unsigned int unsignedValue = static_cast<unsigned int>(nRet);
- ERROR("MV_CC_RegisterImageCallBackEx fail! nRet: 0x{:X}", unsignedValue);
- }
- #endif
- // close device
- nRet = MV_CC_CloseDevice(handle);
- if (MV_OK != nRet)
- {
- ERROR("[MV_CC_CloseDevice Failed]:{:x}",nRet);
- }
- // 销毁句柄
- // destroy handle
- if(handle){
- nRet = MV_CC_DestroyHandle(handle);
- if (MV_OK != nRet)
- {
- ERROR("[MV_CC_DestroyHandle Failed]:{:x}",nRet);
- }
- }
- }
- m_fps_timer->setRunning(false);
- m_1s->stop(); //视频信息发送定时器停止
- m_streaming_active = false;
- }
- LOG("设置相机状态:{}",camera_sta);
- emit cameraStaChanged(camera_sta); //同步状态到qml
- emit serial_data(m_json.setLedState(camera_sta)); //设置补光灯状态
- if(!param::ptlist.empty())
- {
- emit signal_photo_saved();
- }
- }
- void
- Frames::setExposureTime(const QString &exptime)
- {
- bool ok = false;
- float exposure_time = exptime.toFloat(&ok);
- if(handle&&ok){
- //关闭自动曝光
- nRet = MV_CC_SetExposureAutoMode(handle,MV_EXPOSURE_AUTO_MODE_OFF);
- if(nRet==MV_OK){
- LOG("MV_CC_SetExposureAutoMode OFF Success");
- }else{
- ERROR("MV_CC_SetExposureAutoMode OFF Failed");
- }
- nRet = MV_CC_SetExposureTime(handle,exposure_time);
- if(nRet==MV_OK){
- LOG("MV_CC_SetExposureTime Success");
- m_fExposureTime = exptime;
- }else{
- ERROR("MV_CC_SetExposureTime Failed");
- }
- //大部分参数修改完成后,调用此命令进行保存,不建议频繁调用
- nRet = MV_CC_SetCommandValue(handle,"UserSetSave");
- if (MV_OK != nRet)
- {
- ERROR("MV_CC_SetCommandValue UserSetSave fail! nRet [{:x}]", nRet);
- }else{
- LOG("save successful!");
- }
- }
- emit exposureTimeChanged();
- }
- void
- Frames::upload_handle()
- {
- if(m_isUploading) return; //当前有上传在进行,等完成后继续下一个
- if(m_uploadQueue.isEmpty()) return; //上传队列为空
- QString first = m_uploadQueue.takeFirst();
- m_isUploading = true;
- emit signal_wait_upload(first);
- }
- void
- Frames::handle_reult_upload(bool success, const QString &error)
- {
- m_isUploading = false;
- if(success)
- {
- LOG("上传成功:{}",error.toStdString());
- //匹配数据库进行删除
- }else
- {
- WARN("上传失败:{}",error.toStdString());
- }
- upload_handle();
- }
- void
- Frames::mov_x_z_run(){
- /*z3x7*/
- if(z_step==3&&x_step==7){
- emit stopGrabbing(false);
- }
- if(z_step<=2&&x_step==7){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movX_b,"50"));
- }
- /*z3x6*/
- if(z_step==3&&x_step==6)
- {
- //emit stopGrabbing(false);
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==6){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
- }
- /*z3x5*/
- if(z_step==3&&x_step==5)
- {
- //emit stopGrabbing(false);
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==5){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_b,"50"));
- }
- /*z3x4*/
- if(z_step==3&&x_step==4)
- {
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==4){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
- }
- /*z3x3*/
- if(z_step==3&&x_step==3)
- {
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==3){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_b,"50"));
- }
- /**/
- /*z3x2*/
- if(z_step==3&&x_step==2)
- {
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==2){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
- }
- /**/
- /*z3x1*/
- if(z_step==3&&x_step==1)
- {
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==1){
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_b,"50"));
- }
- /*z3x0*/
- if(z_step==3&&x_step==0)
- {
- x_step++;
- z_step = 0;
- emit serial_data(m_json.MotorMove("movx",movX_f,"50"));
- }
- if(z_step<=2&&x_step==0)
- {
- z_step++;
- emit serial_data(m_json.MotorMove("movz",movZ_f,"50"));
- }
- }
- void
- Frames::saveMatAsJpeg(const QVideoFrame &frame){
- // 拍照处理是低频重计算,允许OpenCV使用多线程(取流任务会自行钳制回1)
- cv::setNumThreads(2);
- QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
- if (frame.isValid()) {
- //并发互斥:saveMatAsJpeg 由 QtConcurrent 线程池多线程并发执行,
- //"取号-建名-存图-自增-移轴"必须整段原子化,否则两个线程会取到同一序号,
- //导致同名覆盖、image_num 跳号、mov_x_z_run 重复发轴指令
- QMutexLocker locker(&m_saveMutex);
- QString name = picture_path_floader + "/" + ts +
- "z" + QString::number(z_step) +
- "x" + QString::number(x_step) +
- "_" + QString::number(image_num) + ".jpg";
- QString name_sharp = picture_path_floader + "/" + ts +
- "z" + QString::number(z_step) +
- "x" + QString::number(x_step) +
- "_sharp" +
- "_" + QString::number(image_num) + ".jpg";
- if (frame.toImage().save(name,"jpg",90)) {
- //原图保存成功立即占用并递增序号,保证文件名序号连续不重复
- //(若等锐化任务完成后再递增,锐化未完成时下一张已拍照会复用旧序号,导致缺号)
- image_num++;
- mov_x_z_run(); //原图已保存,立即移动电机,不被锐化/对比度阻塞
- locker.unlock(); //锐化在后台执行,无需继续持锁
- //图像锐化+对比度增强+保存 sharp,异步后台处理,不阻塞抓图与电机移动
- QtConcurrent::run([=](){
- //图像锐化
- Mat image = imread(name.toStdString());
- cv::Mat fused_sharp = m_opencvfunc.unsharpMask(image, 2.0, 3.0);
- //对比度
- cv::Mat Contrast_Image = m_opencvfunc.enhanceContrast(fused_sharp,0);
- //左上角水印:白色字体,文字为图片名称(不含目录)
- cv::putText(Contrast_Image,
- QFileInfo(name_sharp).fileName().toStdString(),
- cv::Point(15, 40),
- cv::FONT_HERSHEY_SIMPLEX,
- 1.2,
- cv::Scalar(144,238,144), //浅绿色 lightgreen
- 2);
- std::vector<int> jpegParams = {cv::IMWRITE_JPEG_QUALITY, 95};
- if (!cv::imwrite(name_sharp.toStdString(), Contrast_Image, jpegParams)) {
- ERROR("Failed to save fused image:{}",name_sharp.toStdString());
- return;
- }
- LOG("[saveMatAsJpeg] sharp image path:{}", name_sharp.toStdString());
- //回主线程统一处理(入队/入库),避免线程池线程直接操作共享队列
- emit signal_sharp_processed(name_sharp);
- });
- } else {
- WARN("[saveMatAsJpeg] write failed:{}",name.toStdString());
- }
- }else{
- ERROR("[saveMatAsJpeg] empty image!");
- }
- }
- void
- Frames::onSharpProcessed(const QString &name_sharp){
- //主线程执行,安全操作共享队列
- param::ptlist.append(name_sharp);
- emit signal_cache_image(name_sharp); //路径入库
- //每处理一张锐化/对比度增强图片,立即加入上传队列异步逐张上传
- m_uploadQueue.append(name_sharp);
- QMetaObject::invokeMethod(this, "upload_handle", Qt::QueuedConnection);
- }
- void
- Frames::processFrameAsync(const RawFrame& frame){
- // ✅ 异步化:取帧线程不再阻塞在图像处理上,帧按值传入
- // (QByteArray 引用计数保证原始数据在任务执行期间存活),
- // 转换结果使用局部变量,避免多线程同时访问共享成员 m_frame。
- // ✅ 任务节流:取流速率(30fps)快于处理速率时限制在途任务数,
- // 防止线程池任务无限积压(每任务持5.6MB输入+22.5MB输出)导致内存暴涨崩溃
- if (!m_frameGate.tryAcquire(1))
- return;
- QtConcurrent::run([this, frame]() {
- FrameGateGuard gateGuard(&m_frameGate);
- // 限制OpenCV内部线程数:QtConcurrent任务本身已4线程并行,OpenCV再开多线程会
- // 叠加成4×4=16线程抢4核,demosaic耗时从24ms恶化到100ms并诱发内存访问崩溃
- cv::setNumThreads(1);
- if (frame.data.isEmpty() ||
- frame.data.size() < frame.width * frame.height) {
- return;
- }
- // [diag] 处理管线阶段耗时统计(每线程每30帧打印一次),
- // 用于定位 cvtColor / QVideoFrame 映射 / memcpy 各自的耗时占比
- thread_local int diagProcN = 0;
- thread_local qint64 diagCvtUs = 0;
- thread_local qint64 diagMapUs = 0;
- thread_local qint64 diagCopyUs = 0;
- QElapsedTimer diagT;
- cv::Mat raw;
- cv::Mat converted;
- diagT.start();
- switch (frame.pixelType) {
- case PixelType_Gvsp_BayerRG8:
- raw = cv::Mat(frame.height, frame.width, CV_8UC1,
- (void*)frame.data.constData());
- cv::cvtColor(raw, converted, cv::COLOR_BayerRG2BGRA);
- break;
- case PixelType_Gvsp_BayerGR8:
- raw = cv::Mat(frame.height, frame.width, CV_8UC1,
- (void*)frame.data.constData());
- cv::cvtColor(raw, converted, cv::COLOR_BayerGR2BGRA);
- break;
- case PixelType_Gvsp_BayerBG8:
- raw = cv::Mat(frame.height, frame.width, CV_8UC1,
- (void*)frame.data.constData());
- cv::cvtColor(raw, converted, cv::COLOR_BayerBG2BGRA);
- break;
- case PixelType_Gvsp_YUV422_YUYV_Packed:
- raw = cv::Mat(frame.height, frame.width, CV_8UC2,
- (void*)frame.data.constData());
- cv::cvtColor(raw, converted, cv::COLOR_YUV2RGBA_YUYV);
- break;
- default:
- return;
- }
- qint64 cvtUs = diagT.nsecsElapsed() / 1000;
- {
- // 加锁保存最新帧,供 saveImage / onMovDone 等读取
- QMutexLocker locker(&m_frameMutex);
- m_frame = converted;
- }
- QVideoFrameFormat vfmt(
- QSize(converted.cols, converted.rows),
- QVideoFrameFormat::Format_RGBA8888
- );
- QVideoFrame vf(vfmt);
- diagT.restart();
- if (!vf.map(QVideoFrame::WriteOnly))
- return;
- qint64 mapUs = diagT.nsecsElapsed() / 1000;
- diagT.restart();
- memcpy(vf.bits(0), converted.data,
- converted.total() * converted.elemSize());
- vf.unmap();
- qint64 copyUs = diagT.nsecsElapsed() / 1000;
- // [diag] 节流统计:每30帧打印一次平均耗时
- diagProcN++;
- diagCvtUs += cvtUs;
- diagMapUs += mapUs;
- diagCopyUs += copyUs;
- #if 0
- if (diagProcN >= 30) {
- LOG("[diag] proc: avg cvt={}us avg map={}us avg copy={}us", diagCvtUs/diagProcN, diagMapUs/diagProcN, diagCopyUs/diagProcN);
- diagProcN = 0;
- diagCvtUs = 0;
- diagMapUs = 0;
- diagCopyUs = 0;
- }
- #endif
- // ✅ 投递回 UI 线程(节流:UI线程未消费完上一帧时丢弃本帧,避免事件队列积压导致内存暴涨)
- if (m_displayPending.testAndSetRelaxed(0, 1)) {
- QMetaObject::invokeMethod(m_videoSink, [this, vf]() {
- m_displayPending.storeRelaxed(0);
- if (m_videoSink) {
- m_videoSink->setVideoFrame(vf);
- }
- }, Qt::QueuedConnection);
- }
- // 拍照触发:多帧可能同时在途,用锁串行化判断与清零,避免重复触发拍照
- bool captureShot = false;
- bool stopAfterShot = false;
- {
- QMutexLocker locker(&m_frameMutex);
- if(param::camera_tc==1||param::camera_tc==100){
- captureShot = true;
- stopAfterShot = (param::camera_tc == 100);
- param::camera_tc = 0;
- }
- }
- if(captureShot){
- QVideoFrame snapshot = vf;
- QtConcurrent::run([=](){
- saveMatAsJpeg(snapshot);
- });
- if (stopAfterShot) {
- emit stopGrabbing(false);
- }
- }
- });
- }
- void
- Frames::handleGrab(bool sta_cam){
- if (m_grabThread.isRunning()) return;
- if(m_grabThread.isFinished()){
- m_grabThread = QtConcurrent::run([=]() {
- // ch:获取数据包大小 | en:Get payload size
- MVCC_INTVALUE_EX stParam;
- memset(&stParam, 0, sizeof(MVCC_INTVALUE_EX));
- // cout << pUser << endl;
- if(sta_cam){
- nRet = MV_CC_GetIntValueEx(handle, "PayloadSize", &stParam);
- if (MV_OK != nRet)
- {
- ERROR("[MV_CC_GetIntValueEx Failed]:{:x}",nRet);
- return;
- }
- }
- if(!handle) return;
- const uint32_t payload = stParam.nCurValue;
- LOG("payload size:{}",payload);
- MV_FRAME_OUT stImageInfo = {0};
- bool flag_cam = sta_cam;
- std::vector<unsigned char> rgbBuf;
- // [diag] 取帧等待/拷贝耗时统计(每30帧打印一次),用于定位帧率瓶颈在相机还是管线
- thread_local int diagGrabN = 0;
- thread_local qint64 diagGrabWaitUs = 0;
- thread_local qint64 diagGrabCopyUs = 0;
- QElapsedTimer diagT;
- while(flag_cam){
- diagT.restart();
- nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 10000);
- qint64 waitUs = diagT.nsecsElapsed() / 1000;
- if (nRet != MV_OK) {
- ERROR("GetOneFrameTimeout:{:x}",nRet);
- emit stopGrabbing(false);
- break;
- }
- RawFrame frame;
- frame.width = stImageInfo.stFrameInfo.nWidth;
- frame.height = stImageInfo.stFrameInfo.nHeight;
- frame.pixelType = stImageInfo.stFrameInfo.enPixelType;
- m_width = frame.width;
- m_height = frame.height;
- const int dataSize = stImageInfo.stFrameInfo.nFrameLen;
- diagT.restart();
- // 尾部预留128字节padding:OpenCV demosaic(Bayer2RGB)的SIMD行尾存在越界读取,
- // Android(scudo分配器)大块内存后紧邻guard page,越界读会触发SEGV_ACCERR崩溃
- frame.data = QByteArray(dataSize + 128, Qt::Uninitialized);
- memcpy(frame.data.data(), stImageInfo.pBufAddr, dataSize);
- qint64 copyUs = diagT.nsecsElapsed() / 1000;
- MV_CC_FreeImageBuffer(handle, &stImageInfo);
- // ✅ 像素处理放到新的 QFuture
- processFrameAsync(frame);
- // ----- FPS -----
- m_fps_timer->incrementFrame();
- #if 0
- // [diag] 节流统计:每30帧打印一次平均耗时
- diagGrabN++;
- diagGrabWaitUs += waitUs;
- diagGrabCopyUs += copyUs;
- if (diagGrabN >= 30) {
- // payload/waitUs = MB/s(每帧字节数 ÷ 每帧等待微秒数)
- qint64 avgWaitUs = diagGrabWaitUs / diagGrabN;
- LOG("[diag] grab: avg wait={}us avg copy={}us avg bw={}MB/s",
- avgWaitUs, diagGrabCopyUs / diagGrabN, (int)((qint64)payload / avgWaitUs));
- diagGrabN = 0;
- diagGrabWaitUs = 0;
- diagGrabCopyUs = 0;
- }
- #endif
- #if 1
- if (param::camera_tc == -1) {
- emit stopGrabbing(false);
- flag_cam = false;
- break;
- }
- #endif
- }
- });
- }
- }
- QString
- Frames::cam_ExposureTime() const
- {
- return m_fExposureTime;
- }
- void
- Frames::registerQmlType()
- {
- qmlRegisterType< Qt6QCamera::Frames > ( "Frames", 0, 1, "Frames" );
- }
- int
- Frames::testJNI(){
- return QJniObject::callStaticMethod<jint>("GetUsbPermission",
- "testfunc",
- "()I");
- }
- void
- Frames::initCam()
- {
- int ret = testJNI();
- LOG("initCam:{}",ret);
- }
- QString
- Frames::formatString() const
- {
- return m_formatString;
- }
- QString
- Frames::fpsString() const
- {
- return m_fpsString;
- }
- QString
- Frames::qulityString() const
- {
- return m_qulityString;
- }
- bool
- Frames:: auto_clicked() const
- {
- return true;
- }
- bool
- Frames:: camera_sw() const
- {
- return camera_sta;
- }
- void
- Frames::onFPSUpdated(double fps){
- m_fpsString = QString( "宽:%1高:%2 图像数量:%3 帧率:%4 曝光:%5" ).arg(QString::number(m_width),
- QString::number(m_height),
- QString::number(param::ptlist.size()),
- QString::number(fps,'f',2),
- cam_ExposureTime());
- emit fpsStringChanged();
- }
- void
- Frames::timer()
- {
- if(param::camera_tc == 10){
- emit signal_stitch();
- setCameraSw(false);
- }
- #ifdef focus_board
- emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
- #endif
- }
- void
- Frames::update_qulstr(QString q_str)
- {
- LOG("update_qulstr:{}",q_str.toStdString());
- m_qulityString = q_str;
- emit qulityStringChanged();
- }
- void
- Frames::saveImage()
- {
- //停止清晰度发送,关闭定时器m1s
- LOG("saveImage start!");
- cv::Mat snapshot;
- {
- // 锁内只做浅拷贝(引用计数保护数据),快速释放锁,避免阻塞取流线程写入 m_frame
- QMutexLocker locker(&m_frameMutex);
- if (m_frame.empty()) {
- // 取流尚未写入首帧时触发(如移动完成先于取流),不更新清晰度
- LOG("saveImage: m_frame empty, skip");
- return;
- }
- snapshot = m_frame;
- }
- Mat image_gray;
- cv::cvtColor(snapshot,image_gray,cv::COLOR_RGBA2GRAY);
- double grayvalue = cv::mean(image_gray)[0];
- m_qulityString = m_opencvfunc.Tenengrad(snapshot);
- emit serial_data(m_json.ImageFunc(m_qulityString.toDouble()));
- emit qulityStringChanged();
- LOG("image_gray:{}",grayvalue);
- LOG("Tenengrad:{}",m_qulityString.toStdString());
- if(grayvalue<10.0){
- emit dataUpload(m_json.MakeJsonWarn("Photo ERROR",QString::number(grayvalue)));
- setCameraSw(false); //关闭相机
- emit signal_photoerr();
- return;
- }
- //});
- }
- void Frames::displayFrame(const QVideoFrame &frame)
- {
- if(m_videoSink){
- m_videoSink->setVideoFrame(frame);
- }else{
- ERROR("m_videoSink Error!");
- }
- }
- //*使用capture捕获照片*/
- void
- Frames::captureImage(){
- setCameraSw(true);
- // 延时10S后触发拍照
- QTimer::singleShot(10000, [](){
- LOG("立即拍照!");
- param::camera_tc = 100;
- //创建文件夹
- });
- }
- //*抓图按钮:模拟MCU拍照指令,下一帧触发按序号保存/锐化/上传*/
- void
- Frames::manualCapture(){
- if(!camera_sw()){
- WARN("manualCapture: 相机未打开,忽略抓图!");
- return;
- }
- param::camera_tc = 1; //与MCU拍照指令一致:processFrameAsync检测到后保存一帧
- LOG("manualCapture: 收到抓图指令!");
- }
- } /* namespace Qt6QCamera */
|