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;
}
相关推荐
还债大湿兄1 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang4 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc4 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇5 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle5 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程