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 小时前
QT-文件创建时间修改器
c++·qt
不知所云,1 小时前
qt cmake自定义资源目录,手动加载资源(图片, qss文件)
开发语言·qt
安冬的码畜日常1 小时前
【玩转 JS 函数式编程_006】2.2 小试牛刀:用函数式编程(FP)实现事件只触发一次
开发语言·前端·javascript·函数式编程·tdd·fp·jasmine
阑梦清川1 小时前
Java继承、final/protected说明、super/this辨析
java·开发语言
PythonFun2 小时前
Python批量下载PPT模块并实现自动解压
开发语言·python·powerpoint
Death2002 小时前
Qt 6 相比 Qt 5 的主要提升与更新
开发语言·c++·qt·交互·数据可视化
机器视觉知识推荐、就业指导2 小时前
使用Qt实现实时数据动态绘制的折线图示例
开发语言·qt
快乐就好ya3 小时前
Java多线程
java·开发语言
CS_GaoMing3 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本
2401_858120534 小时前
Spring Boot框架下的大学生就业招聘平台
java·开发语言