add,单反相机2:
1、实现3s拍照; 2、设置照片保存路径; 3、轮播看板:显示单反实时视频流;
This commit is contained in:
@ -1,12 +1,14 @@
|
||||
#include "SingleLensReflexCameraWindow.h"
|
||||
#include <QMessageBox>
|
||||
#include <QCoreApplication>
|
||||
#include <QBuffer>
|
||||
|
||||
// 全局变量用于回调函数访问
|
||||
static SingleLensReflexCameraOperation* g_cameraOperation = nullptr;
|
||||
|
||||
SingleLensReflexCameraWindow::SingleLensReflexCameraWindow(QWidget* parent)
|
||||
: QDialog(parent)
|
||||
, m_isCapturing(false)
|
||||
{
|
||||
ui.setupUi(this);
|
||||
|
||||
@ -15,24 +17,39 @@ SingleLensReflexCameraWindow::SingleLensReflexCameraWindow(QWidget* parent)
|
||||
m_SingleLensReflexCameraOperation->moveToThread(m_SLRCameraThread);
|
||||
m_SLRCameraThread->start();
|
||||
|
||||
// 基本连接
|
||||
connect(ui.openSLRCamera_btn, &QPushButton::clicked, this, &SingleLensReflexCameraWindow::openSLRCamera);
|
||||
connect(this, &SingleLensReflexCameraWindow::openSLRCameraSignal, m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::OpenSLRCamera);
|
||||
connect(ui.closeSLRCamera_btn, &QPushButton::clicked, this, &SingleLensReflexCameraWindow::closeSLRCamera);
|
||||
connect(this, &SingleLensReflexCameraWindow::closeSLRCameraSignal, m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::CloseSLRCamera);
|
||||
|
||||
connect(this, &SingleLensReflexCameraWindow::openSLRCameraSignal, m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::OpenSLRCamera);
|
||||
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::CamOpenedSignal, this, &SingleLensReflexCameraWindow::onCamOpened);
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::CamClosedSignal, this, &SingleLensReflexCameraWindow::onCamClosed);
|
||||
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::PlotSignal, this, &SingleLensReflexCameraWindow::PlotSLRImageSignal);
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::CamClosedSignal, this, &SingleLensReflexCameraWindow::SLRCamClosedSignal);
|
||||
|
||||
connect(this->ui.dataFolderBtn, SIGNAL(clicked()), this, SLOT(onSelectDataFolder()));
|
||||
|
||||
// 新增信号连接
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::ImageCapturedSignal, this, &SingleLensReflexCameraWindow::onImageCaptured);
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::ErrorSignal, this, &SingleLensReflexCameraWindow::onError);
|
||||
|
||||
// 实时取景信号连接
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::LiveViewImageReady, this, &SingleLensReflexCameraWindow::LiveViewImageReady, Qt::QueuedConnection);
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::LiveViewStarted, this, &SingleLensReflexCameraWindow::LiveViewStarted);
|
||||
connect(m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::LiveViewStopped, this, &SingleLensReflexCameraWindow::LiveViewStopped);
|
||||
|
||||
// 拍照控制信号连接
|
||||
connect(this, &SingleLensReflexCameraWindow::startCaptureSignal, m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::startCapture);
|
||||
connect(this, &SingleLensReflexCameraWindow::stopCaptureSignal, m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::stopCapture);
|
||||
connect(this, &SingleLensReflexCameraWindow::captureOnceSignal, m_SingleLensReflexCameraOperation, &SingleLensReflexCameraOperation::captureOnce);
|
||||
|
||||
// 初始化按钮状态
|
||||
ui.closeSLRCamera_btn->setEnabled(false);
|
||||
|
||||
// 初始化数据保存路径显示(从 AppSettings 恢复或使用默认)
|
||||
ui.dataFolderLineEdit->setText(AppSettings::instance().slrDataFolder());
|
||||
}
|
||||
|
||||
SingleLensReflexCameraWindow::~SingleLensReflexCameraWindow()
|
||||
@ -49,6 +66,23 @@ SingleLensReflexCameraWindow::~SingleLensReflexCameraWindow()
|
||||
m_SingleLensReflexCameraOperation = nullptr;
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraWindow::onSelectDataFolder()
|
||||
{
|
||||
QString dir = QFileDialog::getExistingDirectory(this,
|
||||
QString::fromLocal8Bit("选择数据保存路径"),
|
||||
ui.dataFolderLineEdit->text());
|
||||
|
||||
if (!dir.isEmpty())
|
||||
{
|
||||
ui.dataFolderLineEdit->setText(dir);
|
||||
|
||||
m_SingleLensReflexCameraOperation->setSavePath(dir);
|
||||
|
||||
// 保存路径到 AppSettings
|
||||
AppSettings::instance().setSlrDataFolder(dir);
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraWindow::openSLRCamera()
|
||||
{
|
||||
if (!m_SingleLensReflexCameraOperation->getRecordStatus())
|
||||
@ -66,7 +100,7 @@ void SingleLensReflexCameraWindow::onCamOpened()
|
||||
|
||||
void SingleLensReflexCameraWindow::closeSLRCamera()
|
||||
{
|
||||
emit closeSLRCameraSignal();
|
||||
emit closeSLRCameraSignal();
|
||||
//m_SingleLensReflexCameraOperation->CloseSLRCamera();
|
||||
}
|
||||
|
||||
@ -75,6 +109,7 @@ void SingleLensReflexCameraWindow::onCamClosed()
|
||||
ui.openSLRCamera_btn->setEnabled(true);
|
||||
ui.closeSLRCamera_btn->setEnabled(false);
|
||||
ui.openSLRCamera_btn->setText(QString::fromLocal8Bit("打 开"));
|
||||
m_isCapturing = false;
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraWindow::onImageCaptured(const QString& filePath)
|
||||
@ -88,6 +123,29 @@ void SingleLensReflexCameraWindow::onError(const QString& errorMsg)
|
||||
QMessageBox::warning(this, QString::fromLocal8Bit("相机错误"), errorMsg);
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraWindow::onStartCapture()
|
||||
{
|
||||
if (!m_isCapturing)
|
||||
{
|
||||
m_isCapturing = true;
|
||||
emit startCaptureSignal();
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraWindow::onStopCapture()
|
||||
{
|
||||
if (m_isCapturing)
|
||||
{
|
||||
m_isCapturing = false;
|
||||
emit stopCaptureSignal();
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraWindow::onCaptureOnce()
|
||||
{
|
||||
emit captureOnceSignal();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------------
|
||||
// SingleLensReflexCameraOperation 实现
|
||||
//-------------------------------------------------------------------------------------------------------------------------------
|
||||
@ -99,11 +157,13 @@ SingleLensReflexCameraOperation::SingleLensReflexCameraOperation()
|
||||
m_camera = nullptr;
|
||||
m_isSDKInitialized = false;
|
||||
m_isSessionOpen = false;
|
||||
m_isLiveViewActive = false;
|
||||
m_captureTimer = nullptr;
|
||||
m_liveViewTimer = nullptr;
|
||||
m_imageCounter = 0;
|
||||
|
||||
// 设置保存路径(可以根据需要修改)
|
||||
m_savePath = QCoreApplication::applicationDirPath() + "/CapturedImages/";
|
||||
// 设置保存路径(从 AppSettings 获取,如果为空则使用默认的 CapturedImages 目录)
|
||||
m_savePath = AppSettings::instance().slrDataFolder();
|
||||
|
||||
// 创建保存目录
|
||||
QDir dir;
|
||||
@ -124,6 +184,27 @@ SingleLensReflexCameraOperation::~SingleLensReflexCameraOperation()
|
||||
g_cameraOperation = nullptr;
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::setSavePath(const QString& path)
|
||||
{
|
||||
m_savePath = path;
|
||||
if (!m_savePath.endsWith('/') && !m_savePath.endsWith('\\'))
|
||||
{
|
||||
m_savePath += '/';
|
||||
}
|
||||
|
||||
QDir dir;
|
||||
if (!dir.exists(m_savePath))
|
||||
{
|
||||
dir.mkpath(m_savePath);
|
||||
}
|
||||
}
|
||||
|
||||
QImage SingleLensReflexCameraOperation::getCurrentLiveViewImage()
|
||||
{
|
||||
QMutexLocker locker(&m_liveViewMutex);
|
||||
return m_liveViewImage.copy();
|
||||
}
|
||||
|
||||
EdsError SingleLensReflexCameraOperation::initializeSDK()
|
||||
{
|
||||
EdsError err = EdsInitializeSDK();
|
||||
@ -272,10 +353,165 @@ EdsError SingleLensReflexCameraOperation::setupSaveToHost()
|
||||
return EDS_ERR_OK;
|
||||
}
|
||||
|
||||
EdsError SingleLensReflexCameraOperation::startLiveView()
|
||||
{
|
||||
EdsError err = EDS_ERR_OK;
|
||||
|
||||
if (m_camera == nullptr || !m_isSessionOpen)
|
||||
{
|
||||
return EDS_ERR_DEVICE_NOT_FOUND;
|
||||
}
|
||||
|
||||
// 获取当前输出设备设置
|
||||
EdsUInt32 device = 0;
|
||||
err = EdsGetPropertyData(m_camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Failed to get Evf_OutputDevice, error: " << err << std::endl;
|
||||
// 某些相机可能不支持获取,继续尝试设置
|
||||
}
|
||||
|
||||
// 设置PC为实时取景输出设备
|
||||
device |= kEdsEvfOutputDevice_PC;
|
||||
err = EdsSetPropertyData(m_camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Failed to set Evf_OutputDevice, error: " << err << std::endl;
|
||||
return err;
|
||||
}
|
||||
|
||||
m_isLiveViewActive = true;
|
||||
std::cout << "Live view started" << std::endl;
|
||||
|
||||
emit LiveViewStarted();
|
||||
|
||||
return EDS_ERR_OK;
|
||||
}
|
||||
|
||||
EdsError SingleLensReflexCameraOperation::stopLiveView()
|
||||
{
|
||||
EdsError err = EDS_ERR_OK;
|
||||
|
||||
if (m_camera == nullptr || !m_isSessionOpen)
|
||||
{
|
||||
m_isLiveViewActive = false;
|
||||
return EDS_ERR_OK;
|
||||
}
|
||||
|
||||
// 获取当前输出设备设置
|
||||
EdsUInt32 device = 0;
|
||||
err = EdsGetPropertyData(m_camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Failed to get Evf_OutputDevice, error: " << err << std::endl;
|
||||
}
|
||||
|
||||
// 从输出设备中移除PC
|
||||
device &= ~kEdsEvfOutputDevice_PC;
|
||||
err = EdsSetPropertyData(m_camera, kEdsPropID_Evf_OutputDevice, 0, sizeof(device), &device);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Failed to clear Evf_OutputDevice, error: " << err << std::endl;
|
||||
}
|
||||
|
||||
m_isLiveViewActive = false;
|
||||
std::cout << "Live view stopped" << std::endl;
|
||||
|
||||
emit LiveViewStopped();
|
||||
|
||||
return EDS_ERR_OK;
|
||||
}
|
||||
|
||||
EdsError SingleLensReflexCameraOperation::downloadEvfImage()
|
||||
{
|
||||
EdsError err = EDS_ERR_OK;
|
||||
EdsStreamRef stream = nullptr;
|
||||
EdsEvfImageRef evfImage = nullptr;
|
||||
|
||||
if (m_camera == nullptr || !m_isLiveViewActive)
|
||||
{
|
||||
return EDS_ERR_DEVICE_NOT_FOUND;
|
||||
}
|
||||
|
||||
// 创建内存流
|
||||
err = EdsCreateMemoryStream(0, &stream);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Failed to create memory stream, error: " << err << std::endl;
|
||||
return err;
|
||||
}
|
||||
|
||||
// 创建实时取景图像引用
|
||||
err = EdsCreateEvfImageRef(stream, &evfImage);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Failed to create evf image ref, error: " << err << std::endl;
|
||||
EdsRelease(stream);
|
||||
return err;
|
||||
}
|
||||
|
||||
// 下载实时取景图像
|
||||
err = EdsDownloadEvfImage(m_camera, evfImage);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
// EDS_ERR_OBJECT_NOTREADY 是正常的,表示图像还没准备好
|
||||
if (err != EDS_ERR_OBJECT_NOTREADY)
|
||||
{
|
||||
std::cout << "Failed to download evf image, error: " << err << std::endl;
|
||||
}
|
||||
EdsRelease(evfImage);
|
||||
EdsRelease(stream);
|
||||
return err;
|
||||
}
|
||||
|
||||
// 获取流数据
|
||||
EdsVoid* pData = nullptr;
|
||||
EdsUInt64 length = 0;
|
||||
|
||||
err = EdsGetPointer(stream, &pData);
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
EdsRelease(evfImage);
|
||||
EdsRelease(stream);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = EdsGetLength(stream, &length);
|
||||
if (err != EDS_ERR_OK || length == 0)
|
||||
{
|
||||
EdsRelease(evfImage);
|
||||
EdsRelease(stream);
|
||||
return err;
|
||||
}
|
||||
|
||||
// 将JPEG数据转换为QImage
|
||||
QImage image;
|
||||
if (image.loadFromData(static_cast<const uchar*>(pData), static_cast<int>(length), "JPEG"))
|
||||
{
|
||||
QMutexLocker locker(&m_liveViewMutex);
|
||||
m_liveViewImage = image.copy();
|
||||
locker.unlock();
|
||||
|
||||
// 发送信号通知图像已准备好
|
||||
emit LiveViewImageReady(image);
|
||||
}
|
||||
|
||||
// 释放资源
|
||||
EdsRelease(evfImage);
|
||||
EdsRelease(stream);
|
||||
|
||||
return EDS_ERR_OK;
|
||||
}
|
||||
|
||||
EdsError SingleLensReflexCameraOperation::takePicture()
|
||||
{
|
||||
EdsError err = EDS_ERR_OK;
|
||||
|
||||
if (m_camera == nullptr || !m_isSessionOpen)
|
||||
{
|
||||
return EDS_ERR_DEVICE_NOT_FOUND;
|
||||
}
|
||||
|
||||
// 按下快门
|
||||
err = EdsSendCommand(m_camera, kEdsCameraCommand_PressShutterButton, kEdsCameraCommand_ShutterButton_Completely);
|
||||
if (err != EDS_ERR_OK)
|
||||
@ -459,16 +695,30 @@ void SingleLensReflexCameraOperation::OpenSLRCamera()
|
||||
return;
|
||||
}
|
||||
|
||||
// 启动实时取景
|
||||
err = startLiveView();
|
||||
if (err != EDS_ERR_OK)
|
||||
{
|
||||
std::cout << "Warning: Failed to start live view, error: " << err << std::endl;
|
||||
// 实时取景启动失败不是致命错误,继续执行
|
||||
}
|
||||
|
||||
record = true;
|
||||
|
||||
// 创建并启动定时器(1秒拍一张)
|
||||
// 创建实时取景定时器(约30fps)
|
||||
m_liveViewTimer = new QTimer(this);
|
||||
connect(m_liveViewTimer, &QTimer::timeout, this, &SingleLensReflexCameraOperation::onLiveViewTimer);
|
||||
m_liveViewTimer->start(33); // 约30fps
|
||||
|
||||
// 创建拍照定时器(1秒拍一张),但不立即启动
|
||||
m_captureTimer = new QTimer(this);
|
||||
connect(m_captureTimer, &QTimer::timeout, this, &SingleLensReflexCameraOperation::onCaptureTimer);
|
||||
m_captureTimer->start(2000); // 1000毫秒 = 1秒
|
||||
// 默认启动定时拍照
|
||||
m_captureTimer->start(3000); // 1000毫秒 = 1秒
|
||||
|
||||
emit CamOpenedSignal();
|
||||
|
||||
std::cout << "Camera opened and timer started (1 photo per second)" << std::endl;
|
||||
std::cout << "Camera opened, live view started, capture timer started (1 photo per second)" << std::endl;
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::OpenSLRCamera_callback()
|
||||
@ -481,6 +731,20 @@ void SingleLensReflexCameraOperation::setCallback(void(*func)())
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::onLiveViewTimer()
|
||||
{
|
||||
if (!record || !m_isLiveViewActive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理相机事件
|
||||
EdsGetEvent();
|
||||
|
||||
// 下载实时取景图像
|
||||
downloadEvfImage();
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::onCaptureTimer()
|
||||
{
|
||||
if (!record)
|
||||
@ -490,9 +754,6 @@ void SingleLensReflexCameraOperation::onCaptureTimer()
|
||||
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
// 处理相机事件(重要:需要定期调用以接收回调)
|
||||
EdsGetEvent();
|
||||
|
||||
// 拍照
|
||||
EdsError err = takePicture();
|
||||
if (err != EDS_ERR_OK)
|
||||
@ -509,6 +770,33 @@ void SingleLensReflexCameraOperation::onCaptureTimer()
|
||||
std::cout << "Total pictures taken: " << m_imageCounter << std::endl;
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::startCapture()
|
||||
{
|
||||
if (m_captureTimer && !m_captureTimer->isActive())
|
||||
{
|
||||
m_captureTimer->start(1000);
|
||||
std::cout << "Capture timer started" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::stopCapture()
|
||||
{
|
||||
if (m_captureTimer && m_captureTimer->isActive())
|
||||
{
|
||||
m_captureTimer->stop();
|
||||
std::cout << "Capture timer stopped" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::captureOnce()
|
||||
{
|
||||
if (record)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
takePicture();
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLensReflexCameraOperation::CloseSLRCamera()
|
||||
{
|
||||
std::cout << "SingleLensReflexCameraOperation::CloseSLRCamera,关闭单反相机" << std::endl;
|
||||
@ -517,7 +805,7 @@ void SingleLensReflexCameraOperation::CloseSLRCamera()
|
||||
|
||||
record = false;
|
||||
|
||||
// 停止定时器
|
||||
// 停止拍照定时器
|
||||
if (m_captureTimer != nullptr)
|
||||
{
|
||||
m_captureTimer->stop();
|
||||
@ -525,6 +813,17 @@ void SingleLensReflexCameraOperation::CloseSLRCamera()
|
||||
m_captureTimer = nullptr;
|
||||
}
|
||||
|
||||
// 停止实时取景定时器
|
||||
if (m_liveViewTimer != nullptr)
|
||||
{
|
||||
m_liveViewTimer->stop();
|
||||
delete m_liveViewTimer;
|
||||
m_liveViewTimer = nullptr;
|
||||
}
|
||||
|
||||
// 停止实时取景
|
||||
stopLiveView();
|
||||
|
||||
// 处理剩余事件
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
@ -542,4 +841,4 @@ void SingleLensReflexCameraOperation::CloseSLRCamera()
|
||||
m_imageCounter = 0;
|
||||
|
||||
emit CamClosedSignal();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user