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: 南都拍照系统

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

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

相关推荐
Ustinian_31033 分钟前
【C/C++】For 循环展开与性能优化【附代码讲解】
c语言·开发语言·c++
牵牛老人43 分钟前
Qt 插件开发全解析:从接口定义,插件封装,插件调用到插件间的通信
开发语言·qt
钮钴禄·爱因斯晨1 小时前
AIGC浪潮下,风靡全球的Mcp到底是什么?一文讲懂,技术小白都知道!!
开发语言·人工智能·深度学习·神经网络·生成对抗网络·aigc
22jimmy2 小时前
JavaWeb(二)CSS
java·开发语言·前端·css·入门·基础
机器视觉知识推荐、就业指导4 小时前
面试问题详解五:Qt 信号与槽的动态管理
开发语言·qt
四维碎片10 小时前
【Qt】线程池与全局信号实现异步协作
开发语言·qt·ui·visual studio
IT码农-爱吃辣条10 小时前
Three.js 初级教程大全
开发语言·javascript·three.js
☺����11 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
染翰11 小时前
lua入门以及在Redis中的应用
开发语言·redis·lua
王者鳜錸11 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化