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))
{
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精简后的代码:
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))
{
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摄像头显示功能,并对程序功能进行了分析,最后进行了实际测试。