Qt操作Windows平板上摄像头
方案一:使用QCamera
项目配置(pro文件)
qmake
QT += multimedia multimediawidgets# 如果需要使用QML
QT += qml quick# Windows平台可能需要
win32: LIBS += -lole32
1. 完整摄像头控制类
cpp
#include <QApplication> #include <QCamera> #include <QCameraInfo> #include <QCameraViewfinder> #include <QCameraImageCapture> #include <QMediaRecorder> #include <QCameraFocus> #include <QCameraExposure> #include <QCameraFlashControl> #include <QComboBox> #include <QSlider> #include <QCheckBox> #include <QGroupBox> #include <QVBoxLayout> #include <QHBoxLayout> #include <QPushButton> #include <QLabel> #include <QWidget>class AdvancedCameraWindow : public QWidget {Q_OBJECTpublic:AdvancedCameraWindow(QWidget *parent = nullptr) : QWidget(parent){setupUI();refreshCameraList();setupConnections();}private slots:void refreshCameraList(){m_cameraCombo->clear();QList<QCameraInfo> cameras = QCameraInfo::availableCameras();for (const QCameraInfo &cameraInfo : cameras) {QString cameraName = cameraInfo.description();// 判断摄像头类型if (cameraInfo.position() == QCamera::FrontFace) {cameraName += " (前置)";} else if (cameraInfo.position() == QCamera::BackFace) {cameraName += " (后置)";}m_cameraCombo->addItem(cameraName, QVariant::fromValue(cameraInfo));}}void switchCamera(int index){if (m_camera && m_camera->status() == QCamera::ActiveStatus) {m_camera->stop();}if (index >= 0) {QCameraInfo cameraInfo = m_cameraCombo->itemData(index).value<QCameraInfo>();m_camera = new QCamera(cameraInfo, this);setupCamera();m_camera->start();updateCameraCapabilities();}}void toggleTorch(bool enabled){if (!m_camera || !m_flashControl) return;if (enabled) {m_flashControl->setFlashMode(QCameraExposure::FlashTorch);} else {m_flashControl->setFlashMode(QCameraExposure::FlashOff);}}void toggleCamera(bool start){if (start) {if (m_camera) {m_camera->start();}} else {if (m_camera) {m_camera->stop();}}}void setZoom(int value){if (m_focus) {m_focus->zoomTo(value, value);}}void setExposureCompensation(int value){if (m_exposure) {m_exposure->setExposureCompensation(value / 10.0);}}void captureImage(){if (m_imageCapture && m_imageCapture->isReadyForCapture()) {QString filename = QString("capture_%1.jpg").arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"));m_imageCapture->capture(filename);}}void imageCaptured(int id, const QImage &preview){Q_UNUSED(id)preview.save("latest_capture.jpg");}private:void setupUI(){QVBoxLayout *mainLayout = new QVBoxLayout(this);// 摄像头选择区域QGroupBox *cameraGroup = new QGroupBox("摄像头设置", this);QHBoxLayout *cameraLayout = new QHBoxLayout(cameraGroup);cameraLayout->addWidget(new QLabel("选择摄像头:"));m_cameraCombo = new QComboBox(this);cameraLayout->addWidget(m_cameraCombo);QPushButton *refreshBtn = new QPushButton("刷新列表", this);cameraLayout->addWidget(refreshBtn);mainLayout->addWidget(cameraGroup);// 视频显示区域m_viewfinder = new QCameraViewfinder(this);m_viewfinder->setMinimumSize(640, 480);mainLayout->addWidget(m_viewfinder);// 控制按钮区域QGroupBox *controlGroup = new QGroupBox("摄像头控制", this);QGridLayout *controlLayout = new QGridLayout(controlGroup);// 闪光灯控制m_torchCheck = new QCheckBox("开启闪光灯/手电筒", this);controlLayout->addWidget(m_torchCheck, 0, 0, 1, 2);// 变焦控制controlLayout->addWidget(new QLabel("变焦:"), 1, 0);m_zoomSlider = new QSlider(Qt::Horizontal, this);m_zoomSlider->setRange(1, 10);m_zoomSlider->setValue(1);controlLayout->addWidget(m_zoomSlider, 1, 1);// 曝光补偿controlLayout->addWidget(new QLabel("曝光补偿:"), 2, 0);m_exposureSlider = new QSlider(Qt::Horizontal, this);m_exposureSlider->setRange(-20, 20);m_exposureSlider->setValue(0);controlLayout->addWidget(m_exposureSlider, 2, 1);mainLayout->addWidget(controlGroup);// 操作按钮QHBoxLayout *buttonLayout = new QHBoxLayout();m_startBtn = new QPushButton("开始", this);m_stopBtn = new QPushButton("停止", this);m_captureBtn = new QPushButton("拍照", this);buttonLayout->addWidget(m_startBtn);buttonLayout->addWidget(m_stopBtn);buttonLayout->addWidget(m_captureBtn);mainLayout->addLayout(buttonLayout);}void setupConnections(){connect(m_cameraCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),this, &AdvancedCameraWindow::switchCamera);connect(m_torchCheck, &QCheckBox::toggled,this, &AdvancedCameraWindow::toggleTorch);connect(m_startBtn, &QPushButton::clicked,this, [this]() { toggleCamera(true); });connect(m_stopBtn, &QPushButton::clicked,this, [this]() { toggleCamera(false); });connect(m_captureBtn, &QPushButton::clicked,this, &AdvancedCameraWindow::captureImage);connect(m_zoomSlider, &QSlider::valueChanged,this, &AdvancedCameraWindow::setZoom);connect(m_exposureSlider, &QSlider::valueChanged,this, &AdvancedCameraWindow::setExposureCompensation);}void setupCamera(){if (!m_camera) return;m_camera->setViewfinder(m_viewfinder);// 设置图像捕获m_imageCapture = new QCameraImageCapture(m_camera, this);m_camera->setCaptureMode(QCamera::CaptureStillImage);connect(m_imageCapture, &QCameraImageCapture::imageCaptured,this, &AdvancedCameraWindow::imageCaptured);// 获取控制接口m_focus = m_camera->focus();m_exposure = m_camera->exposure();m_flashControl = m_camera->exposure(); // Flash control is part of exposure}void updateCameraCapabilities(){// 更新UI以反映当前摄像头的功能if (m_exposure) {bool hasTorch = m_exposure->isFlashModeSupported(QCameraExposure::FlashTorch);m_torchCheck->setEnabled(hasTorch);m_torchCheck->setChecked(false);}if (m_focus) {bool canZoom = m_focus->isAvailable() && m_focus->isZoomModeSuppo