QT相机连接与拍照

先看效果

初始化

复制代码
    auto mainLayout = new QHBoxLayout(this);
    m_viewfinder = new QCameraViewfinder(this);
    m_viewfinder->setStyleSheet("border-radius: 20px;background-color:rgb(43,48,70)");
    mainLayout->addWidget(m_viewfinder,8);

选择相机

复制代码
void cameraeidget::selectDevice(const QCameraInfo &cameraInfo)
{
    bool restart = isStarted();
    if (isStarted()) {
        stop();
    }

    if (m_camera) {
        delete m_camera;
    }

    m_curCameraInfo = cameraInfo;
    m_camera = new QCamera(m_curCameraInfo);
    m_camera->setViewfinder(m_viewfinder);

    if (restart)//首次启动没有重连
        start();

}

启动相机

复制代码
void cameraeidget::start()
{
    if (! m_isStarted) {
        m_isStarted = true;

        m_camera->start();

        // QList<QCameraInfo> curCameraInfoList = QCameraInfo::availableCameras();
        // for (int i = 0; i < curCameraInfoList.count(); i++) {
        //     qDebug() << "连接相机数量:"<< curCameraInfoList.count();
        //     qDebug() << "相机名称:"<< curCameraInfoList.at(i).deviceName();
        // }
        imageCapture = new QCameraImageCapture(m_camera);
        const QStringList supportedImageCodecs = imageCapture->supportedImageCodecs();
        imageCodecBox->clear();
        for (const QString &codecName : supportedImageCodecs) {
            QString description = imageCapture->imageCodecDescription(codecName);
            imageCodecBox->addItem(codecName , QVariant(codecName));
             // qDebug() << "支持编码格式:"<< QVariant(codecName)<<"description"<<description;
        }
        imageCodecBox->setCurrentIndex(5);

        QCameraViewfinderSettings set;
        set.setResolution(1920,1080);
        m_camera->setViewfinderSettings(set);
        qDebug() <<m_camera->status();
        // m_camera->setCaptureMode(QCamera::CaptureStillImage);

        imageResolutionBox->clear();
        const QList<QSize> supportedResolutions = imageCapture->supportedResolutions();
        for (const QSize &resolution : supportedResolutions) {
            imageResolutionBox->addItem(QString("%1x%2").arg(resolution.width()).arg(resolution.height()),
                                            QVariant(resolution));
            // qDebug() << "支持分辨率:"<<QString("%1x%2").arg(resolution.width()).arg(resolution.height())<<endl;
        }
        imageResolutionBox->setCurrentIndex(2);

    }


    QTimer::singleShot(2 * 1000,this,[=]{
        emit send_status(m_isStarted);
    });

}

分辨率设置

复制代码
connect(imageResolutionBox,&QComboBox::currentTextChanged,this,[ = ]{
        QCameraViewfinderSettings set;
        QSize fbl = imageResolutionBox->itemData(imageResolutionBox->currentIndex()).toSize();
        set.setResolution(fbl);
        m_camera->setViewfinderSettings(set);
    });

照片格式

复制代码
 connect(imageCodecBox,&QComboBox::currentTextChanged,this,[ = ]{

        QImageEncoderSettings settings = imageCapture->encodingSettings();
        settings.setCodec(imageCodecBox->currentText());
        imageCapture->setEncodingSettings(settings);
    });

下载路径

复制代码
connect(m_filelocation_button, &QPushButton::clicked, this, [ = ] {
        QDir dir= QCoreApplication::applicationDirPath();
        dir.cdUp();
        localPath = dir.path();
        localPath = QFileDialog::getExistingDirectory(this, tr("选择下载路径"),
                                                     localPath,
                                                     QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);
        lineedit2->setText(localPath);
    });

源码:GitHub - houyawei-NO1/ND-Camera: 南都拍照系统

后续想添加一个二维码识别的库。

二维码太小手机会自动放大扫,不知道哪个库也支持,直接扫肯定扫不上的。

相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty2 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再2 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame