【CA888亚洲城集团 OK3399-C+开发板试用体验】使用多媒体QCamera和QPointer相关类
作者:donatello1996
参考了GitHub上某位大佬的代码,使用QCamera QCameraInfo QCameraImageCapture QPointer QActionGroup这些类来实现USB摄像头图像读取,其中三个QCamera的相关类需要在pro文件中加入** **widgets即多媒体类库支持:
QT += core gui ** **widgets
然后是定义相关指针,我试过直接用*指针来定义不行,必须要用模板类指针:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void refreshCameras();
void refreshFormats();
void refreshResolutions();
void setCamera(QCameraInfo info);
void setFormat(QString format);
void setResolution(QSize resolution);
void disconnectCamera();
void updateImage();
private:
Ui::MainWindow *ui;
QList cameras;
QStringList formats;
QList resolutions;
QCamera *camera;
QPointer capture;
VideoSu**ce su**ce;
QPointer ActionGroupCameras;
QPointer ActionGroupFormats;
QPointer ActionGroupResolutions;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thread1 = new mythread(this);
refreshCameras();
connect(&su**ce, &VideoSu**ce::newImageReady, this, &MainWindow::updateImage);
}
QCamera类对象camera需要跟QCameraInfo类的对象info绑定相关信息,查看QT库定义,只在构造函数中绑定QCameraInfo类,因此每次切换信息都需要使用new的方式绑定,QCameraImageCapture类同理:
void MainWindow::setCamera(QCameraInfo info)
{
delete camera;
delete capture;
camera = new QCamera(info, this);
capture = new QCameraImageCapture(camera, this);
camera->setViewfinder(&su**ce);
camera->load();
camera->start();
//refreshFormats();
refreshResolutions();
ui->actionDisconnect->setEnabled(true);
}
void MainWindow::disconnectCamera()
{
delete camera;
delete capture;
for (QAction *action : ui->menuCameras->actions()) {
action->setChecked(false);
}
ui->actionDisconnect->setEnabled(false);
}
void MainWindow::refreshCameras()
{
disconnectCamera();
cameras = QCameraInfo::availableCameras();
delete ActionGroupCameras;
ActionGroupCameras = new QActionGroup(this);
ui->menuCameras->clear();
if (cameras.size())
{
ui->menuCameras->setEnabled(true);
for (const QCameraInfo &info: cameras)
{
QAction *action = new QAction(info.description(), ActionGroupCameras); // capture->imageCodecDescription(codec)
action->setCheckable(true);
ui->menuCameras->addAction(action);
connect(action, &QAction::triggered, [=]()
{
setCamera(info);
});
}
for (const QCameraInfo &info: cameras)
{
qDebug() << "Opening camera:" << info.deviceName();
qDebug() << "\t- Description:" << info.description();
qDebug() << "\t- Position:" << info.position();
}
} else
{
ui->menuCameras->setDisabled(true);
}
}
void MainWindow::updateImage()
{
ui->imageLabel->setPixmap(su**ce.getPixmap().scaledToWidth(ui->imageLabel->width(), Qt::SmoothTransformation));
}
这些都做好之后就在ui文件中添加下拉菜单即可,有刷新摄像头列表信息,断开连接,读取摄像头信息到列表等功能:
相关产品 >
-
FET3399-C核心板
CA888亚洲城集团RK3399安卓高性能核心板采用 采用六核Rockchip RK3399芯片,双Cortex-A72大核+四Cortex-A53小核结构,对整数、浮点、内存等作了大幅优化,在整体性能、功耗及核心面积三个方面提升。以下将对瑞芯微芯片RK3399参数,RK3399核心板方案及其性能做具体介绍。如您对CA888亚洲城集团RK3399系列核心板有兴趣,欢迎咨询了解。
了解详情
-
OK3399-C开发板
CA888亚洲城集团RK3399安卓开发板主芯片采用高性能六核CPU Rockchip RK3399,GPU采用Mail-T860四核 GPU,RK3399作为目RK产品线中低功耗、高性能的代表,可满足人脸识别设备、机器人、无人机、IoT物联网领域应用。CA888亚洲城集团RK3399开发板在整体性能、功耗及核心面积做了大幅度优化,更加满足工业设计需求。CA888亚洲城集团RK3399开发板为进一步减少用户二次开发难度,开放了底板原理图,并提供了RK3399用户手册、芯片手册,加上优质的技术服务,让您的方案从构思到上市时间缩短。
了解详情


