|
@@ -33,6 +33,7 @@
|
|
|
#include <QTimer>
|
|
#include <QTimer>
|
|
|
#include <QDateTime>
|
|
#include <QDateTime>
|
|
|
#include <QDebug>
|
|
#include <QDebug>
|
|
|
|
|
+#include <QFileInfo>
|
|
|
#include <QtConcurrent/QtConcurrent>
|
|
#include <QtConcurrent/QtConcurrent>
|
|
|
#include <m_workthread.h>
|
|
#include <m_workthread.h>
|
|
|
#include <mserial.h>
|
|
#include <mserial.h>
|
|
@@ -116,8 +117,7 @@ Frames::Frames(QObject * parent)
|
|
|
qRegisterMetaType<cv::Mat>("cv::Mat");
|
|
qRegisterMetaType<cv::Mat>("cv::Mat");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- stitch_des = 0;
|
|
|
|
|
|
|
+ image_num = 1;
|
|
|
m_width = 0;
|
|
m_width = 0;
|
|
|
m_height = 0;
|
|
m_height = 0;
|
|
|
connect( m_1s, &QTimer::timeout, this, &Frames::timer);
|
|
connect( m_1s, &QTimer::timeout, this, &Frames::timer);
|
|
@@ -143,6 +143,9 @@ Frames::Frames(QObject * parent)
|
|
|
connect(this,&Frames::signal_photo_saved,
|
|
connect(this,&Frames::signal_photo_saved,
|
|
|
this,&Frames::upload_handle);
|
|
this,&Frames::upload_handle);
|
|
|
|
|
|
|
|
|
|
+ connect(this,&Frames::signal_sharp_processed,
|
|
|
|
|
+ this,&Frames::onSharpProcessed,Qt::QueuedConnection);
|
|
|
|
|
+
|
|
|
initCam();
|
|
initCam();
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -242,9 +245,11 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context());
|
|
auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context());
|
|
|
jboolean javaState = stat_cam ? JNI_TRUE :JNI_FALSE;
|
|
jboolean javaState = stat_cam ? JNI_TRUE :JNI_FALSE;
|
|
|
param::camera_tc = 0;
|
|
param::camera_tc = 0;
|
|
|
- stitch_des = 0;
|
|
|
|
|
|
|
+ image_num = 1;
|
|
|
camera_sta = stat_cam;
|
|
camera_sta = stat_cam;
|
|
|
if(camera_sta && m_streaming_active) return; //如果已经是打开状态,退出
|
|
if(camera_sta && m_streaming_active) return; //如果已经是打开状态,退出
|
|
|
|
|
+ //如果已经是关闭状态且句柄已销毁,防止重复关闭对无效句柄操作(MV_E_HANDLE)
|
|
|
|
|
+ if(!camera_sta && handle == nullptr && !m_streaming_active) return;
|
|
|
MVCC_FLOATVALUE stExposureTime = {0};
|
|
MVCC_FLOATVALUE stExposureTime = {0};
|
|
|
static int fd = 0;
|
|
static int fd = 0;
|
|
|
if(camera_sta){
|
|
if(camera_sta){
|
|
@@ -252,12 +257,11 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
{
|
|
{
|
|
|
int ret = testJNI();
|
|
int ret = testJNI();
|
|
|
LOG("testJNI ret:{}",ret);
|
|
LOG("testJNI ret:{}",ret);
|
|
|
-
|
|
|
|
|
#if 0
|
|
#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());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ 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
|
|
#else
|
|
|
if (handle)
|
|
if (handle)
|
|
|
{
|
|
{
|
|
@@ -497,11 +501,13 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
}else if(param::camera_type.contains("hikrobot")){
|
|
}else if(param::camera_type.contains("hikrobot")){
|
|
|
- nRet = MV_CC_StopGrabbing(handle);
|
|
|
|
|
- if (MV_OK != nRet)
|
|
|
|
|
- {
|
|
|
|
|
- ERROR("[MV_CC_StopGrabbing Failed]:{:x}",nRet);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //句柄有效才执行 SDK 关闭操作;句柄已被销毁(如并发重复关闭)时直接跳过,避免 MV_E_HANDLE
|
|
|
|
|
+ if(handle != nullptr){
|
|
|
|
|
+ nRet = MV_CC_StopGrabbing(handle);
|
|
|
|
|
+ if (MV_OK != nRet)
|
|
|
|
|
+ {
|
|
|
|
|
+ ERROR("[MV_CC_StopGrabbing Failed]:{:x}",nRet);
|
|
|
|
|
+ }
|
|
|
#if 0
|
|
#if 0
|
|
|
// ch:注销抓图回调 | en:Unregister image callback
|
|
// ch:注销抓图回调 | en:Unregister image callback
|
|
|
nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
|
|
nRet = MV_CC_RegisterImageCallBackEx(handle, NULL, NULL);
|
|
@@ -512,19 +518,20 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
// close device
|
|
// 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_CloseDevice(handle);
|
|
|
|
|
+ if (MV_OK != nRet)
|
|
|
|
|
+ {
|
|
|
|
|
+ ERROR("[MV_CC_CloseDevice Failed]:{:x}",nRet);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 销毁句柄
|
|
|
|
|
+ // destroy handle
|
|
|
nRet = MV_CC_DestroyHandle(handle);
|
|
nRet = MV_CC_DestroyHandle(handle);
|
|
|
if (MV_OK != nRet)
|
|
if (MV_OK != nRet)
|
|
|
{
|
|
{
|
|
|
ERROR("[MV_CC_DestroyHandle Failed]:{:x}",nRet);
|
|
ERROR("[MV_CC_DestroyHandle Failed]:{:x}",nRet);
|
|
|
}
|
|
}
|
|
|
|
|
+ //销毁后置空,防止悬垂指针被再次使用
|
|
|
|
|
+ handle = nullptr;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
m_fps_timer->setRunning(false);
|
|
m_fps_timer->setRunning(false);
|
|
@@ -579,15 +586,17 @@ Frames::setExposureTime(const QString &exptime)
|
|
|
void
|
|
void
|
|
|
Frames::upload_handle()
|
|
Frames::upload_handle()
|
|
|
{
|
|
{
|
|
|
- if(!param::ptlist.empty()){
|
|
|
|
|
- emit signal_wait_upload(param::ptlist.first());
|
|
|
|
|
- param::ptlist.removeFirst();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if(m_isUploading) return; //当前有上传在进行,等完成后继续下一个
|
|
|
|
|
+ if(m_uploadQueue.isEmpty()) return; //上传队列为空
|
|
|
|
|
+ QString first = m_uploadQueue.takeFirst();
|
|
|
|
|
+ m_isUploading = true;
|
|
|
|
|
+ emit signal_wait_upload(first);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void
|
|
void
|
|
|
Frames::handle_reult_upload(bool success, const QString &error)
|
|
Frames::handle_reult_upload(bool success, const QString &error)
|
|
|
{
|
|
{
|
|
|
|
|
+ m_isUploading = false;
|
|
|
if(success)
|
|
if(success)
|
|
|
{
|
|
{
|
|
|
LOG("上传成功:{}",error.toStdString());
|
|
LOG("上传成功:{}",error.toStdString());
|
|
@@ -711,27 +720,50 @@ void
|
|
|
Frames::saveMatAsJpeg(const QVideoFrame &frame){
|
|
Frames::saveMatAsJpeg(const QVideoFrame &frame){
|
|
|
QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
|
|
QString ts = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
|
|
|
if (frame.isValid()) {
|
|
if (frame.isValid()) {
|
|
|
|
|
+ //并发互斥:saveMatAsJpeg 由 QtConcurrent 线程池多线程并发执行,
|
|
|
|
|
+ //"取号-建名-存图-自增-移轴"必须整段原子化,否则两个线程会取到同一序号,
|
|
|
|
|
+ //导致同名覆盖、image_num 跳号、mov_x_z_run 重复发轴指令
|
|
|
|
|
+ QMutexLocker locker(&m_saveMutex);
|
|
|
QString name = picture_path_floader + "/" + ts +
|
|
QString name = picture_path_floader + "/" + ts +
|
|
|
"z" + QString::number(z_step) +
|
|
"z" + QString::number(z_step) +
|
|
|
- "x" + QString::number(x_step) + ".jpg";
|
|
|
|
|
- QString name_sharp = name + "_sharp.jpg";
|
|
|
|
|
|
|
+ "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)) {
|
|
if (frame.toImage().save(name,"jpg",90)) {
|
|
|
- //图像锐化
|
|
|
|
|
- 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);
|
|
|
|
|
- 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());
|
|
|
|
|
- } else {
|
|
|
|
|
- // 保存清晰图路径,用于景深融合后的清晰度对比
|
|
|
|
|
- LOG("[saveMatAsJpeg] sharp image path:{}", name_sharp.toStdString());
|
|
|
|
|
- param::ptlist.append(name_sharp);
|
|
|
|
|
- emit signal_cache_image(name_sharp); //路径入库
|
|
|
|
|
- }
|
|
|
|
|
LOG("[saveMatAsJpeg] saved:{}",name.toStdString());
|
|
LOG("[saveMatAsJpeg] saved:{}",name.toStdString());
|
|
|
- mov_x_z_run();
|
|
|
|
|
|
|
+ //原图保存成功立即占用并递增序号,保证文件名序号连续不重复
|
|
|
|
|
+ //(若等锐化任务完成后再递增,锐化未完成时下一张已拍照会复用旧序号,导致缺号)
|
|
|
|
|
+ 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 {
|
|
} else {
|
|
|
WARN("[saveMatAsJpeg] write failed:{}",name.toStdString());
|
|
WARN("[saveMatAsJpeg] write failed:{}",name.toStdString());
|
|
|
}
|
|
}
|
|
@@ -741,6 +773,16 @@ Frames::saveMatAsJpeg(const QVideoFrame &frame){
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void
|
|
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){
|
|
Frames::processFrameAsync(const RawFrame& frame){
|
|
|
//QtConcurrent::run([=]() {
|
|
//QtConcurrent::run([=]() {
|
|
|
cv::Mat raw;
|
|
cv::Mat raw;
|
|
@@ -830,14 +872,27 @@ Frames::handleGrab(bool sta_cam){
|
|
|
LOG("payload size:{}",payload);
|
|
LOG("payload size:{}",payload);
|
|
|
MV_FRAME_OUT stImageInfo = {0};
|
|
MV_FRAME_OUT stImageInfo = {0};
|
|
|
bool flag_cam = sta_cam;
|
|
bool flag_cam = sta_cam;
|
|
|
|
|
+ //连续取流超时计数,连续 N 次(每次10s)仍未取到帧才判定取流异常,避免网络抖动导致误断流
|
|
|
|
|
+ int grab_timeout_cnt = 0;
|
|
|
|
|
+ const int GRAB_TIMEOUT_MAX = 3;
|
|
|
std::vector<unsigned char> rgbBuf;
|
|
std::vector<unsigned char> rgbBuf;
|
|
|
while(flag_cam){
|
|
while(flag_cam){
|
|
|
|
|
+ if(!handle){ //句柄已被关闭/销毁,退出取流循环
|
|
|
|
|
+ flag_cam = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 10000);
|
|
nRet = MV_CC_GetImageBuffer(handle, &stImageInfo, 10000);
|
|
|
if (nRet != MV_OK) {
|
|
if (nRet != MV_OK) {
|
|
|
ERROR("GetOneFrameTimeout:{:x}",nRet);
|
|
ERROR("GetOneFrameTimeout:{:x}",nRet);
|
|
|
- emit stopGrabbing(false);
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ if(++grab_timeout_cnt >= GRAB_TIMEOUT_MAX){
|
|
|
|
|
+ //连续多次取不到帧,视为取流异常,关闭相机
|
|
|
|
|
+ emit stopGrabbing(false);
|
|
|
|
|
+ flag_cam = false;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
+ grab_timeout_cnt = 0;
|
|
|
RawFrame frame;
|
|
RawFrame frame;
|
|
|
frame.width = stImageInfo.stFrameInfo.nWidth;
|
|
frame.width = stImageInfo.stFrameInfo.nWidth;
|
|
|
frame.height = stImageInfo.stFrameInfo.nHeight;
|
|
frame.height = stImageInfo.stFrameInfo.nHeight;
|
|
@@ -958,6 +1013,11 @@ Frames::saveImage()
|
|
|
{
|
|
{
|
|
|
//停止清晰度发送,关闭定时器m1s
|
|
//停止清晰度发送,关闭定时器m1s
|
|
|
LOG("saveImage start!");
|
|
LOG("saveImage start!");
|
|
|
|
|
+ //相机未打开时直接返回,不再计算灰度值/清晰度,避免向串口发送无意义的focus值
|
|
|
|
|
+ if(!camera_sw()){
|
|
|
|
|
+ ERROR("Camera not open, saveImage abort!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
//灰度值检测,全黑则不再往下进行,上报预警消息
|
|
//灰度值检测,全黑则不再往下进行,上报预警消息
|
|
|
if(m_frame.empty()) return;
|
|
if(m_frame.empty()) return;
|
|
|
//QtConcurrent::run([=](){
|
|
//QtConcurrent::run([=](){
|
|
@@ -1001,5 +1061,16 @@ Frames::captureImage(){
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//*抓图按钮:模拟MCU拍照指令,下一帧触发按序号保存/锐化/上传*/
|
|
|
|
|
+void
|
|
|
|
|
+Frames::manualCapture(){
|
|
|
|
|
+ if(!camera_sw()){
|
|
|
|
|
+ WARN("manualCapture: 相机未打开,忽略抓图!");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ param::camera_tc = 1; //与MCU拍照指令一致:processFrameAsync检测到后保存一帧
|
|
|
|
|
+ LOG("manualCapture: 收到抓图指令!");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
|
|
|
} /* namespace Qt6QCamera */
|
|
} /* namespace Qt6QCamera */
|