- 积分
- 64
贡献394
飞刀124 FD
注册时间2018-10-24
在线时间19 小时
扫一扫,手机访问本帖
|
【飞凌嵌入式 OK3399-C+开发板试用体验】使用多媒体QCamera和QPointer相关类实现USB摄像头读取
参考了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<QCameraInfo> cameras;
- QStringList formats;
- QList<QSize> resolutions;
- <font color="Blue">QCamera *camera;
- QPointer<QCameraImageCapture> capture;</font>
- VideoSu**ce su**ce;
- QPointer<QActionGroup> ActionGroupCameras;
- QPointer<QActionGroup> ActionGroupFormats;
- QPointer<QActionGroup> ActionGroupResolutions;
- };
复制代码
VideoSu**ce类对象su**ce是自行编写的类,不需要使用指针指定,可以用实体类对象:
- 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);
- }
复制代码
与摄像头断开连接时将QCamera类对象和QCameraImageCapture类资源回收:
- 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);
- }
- }
复制代码
最后就是更新Label标签显示图像的槽函数了:
- void MainWindow::updateImage()
- {
- ui->imageLabel->setPixmap(su**ce.getPixmap().scaledToWidth(ui->imageLabel->width(), Qt::SmoothTransformation));
- }
复制代码
这些都做好之后就在ui文件中添加下拉菜单即可,有刷新摄像头列表信息,断开连接,读取摄像头信息到列表等功能:
查看运行效果:
|
|