332 lines
10 KiB
C++
332 lines
10 KiB
C++
// CameraManager.cpp
|
|
#include "CameraManager.h"
|
|
#include "GalaxyRuntime.h"
|
|
#include <QDebug>
|
|
#include <cmath>
|
|
|
|
const QString CameraManager::LEFT_SN = "GCQ25060290";
|
|
const QString CameraManager::RIGHT_SN = "GCQ25060284";
|
|
|
|
// Helper: gxstring <-> QString
|
|
static QString gxToQString(const GxIAPICPP::gxstring& gs)
|
|
{
|
|
return QString::fromLocal8Bit(gs.c_str());
|
|
}
|
|
static GxIAPICPP::gxstring qToGxString(const QString& qs)
|
|
{
|
|
return GxIAPICPP::gxstring(qs.toLocal8Bit().constData());
|
|
}
|
|
|
|
// Helper: grab one frame, convert to QImage
|
|
static QImage grabOneFrame(CGXDevicePointer device, CGXStreamPointer stream, int timeoutMs)
|
|
{
|
|
CImageDataPointer imgData = stream->GetImage(timeoutMs);
|
|
if (imgData.IsNull()) return QImage();
|
|
if (imgData->GetStatus() != GX_FRAME_STATUS_SUCCESS) return QImage();
|
|
|
|
int w = (int)imgData->GetWidth();
|
|
int h = (int)imgData->GetHeight();
|
|
if (w <= 0 || h <= 0) return QImage();
|
|
|
|
// Bayer -> RGB24 conversion (SDK manages the output buffer)
|
|
void* rgbBuf = imgData->ConvertToRGB24(GX_BIT_0_7, GX_RAW2RGB_NEIGHBOUR, false);
|
|
if (!rgbBuf) return QImage();
|
|
|
|
// Deep copy - SDK owns the buffer, must copy before imgData is released
|
|
QImage img((const uchar*)rgbBuf, w, h, w * 3, QImage::Format_RGB888);
|
|
return img.copy();
|
|
}
|
|
|
|
// =====================================================================
|
|
// CaptureWorker
|
|
// =====================================================================
|
|
CaptureWorker::CaptureWorker(CGXDevicePointer dev)
|
|
: m_device(dev), m_running(false)
|
|
{
|
|
}
|
|
|
|
void CaptureWorker::startCapture()
|
|
{
|
|
CGXStreamPointer stream;
|
|
try {
|
|
stream = m_device->OpenStream(0);
|
|
stream->StartGrab();
|
|
m_device->GetRemoteFeatureControl()
|
|
->GetCommandFeature("AcquisitionStart")->Execute();
|
|
m_running.store(true);
|
|
|
|
while (m_running.load()) {
|
|
try {
|
|
QImage img = grabOneFrame(m_device, stream, 200);
|
|
if (img.isNull()) continue;
|
|
|
|
// Scale down for preview (5120 -> 512)
|
|
QImage preview = img.scaled(640, 640,
|
|
Qt::KeepAspectRatio, Qt::FastTransformation);
|
|
emit frameReady(preview);
|
|
}
|
|
catch (...) {
|
|
// skip bad frame
|
|
}
|
|
}
|
|
|
|
m_device->GetRemoteFeatureControl()
|
|
->GetCommandFeature("AcquisitionStop")->Execute();
|
|
stream->StopGrab();
|
|
stream->Close();
|
|
}
|
|
catch (CGalaxyException& e) {
|
|
emit errorOccurred(QString("Capture error: %1").arg(e.what()));
|
|
try {
|
|
if (!stream.IsNull()) {
|
|
m_device->GetRemoteFeatureControl()
|
|
->GetCommandFeature("AcquisitionStop")->Execute();
|
|
stream->StopGrab();
|
|
stream->Close();
|
|
}
|
|
} catch (...) {}
|
|
}
|
|
}
|
|
|
|
void CaptureWorker::stopCapture()
|
|
{
|
|
m_running.store(false);
|
|
}
|
|
|
|
// =====================================================================
|
|
// CameraManager
|
|
// =====================================================================
|
|
CameraManager::CameraManager(QObject* parent) : QObject(parent)
|
|
{
|
|
m_serial[LEFT] = LEFT_SN;
|
|
m_serial[RIGHT] = RIGHT_SN;
|
|
for (int i = 0; i < 2; i++) {
|
|
m_connected[i] = false;
|
|
m_thread[i] = nullptr;
|
|
m_worker[i] = nullptr;
|
|
}
|
|
}
|
|
|
|
void CameraManager::setSerialNumber(CamIndex idx, const QString& serialNumber)
|
|
{
|
|
if (idx != LEFT && idx != RIGHT) return;
|
|
if (!serialNumber.trimmed().isEmpty())
|
|
m_serial[idx] = serialNumber.trimmed();
|
|
}
|
|
|
|
QString CameraManager::serialNumber(CamIndex idx) const
|
|
{
|
|
return (idx == LEFT || idx == RIGHT) ? m_serial[idx] : QString();
|
|
}
|
|
|
|
CameraManager::~CameraManager()
|
|
{
|
|
stopPreview(LEFT); stopPreview(RIGHT);
|
|
disconnectCamera(LEFT); disconnectCamera(RIGHT);
|
|
uninitSDK();
|
|
}
|
|
|
|
bool CameraManager::initSDK()
|
|
{
|
|
try {
|
|
const auto sdk = bundledGalaxySdk();
|
|
if (!sdk.isEmpty()) configureGalaxyRuntime(sdk);
|
|
IGXFactory::GetInstance().Init(); return true;
|
|
}
|
|
catch (CGalaxyException& e) {
|
|
qWarning() << "SDK init failed:" << e.what(); return false;
|
|
} catch (const std::exception& e) {
|
|
qWarning() << "Camera runtime setup failed:" << e.what(); return false;
|
|
}
|
|
}
|
|
|
|
void CameraManager::uninitSDK()
|
|
{
|
|
try { IGXFactory::GetInstance().Uninit(); } catch (...) {}
|
|
}
|
|
|
|
QStringList CameraManager::enumerateDevices()
|
|
{
|
|
QStringList list;
|
|
try {
|
|
GxIAPICPP::gxdeviceinfo_vector devList;
|
|
IGXFactory::GetInstance().UpdateAllDeviceList(1000, devList);
|
|
for (uint32_t i = 0; i < devList.size(); i++)
|
|
list << gxToQString(devList[i].GetSN());
|
|
} catch (...) {}
|
|
return list;
|
|
}
|
|
|
|
QString CameraManager::snForIndex(CamIndex idx) const
|
|
{
|
|
return serialNumber(idx);
|
|
}
|
|
|
|
bool CameraManager::connectCamera(CamIndex idx)
|
|
{
|
|
if (m_connected[idx]) return true;
|
|
try {
|
|
GxIAPICPP::gxdeviceinfo_vector devList;
|
|
IGXFactory::GetInstance().UpdateAllDeviceList(1000, devList);
|
|
|
|
QString sn = snForIndex(idx);
|
|
bool found = false;
|
|
for (uint32_t i = 0; i < devList.size(); i++) {
|
|
if (gxToQString(devList[i].GetSN()) == sn) {
|
|
found = true; break;
|
|
}
|
|
}
|
|
if (!found) {
|
|
emit errorOccurred(idx, QString("Camera %1 not found").arg(sn));
|
|
return false;
|
|
}
|
|
|
|
m_device[idx] = IGXFactory::GetInstance().OpenDeviceBySN(
|
|
qToGxString(sn), GX_ACCESS_EXCLUSIVE);
|
|
if (m_device[idx].IsNull()) {
|
|
emit errorOccurred(idx, "OpenDeviceBySN returned null");
|
|
return false;
|
|
}
|
|
m_connected[idx] = true;
|
|
emit cameraConnected(idx);
|
|
return true;
|
|
}
|
|
catch (CGalaxyException& e) {
|
|
emit errorOccurred(idx, QString("Connect failed: %1").arg(e.what()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void CameraManager::disconnectCamera(CamIndex idx)
|
|
{
|
|
stopPreview(idx);
|
|
if (!m_device[idx].IsNull()) {
|
|
try { m_device[idx]->Close(); } catch (...) {}
|
|
m_device[idx] = CGXDevicePointer(); // release smart ptr
|
|
}
|
|
m_connected[idx] = false;
|
|
emit cameraDisconnected(idx);
|
|
}
|
|
|
|
bool CameraManager::isConnected(CamIndex idx) const { return m_connected[idx]; }
|
|
|
|
bool CameraManager::startPreview(CamIndex idx)
|
|
{
|
|
if (!m_connected[idx] || m_device[idx].IsNull() || m_thread[idx])
|
|
return false;
|
|
|
|
m_worker[idx] = new CaptureWorker(m_device[idx]);
|
|
m_thread[idx] = new QThread();
|
|
m_worker[idx]->moveToThread(m_thread[idx]);
|
|
|
|
connect(m_thread[idx], &QThread::started, m_worker[idx], &CaptureWorker::startCapture);
|
|
if (idx == LEFT)
|
|
connect(m_worker[idx], &CaptureWorker::frameReady, this, &CameraManager::leftFrameReady);
|
|
else
|
|
connect(m_worker[idx], &CaptureWorker::frameReady, this, &CameraManager::rightFrameReady);
|
|
connect(m_worker[idx], &CaptureWorker::errorOccurred,
|
|
this, [this, idx](const QString& msg){ emit errorOccurred(idx, msg); });
|
|
connect(m_thread[idx], &QThread::finished, m_worker[idx], &QObject::deleteLater);
|
|
connect(m_thread[idx], &QThread::finished, m_thread[idx], &QObject::deleteLater);
|
|
|
|
m_thread[idx]->start();
|
|
return true;
|
|
}
|
|
|
|
void CameraManager::stopPreview(CamIndex idx)
|
|
{
|
|
if (m_worker[idx]) m_worker[idx]->stopCapture();
|
|
if (m_thread[idx]) {
|
|
m_thread[idx]->quit();
|
|
m_thread[idx]->wait(3000);
|
|
m_thread[idx] = nullptr;
|
|
m_worker[idx] = nullptr;
|
|
}
|
|
}
|
|
|
|
QImage CameraManager::captureSingleFrame(CamIndex idx)
|
|
{
|
|
if (!m_connected[idx] || m_device[idx].IsNull()) return QImage();
|
|
bool wasPreviewing = (m_thread[idx] != nullptr);
|
|
if (wasPreviewing) stopPreview(idx);
|
|
|
|
QImage result;
|
|
try {
|
|
CGXStreamPointer stream = m_device[idx]->OpenStream(0);
|
|
stream->StartGrab();
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetCommandFeature("AcquisitionStart")->Execute();
|
|
|
|
result = grabOneFrame(m_device[idx], stream, 3000);
|
|
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetCommandFeature("AcquisitionStop")->Execute();
|
|
stream->StopGrab();
|
|
stream->Close();
|
|
}
|
|
catch (CGalaxyException& e) {
|
|
emit errorOccurred(idx, QString("Capture failed: %1").arg(e.what()));
|
|
}
|
|
|
|
if (wasPreviewing) startPreview(idx);
|
|
return result;
|
|
}
|
|
|
|
bool CameraManager::setExposureTime(CamIndex idx, double us)
|
|
{
|
|
if (!m_connected[idx] || m_device[idx].IsNull()) return false;
|
|
try {
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetEnumFeature("ExposureAuto")->SetValue("Off");
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetFloatFeature("ExposureTime")->SetValue(us);
|
|
return true;
|
|
} catch (CGalaxyException& e) {
|
|
emit errorOccurred(idx, QString("Set exposure failed: %1").arg(e.what()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool CameraManager::getExposureTime(CamIndex idx, double& us)
|
|
{
|
|
us = 0.0;
|
|
if (!m_connected[idx] || m_device[idx].IsNull()) return false;
|
|
try {
|
|
us = m_device[idx]->GetRemoteFeatureControl()
|
|
->GetFloatFeature("ExposureTime")->GetValue();
|
|
return std::isfinite(us);
|
|
} catch (CGalaxyException& e) {
|
|
emit errorOccurred(idx, QString("Get exposure failed: %1").arg(e.what()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool CameraManager::setGain(CamIndex idx, double dB)
|
|
{
|
|
if (!m_connected[idx] || m_device[idx].IsNull()) return false;
|
|
try {
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetEnumFeature("GainAuto")->SetValue("Off");
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetFloatFeature("Gain")->SetValue(dB);
|
|
return true;
|
|
} catch (CGalaxyException& e) {
|
|
emit errorOccurred(idx, QString("Set gain failed: %1").arg(e.what()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool CameraManager::applyWhiteBalanceOnce(CamIndex idx)
|
|
{
|
|
if (!m_connected[idx] || m_device[idx].IsNull()) return false;
|
|
try {
|
|
m_device[idx]->GetRemoteFeatureControl()
|
|
->GetEnumFeature("BalanceWhiteAuto")
|
|
->SetValue("Once");
|
|
return true;
|
|
} catch (CGalaxyException& e) {
|
|
emit errorOccurred(idx, QString("Apply white balance once failed: %1").arg(e.what()));
|
|
return false;
|
|
}
|
|
}
|