飞凌嵌入式
直播中

fsdzdzy

10年用户 260经验值
擅长:嵌入式技术 控制/MCU opencv
私信 关注
[技术]

【飞凌RK3568开发板试用体验】7-Qt摄像头程序精简与分析

OK3568-C板子中自带了一个摄像头的例程,可以实现拍照和录像功能,这个程序可基于Qt开发,为了便于理解Qt读取USB摄像头图像的流程,我对程序进行了精简,只保留读取USB摄像头并将图像流展示出来的功能。

1 主要代码分析

将代码精简后,可以知道Qt读取并显示USB摄像头,需要3个基本元素:

  • QCamera:它是用于读取摄像头视频信号的接口函数
  • QCameraInfo:它提供相机设备的常规信息,可以用来查询系统上当前可用的相机设备
  • QCameraViewfinder:它提供了一个相机取景器的小部,该类继承于QVideoWidget类,用于显示多媒体类提供的视频

1.1 查找USB相机

//可用相机列表
const QList<QCameraInfo> availableCameras = QCameraInfo::availableCameras();
for (const QCameraInfo &cameraInfo : availableCameras)
{
    qDebug() << cameraInfo.description();
    if (cameraInfo.description().contains("USB", Qt::CaseSensitive))
    {
        //USB摄像头
        QPushButton *camera = new QPushButton;
        camera->setText(cameraInfo.description());
        camera->setFont(font);
        camera->setCheckable(true);
        if (cameraInfo == QCameraInfo::defaultCamera())
        {
            camera->setDefault(true);
        }
        else
        {
            camera->setDefault(false);
        }
​
        //启动相机
        connect(camera, SIGNAL(clicked(bool)), this, SLOT(on_cameraClick()));
        vLayout->addWidget(camera);
​
        m_cameraInfo = cameraInfo;
        break;
    }
}

1.2 启动相机与显示

//创建摄像头对象
m_camera = new QCamera(m_cameraInfo);
​
//创建取景器
m_viewfinder = new QCameraViewfinder();
​
//配置摄像头的模式--捕获静止图像
QCamera::CaptureModes captureMode = QCamera::CaptureStillImage;
if (m_camera->isCaptureModeSupported(captureMode))
{
    m_camera->unload();
    m_camera->setCaptureMode(captureMode);
    //设置取景器显示
    m_camera->setViewfinder(m_viewfinder);
    //启动摄像头
    m_camera->start();
}

2 精简后的完整代码

qtcamera.h精简后的代码:

#ifndef QTCAMERA_H
#define QTCAMERA_H#include <QCamera>
#include <QCameraInfo>
#include <QCameraViewfinder>
#include <QMainWindow>class qtCamera : public QMainWindow
{
    Q_OBJECT
​
public:
    qtCamera();
​
private slots:
    void on_cameraClick();
    void on_exitClicked();
​
private:
    QCamera *m_camera;
    QCameraInfo m_cameraInfo;
    QCameraViewfinder *m_viewfinder;
};
​
#endif

qtcamera.cpp精简后的代码:

#include "qtcamera.h"
#include <QApplication>
#include <QtWidgets>
#include <QHBoxLayout>
#include <QVBoxLayout>#define FONT_SIZE 20
​
qtCamera::qtCamera()
{
    QBoxLayout *vLayout = new QVBoxLayout();
    const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
    QFont font;
    font.setPixelSize(FONT_SIZE);
    resize(availableGeometry.width(), availableGeometry.height());
​
    //可用相机列表
    const QList<QCameraInfo> availableCameras = QCameraInfo::availableCameras();
    for (const QCameraInfo &cameraInfo : availableCameras)
    {
        qDebug() << cameraInfo.description();
        if (cameraInfo.description().contains("USB", Qt::CaseSensitive) || cameraInfo.description().contains("UVC", Qt::CaseSensitive))
        {
            //USB摄像头
            QPushButton *camera = new QPushButton;
            camera->setText(cameraInfo.description());
            camera->setFont(font);
            camera->setCheckable(true);
            if (cameraInfo == QCameraInfo::defaultCamera())
            {
                camera->setDefault(true);
            }
            else
            {
                camera->setDefault(false);
            }
​
            //启动相机
            connect(camera, SIGNAL(clicked(bool)), this, SLOT(on_cameraClick()));
            vLayout->addWidget(camera);
​
            m_cameraInfo = cameraInfo;
            break;
        }
    }
​
    //退出按钮
    QPushButton *exitButton = new QPushButton;
    exitButton->setText(tr("Exit"));
    exitButton->setFont(font);
    connect(exitButton, SIGNAL(clicked(bool)), this, SLOT(on_exitClicked()));
​
    //按钮垂直布局
    vLayout->addWidget(exitButton);
    vLayout->setAlignment(Qt::AlignTop);
​
    //创建取景器
    m_viewfinder = new QCameraViewfinder();
​
    //整体水平布局
    QBoxLayout *hlayout = new QHBoxLayout;
    hlayout->setMargin(0);
    hlayout->addWidget(m_viewfinder);
    hlayout->addLayout(vLayout);
    hlayout->setStretch(0,10);
    hlayout->setStretch(1,1);
    hlayout->setContentsMargins(10, 10, 10, 10);
​
    QWidget *widget = new QWidget;
    widget->setLayout(hlayout);
    setCentralWidget(widget);
    setWindowFlags(Qt::FramelessWindowHint);
}
​
//切换相机
void qtCamera::on_cameraClick()
{
    //创建摄像头对象
    m_camera = new QCamera(m_cameraInfo);
​
    //配置摄像头的模式--捕获静止图像
    QCamera::CaptureModes captureMode = QCamera::CaptureStillImage;
    if (m_camera->isCaptureModeSupported(captureMode))
    {
        m_camera->unload();
        m_camera->setCaptureMode(captureMode);
        //设置取景器显示
        m_camera->setViewfinder(m_viewfinder);
        //启动摄像头
        m_camera->start();
    }
}
​
void qtCamera::on_exitClicked()
{
    qApp->exit(0);
}

3 精简后代码的运行效果

在OK3568-C板子中的实际运行效果如下,我给板子外接了一个USB摄像头,程序识别的名称为“LiteOS:UVC CAMERA”

打印信息中,可用看到列举了多个摄像头信息,程序里只挑选含义USB或UVC字样的,判断为USB类型的摄像头

4 总结

本篇对板子资料中的Qt摄像头例程进行精简,只保留USB摄像头显示功能,并对程序功能进行了分析,最后进行了实际测试。

更多回帖

发帖
×
20
完善资料,
赚取积分