Qt之摄像头操作

简单的摄像头测试类

头文件camerawidget.h

cpp 复制代码
#ifndef CAMERAWIDGET_H
#define CAMERAWIDGET_H

#include <QWidget>
#include <QList>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QCameraViewfinderSettings>
#include <QVBoxLayout> // Include the QVBoxLayout header
#include <QCameraImageCapture>

QT_BEGIN_NAMESPACE
namespace Ui { class CameraWidget; }
QT_END_NAMESPACE

class CameraWidget : public QWidget
{
    Q_OBJECT

public:
    explicit CameraWidget(QWidget *parent = nullptr);
    ~CameraWidget();

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();

private:
    Ui::CameraWidget *ui;
    QCamera* myCamera;
    QCameraViewfinder* vf;
    QCameraImageCapture *imageCapture;

    void openDevice();
    void closeDevice();


};
#endif // CAMERAWIDGET_H

源文件camerawidget.c

cpp 复制代码
#include "camerawidget.h"
#include "ui_camerawidget.h"
#include <QGraphicsItem>
CameraWidget::CameraWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CameraWidget)
{
    ui->setupUi(this);
    ui->label->setText("摄像头类型");

    myCamera = nullptr;
    imageCapture = nullptr; // 初始化图像捕获对象
      QString str ;
    // 查找所有可用的摄像头信息
       QList<QCameraInfo> infos = QCameraInfo::availableCameras();
    foreach(QCameraInfo info, infos){
        qDebug() << info.description() << info.deviceName();
        // Check and set orientation property if available
        if(info.deviceName()=="HLC3086-RGB"){
            qDebug()<<info.deviceName()+"测试";
                        QVariantMap properties = myCamera->property("properties").toMap();
                        if (properties.contains("orientation")) {
                            myCamera->setProperty("orientation", properties.value("orientation"));
                        }
        }
        ui->comboBox->addItem(info.deviceName());
    }
    setFixedSize(800,700);
    setWindowTitle("摄像头测试");
    // Create a layout and set it as the main layout for the widget
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    ui->frame->setLayout(mainLayout);
}
void CameraWidget::on_pushButton_clicked()
{
    closeDevice();
    openDevice();
}

void CameraWidget::on_pushButton_2_clicked()
{
    closeDevice();
}

void CameraWidget::openDevice(){
    if (myCamera == nullptr) {
        myCamera = new QCamera(ui->comboBox->currentText().toUtf8(), this);

        vf = new QCameraViewfinder(this);
        vf->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

        qDebug()<<ui->comboBox->currentText().toUtf8();


        myCamera->setViewfinder(vf);

        // Add the viewfinder to the layout
        ui->frame->layout()->addWidget(vf);

        myCamera->start();
    }
}

void CameraWidget::closeDevice(){
    if (myCamera != nullptr) {
        myCamera->stop();
        delete myCamera; // Clean up the camera
        myCamera = nullptr;

        // Remove the viewfinder from the layout
        ui->frame->layout()->removeWidget(vf);
        delete vf; // Clean up the viewfinder
        vf = nullptr;
    }
}


CameraWidget::~CameraWidget()
{
    delete ui;
}
相关推荐
iCxhust3 小时前
c# U盘映像生成工具
开发语言·单片机·c#
yangzhi_emo4 小时前
ES6笔记2
开发语言·前端·javascript
emplace_back5 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk5 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶5 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl5 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
收破烂的小熊猫~5 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
nananaij6 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
阿蒙Amon6 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#