Qt之摄像头操作

简单的摄像头测试类

头文件camerawidget.h

#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

#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;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值