Files
VisionGuide/source/CameraPanel.cpp
T
2026-10-26 10:56:56 +08:00

284 lines
13 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// CameraPanel.cpp
#include "CameraPanel.h"
#include "ui_MainWindow.h"
#include "CameraManager.h"
#include "AppSettings.h"
#include <QVBoxLayout>
#include <QComboBox>
#include <QtSerialPort/QSerialPortInfo>
#define CN_LEFT_CAM "左相机"
#define CN_RIGHT_CAM "右相机"
#define CN_LINKED "左右联动"
#define CN_START_PREVIEW "开始预览"
#define CN_STOP_PREVIEW "停止预览"
#define CN_FREEZE "冻结"
#define CN_UNFREEZE "解冻"
CameraPanel::CameraPanel(Ui::MainWindowClass& u, CameraManager* mgr, QObject* parent)
: QObject(parent), ui(u), m_mgr(mgr)
{
// 在相机参数 tab 顶部插入目标选择下拉
ui.chkLinkedCam->hide();
m_comboCamTarget = new QComboBox(ui.tabCamParams);
m_comboCamTarget->addItem(QString::fromUtf8(CN_LEFT_CAM), 0);
m_comboCamTarget->addItem(QString::fromUtf8(CN_RIGHT_CAM), 1);
m_comboCamTarget->addItem(QString::fromUtf8(CN_LINKED), 2);
if (auto* lay = qobject_cast<QVBoxLayout*>(ui.tabCamParams->layout()))
lay->insertWidget(0, m_comboCamTarget);
auto& settings = AppSettings::instance();
const int target = qBound(0, settings.getRaw(QStringLiteral("camera/paramTarget"), QStringLiteral("2")).toInt(), 2);
m_comboCamTarget->setCurrentIndex(target);
const int exposure = settings.getRaw(QStringLiteral("camera/exposureUs"), QString::number(ui.spinExposure->value())).toInt();
ui.spinExposure->setValue(exposure);
ui.sliderExposure->setValue(exposure);
const double gain = settings.getRaw(QStringLiteral("camera/gain"), QString::number(ui.spinGain->value(), 'f', 2)).toDouble();
ui.spinGain->setValue(gain);
ui.sliderGain->setValue(int(gain * 10.0));
ui.chkBandwidthLimit->setChecked(settings.getRaw(QStringLiteral("camera/bandwidthLimit"), ui.chkBandwidthLimit->isChecked() ? QStringLiteral("1") : QStringLiteral("0")) == QStringLiteral("1"));
// ── 信号连接 ─────────────────────────────────────────────
connect(ui.btnLeftConnect, &QPushButton::clicked, this, &CameraPanel::onLeftConnect);
connect(ui.btnLeftDisconnect, &QPushButton::clicked, this, &CameraPanel::onLeftDisconnect);
connect(ui.btnLeftPreview, &QPushButton::clicked, this, &CameraPanel::onLeftPreviewToggle);
connect(ui.btnLeftFreeze, &QPushButton::clicked, this, &CameraPanel::onLeftFreeze);
connect(ui.btnRightConnect, &QPushButton::clicked, this, &CameraPanel::onRightConnect);
connect(ui.btnRightDisconnect, &QPushButton::clicked, this, &CameraPanel::onRightDisconnect);
connect(ui.btnRightPreview, &QPushButton::clicked, this, &CameraPanel::onRightPreviewToggle);
connect(ui.btnRightFreeze, &QPushButton::clicked, this, &CameraPanel::onRightFreeze);
connect(ui.btnApplyCamParams, &QPushButton::clicked, this, &CameraPanel::onApplyParams);
connect(ui.btnApplyWB, &QPushButton::clicked, this, &CameraPanel::onApplyWhiteBalance);
connect(m_comboCamTarget, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &CameraPanel::onCamTargetChanged);
// 曝光 / 增益联动
connect(ui.sliderExposure, &QSlider::valueChanged, ui.spinExposure, &QSpinBox::setValue);
connect(ui.spinExposure, QOverload<int>::of(&QSpinBox::valueChanged),
ui.sliderExposure, &QSlider::setValue);
connect(ui.sliderGain, &QSlider::valueChanged, [&u](int v){ u.spinGain->setValue(v/10.0); });
// 跨线程帧信号 — QImage 值传递 + 显式 QueuedConnection
connect(m_mgr, &CameraManager::leftFrameReady,
this, &CameraPanel::onLeftFrameReady, Qt::QueuedConnection);
connect(m_mgr, &CameraManager::rightFrameReady,
this, &CameraPanel::onRightFrameReady, Qt::QueuedConnection);
connect(m_mgr, &CameraManager::errorOccurred,
this, &CameraPanel::onCameraError, Qt::QueuedConnection);
ui.btnLeftPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
ui.btnRightPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
ui.btnLeftFreeze->setText(QString::fromUtf8(CN_FREEZE));
ui.btnRightFreeze->setText(QString::fromUtf8(CN_FREEZE));
updateStates();
}
// ── 外部采集接口(MeasurePanel / OPC-UA 调用)────────────────────────
QImage CameraPanel::captureLeft() {
if (!m_mgr->isConnected(CameraManager::LEFT)) return {};
QImage img = m_mgr->captureSingleFrame(CameraManager::LEFT);
if (!img.isNull()) m_lastLeft = img;
return img;
}
QImage CameraPanel::captureRight() {
if (!m_mgr->isConnected(CameraManager::RIGHT)) return {};
QImage img = m_mgr->captureSingleFrame(CameraManager::RIGHT);
if (!img.isNull()) m_lastRight = img;
return img;
}
bool CameraPanel::leftConnected() const {
return m_mgr && m_mgr->isConnected(CameraManager::LEFT);
}
bool CameraPanel::rightConnected() const {
return m_mgr && m_mgr->isConnected(CameraManager::RIGHT);
}
// ── 按钮状态同步 ─────────────────────────────────────────────────
void CameraPanel::updateStates()
{
bool lC = m_mgr->isConnected(CameraManager::LEFT);
bool rC = m_mgr->isConnected(CameraManager::RIGHT);
ui.btnLeftConnect->setEnabled(!lC); ui.btnLeftDisconnect->setEnabled(lC);
ui.btnLeftPreview->setEnabled(lC); ui.btnLeftFreeze->setEnabled(lC && m_lPrev);
ui.labelLeftStatus->setText(lC ? QString::fromUtf8("● 已连接") : QString::fromUtf8("● 未连接"));
ui.labelLeftStatus->setStyleSheet(lC ? "color:#00cc66;" : "color:gray;");
ui.btnRightConnect->setEnabled(!rC); ui.btnRightDisconnect->setEnabled(rC);
ui.btnRightPreview->setEnabled(rC); ui.btnRightFreeze->setEnabled(rC && m_rPrev);
ui.labelRightStatus->setText(rC ? QString::fromUtf8("● 已连接") : QString::fromUtf8("● 未连接"));
ui.labelRightStatus->setStyleSheet(rC ? "color:#00cc66;" : "color:gray;");
ui.btnApplyCamParams->setEnabled(lC || rC);
ui.btnApplyWB->setEnabled(lC || rC);
}
// ── 左相机 slots ─────────────────────────────────────────────────
void CameraPanel::onLeftConnect() {
m_mgr->initSDK();
if (m_mgr->connectCamera(CameraManager::LEFT))
emit logMessage(QString::fromUtf8("左相机连接成功"));
m_lPrev = false; m_lFrozen = false;
ui.btnLeftPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
updateStates();
}
void CameraPanel::onLeftDisconnect() {
m_lPrev = false; m_lFrozen = false;
m_mgr->stopPreview(CameraManager::LEFT);
m_mgr->disconnectCamera(CameraManager::LEFT);
ui.labelLeftPreview->clear();
ui.labelLeftPreview->setText(QString::fromUtf8("左相机未连接"));
ui.btnLeftPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
emit logMessage(QString::fromUtf8("左相机已断开"));
updateStates();
}
void CameraPanel::onLeftPreviewToggle() {
if (m_lPrev) {
m_mgr->stopPreview(CameraManager::LEFT);
m_lPrev = false; m_lFrozen = false;
ui.btnLeftPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
} else {
if (m_mgr->startPreview(CameraManager::LEFT)) {
m_lPrev = true; m_lFrozen = false;
ui.btnLeftPreview->setText(QString::fromUtf8(CN_STOP_PREVIEW));
}
}
updateStates();
}
void CameraPanel::onLeftFreeze() {
m_lFrozen = !m_lFrozen;
ui.btnLeftFreeze->setText(m_lFrozen ? QString::fromUtf8(CN_UNFREEZE) : QString::fromUtf8(CN_FREEZE));
}
// ── 右相机 slots ─────────────────────────────────────────────────
void CameraPanel::onRightConnect() {
m_mgr->initSDK();
if (m_mgr->connectCamera(CameraManager::RIGHT))
emit logMessage(QString::fromUtf8("右相机连接成功"));
m_rPrev = false; m_rFrozen = false;
ui.btnRightPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
updateStates();
}
void CameraPanel::onRightDisconnect() {
m_rPrev = false; m_rFrozen = false;
m_mgr->stopPreview(CameraManager::RIGHT);
m_mgr->disconnectCamera(CameraManager::RIGHT);
ui.labelRightPreview->clear();
ui.labelRightPreview->setText(QString::fromUtf8("右相机未连接"));
ui.btnRightPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
emit logMessage(QString::fromUtf8("右相机已断开"));
updateStates();
}
void CameraPanel::onRightPreviewToggle() {
if (m_rPrev) {
m_mgr->stopPreview(CameraManager::RIGHT);
m_rPrev = false; m_rFrozen = false;
ui.btnRightPreview->setText(QString::fromUtf8(CN_START_PREVIEW));
} else {
if (m_mgr->startPreview(CameraManager::RIGHT)) {
m_rPrev = true; m_rFrozen = false;
ui.btnRightPreview->setText(QString::fromUtf8(CN_STOP_PREVIEW));
}
}
updateStates();
}
void CameraPanel::onRightFreeze() {
m_rFrozen = !m_rFrozen;
ui.btnRightFreeze->setText(m_rFrozen ? QString::fromUtf8(CN_UNFREEZE) : QString::fromUtf8(CN_FREEZE));
}
// ── 帧显示 — FastTransformation 加速预览 ───────────────────────
void CameraPanel::onLeftFrameReady(const QImage& img) {
m_lastLeft = img;
if (m_lFrozen) return;
QImage s = img.scaled(ui.labelLeftPreview->size(), Qt::KeepAspectRatio, Qt::FastTransformation);
ui.labelLeftPreview->setPixmap(QPixmap::fromImage(s));
}
void CameraPanel::onRightFrameReady(const QImage& img) {
m_lastRight = img;
if (m_rFrozen) return;
QImage s = img.scaled(ui.labelRightPreview->size(), Qt::KeepAspectRatio, Qt::FastTransformation);
ui.labelRightPreview->setPixmap(QPixmap::fromImage(s));
}
void CameraPanel::onCameraError(int idx, const QString& msg) {
emit logMessage(QString("[Camera %1] %2").arg(idx).arg(msg));
updateStates();
}
void CameraPanel::onCamTargetChanged(int) {
AppSettings::instance().set(QStringLiteral("camera/paramTarget"),
QString::number(m_comboCamTarget->currentIndex()));
int t = m_comboCamTarget->currentData().toInt();
auto idx = (t == 1) ? CameraManager::RIGHT : CameraManager::LEFT;
if (!m_mgr->isConnected(idx)) return;
try {
auto fc = m_mgr->getDevice(idx)->GetRemoteFeatureControl();
double exp = fc->GetFloatFeature("ExposureTime")->GetValue();
double gain = fc->GetFloatFeature("Gain")->GetValue();
ui.spinExposure->blockSignals(true); ui.sliderExposure->blockSignals(true);
ui.spinGain->blockSignals(true); ui.sliderGain->blockSignals(true);
ui.spinExposure->setValue(int(exp)); ui.sliderExposure->setValue(int(exp));
ui.spinGain->setValue(gain); ui.sliderGain->setValue(int(gain*10));
ui.spinExposure->blockSignals(false); ui.sliderExposure->blockSignals(false);
ui.spinGain->blockSignals(false); ui.sliderGain->blockSignals(false);
} catch (...) {}
}
void CameraPanel::onApplyParams() {
double exposure = ui.spinExposure->value();
double gain = ui.spinGain->value();
int target = m_comboCamTarget->currentData().toInt();
bool bwLimit = ui.chkBandwidthLimit->isChecked();
QString bwMode = bwLimit ? "On" : "Off";
auto& settings = AppSettings::instance();
settings.set(QStringLiteral("camera/exposureUs"), QString::number(exposure, 'f', 0));
settings.set(QStringLiteral("camera/gain"), QString::number(gain, 'f', 3));
settings.set(QStringLiteral("camera/bandwidthLimit"), bwLimit ? QStringLiteral("1") : QStringLiteral("0"));
auto apply = [&](CameraManager::CamIndex ci) {
if (!m_mgr->isConnected(ci)) return;
m_mgr->setExposureTime(ci, exposure);
m_mgr->setGain(ci, gain);
try {
auto fc = m_mgr->getDevice(ci)->GetRemoteFeatureControl();
fc->GetIntFeature("DeviceLinkSelector")->SetValue(0);
fc->GetEnumFeature("DeviceLinkThroughputLimitMode")
->SetValue(bwMode.toStdString().c_str());
} catch (...) {
emit logMessage(QString::fromUtf8("[Camera] 带宽限制模式设置失败"));
}
};
if (target == 0 || target == 2) apply(CameraManager::LEFT);
if (target == 1 || target == 2) apply(CameraManager::RIGHT);
emit logMessage(QString("Params: Exp=%1 Gain=%2")
.arg(exposure).arg(gain));
}
void CameraPanel::onApplyWhiteBalance()
{
const int target = m_comboCamTarget->currentData().toInt();
bool applied = false;
auto apply = [&](CameraManager::CamIndex ci, const QString& name) {
if (!m_mgr->isConnected(ci)) return;
if (m_mgr->applyWhiteBalanceOnce(ci)) {
applied = true;
emit logMessage(QString::fromUtf8("%1自动白平衡已执行(Once)").arg(name));
}
};
if (target == 0 || target == 2)
apply(CameraManager::LEFT, QString::fromUtf8("左相机"));
if (target == 1 || target == 2)
apply(CameraManager::RIGHT, QString::fromUtf8("右相机"));
if (!applied)
emit logMessage(QString::fromUtf8("没有已连接的相机,无法执行自动白平衡"));
}
void CameraPanel::refreshComPorts() {
// 留空,供 LightPanel 使用(串口枚举逻辑移到 LightPanel)
}