各模块的基本实现——1. 在Qt界面上显示树莓派摄像头捕获到的图像
一些废话
我——一个很久之前做过嵌入式程序开发现在逐渐变为MATLAB玩家的小菜鸡。
前一段时间发现竟然连全局变量怎么声明都快忘记了,瞬间陷入焦躁不安忧虑难过中…
遂赶紧开始码代码并刷一波C++语法书安慰一下自己。
一个小功能实现之后,感觉回了一点血,记忆仿佛又回来了ヾ(◍°∇°◍)ノ゙~
这篇文章用一个小demo实现摄像头模块的驱动,即在Qt界面上显示树莓派摄像头捕获到的图像,具备一些基本的人机交互功能。
因为在第一章中我们已经完成了交叉编译环境的配置,后面的程序开发均是在PC端的Ubuntu18.04环境下,并将可执行文件放在树莓派上执行。
一、思路
demo的功能和实现的基本思路见下图。
二、代码
Show You My Code~主要是3个文件mainwindow.h,main.cpp和mainwindow.cpp
mainwindow.ui中的操作比较简单,不再赘述。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QObject>
#include <QToolBar>
#include <QAction>
#include <QTimer>
#include <opencv2/opencv.hpp>
#include <iostream>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QToolBar *cameraTool;
QAction *actStartCamera;
QAction *actStopCamera;
QAction *actImageCapture;
cv::VideoCapture g_cap;
QTimer *timer;
cv::Mat frame;
QImage frameCap;
bool frameCaptureFlag = false;
void initTimer();
void initCamera();
void createToolBars();
void createActions();
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void slot_actStartCamera_triggered();
void slot_actStopCamera_triggered();
void slot_actCapture_triggered();
void slot_timer_timeout(