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

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

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

相关推荐
太阳的后裔21 小时前
随笔一些用C#封装的控件
开发语言·c#
tianyuanwo21 小时前
Rust语言组件RPM包编译原理与Cargo工具详解
开发语言·网络·rust·rpm
ajassi20001 天前
开源 C++ QT Widget 开发(十五)多媒体--音频播放
linux·c++·qt·开源
CodeCraft Studio1 天前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一1 天前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
lingchen19061 天前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
gb42152871 天前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
一朵梨花压海棠go1 天前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript
蒋星熠1 天前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
翻滚丷大头鱼1 天前
Java 集合Collection—List
java·开发语言