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;
}
相关推荐
长河21 分钟前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
Cyan_RA931 分钟前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
再见晴天*_*3 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun08275 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge7 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘7 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务8 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程8 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther8 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_9 小时前
C++:类和对象(下)
开发语言·c++