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

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

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

上一篇:基于JavaSpringmvc+myabtis+html的鲜花商城系统设计和实现


下一篇:研究深度神经网络优化稳定性,证明在一定条件下梯度下降和随机梯度下降方法能有效控制损失函数