#include "monitor.h"
#include "ui_monitor.h"
extern "C"
{
#include <stdio.h>
#include <stdlib.h>
}
Monitor::Monitor(QWidget *parent) : QWidget(parent)
{
//创建文件夹
QDir dir;
for (long j = 0; j < MAX_VIDEO; j++)
{
dir.mkdir("./pic" + QString::number(j));
}
//初始化标志位和计数变量
record_flag = false;
play_flag = false;
video_flag = false;
x2_flag = false;
video_count = 0;
pic_count = 0;
i = 0;
//动态生成按钮控件并绑定信号和槽
rec = new QPushButton(this);
rec->show();
rec->move(370, 25);
rec->resize(70, 25);
rec->setText(tr("Record"));
connect(rec, SIGNAL(clicked()), this, SLOT(rec_clicked()));
end = new QPushButton(this);
end->show();
end->move(370, 85);
end->resize(70, 25);
end->setText(tr("End"));
connect(end, SIGNAL(clicked()), this, SLOT(end_clicked()));
video = new QPushButton(this);
video->show();
video->move(370, 145);
video->resize(70, 25);
video->setText(tr("Video"));
connect(video, SIGNAL(clicked()), this, SLOT(video_clicked()));
back = new QPushButton(this);
back->show();
back->move(370, 205);
back->resize(70, 25);
back->setText(tr("Back"));
connect(back, SIGNAL(clicked()), this, SLOT(back_clicked()));
play = new QPushButton(this);
play->hide();
play->move(335, 25);
play->resize(70, 25);
play->setText(tr("Play"));
connect(play, SIGNAL(clicked()), this, SLOT(play_clicked()));
pause = new QPushButton(this);
pause->hide();
pause->move(410, 25);
pause->resize(70, 25);
pause->setText(tr("Pause"));
connect(pause, SIGNAL(clicked()), this, SLOT(pause_clicked()));
pre = new QPushButton(this);
pre->hide();
pre->move(335, 85);
pre->resize(70, 25);
pre->setText(tr("Pre"));
connect(pre, SIGNAL(clicked()), this, SLOT(pre_clicked()));
next = new QPushButton(this);
next->hide();
next->move(410, 85);
next->resize(70, 25);
next->setText(tr("Next"));
connect(next, SIGNAL(clicked()), this, SLOT(next_clicked()));
de = new QPushButton(this);
de->hide();
de->move(370, 165);
de->resize(70, 25);
de->setText(tr("Delete"));
connect(de, SIGNAL(clicked()), this, SLOT(de_clicked()));
x2 = new QPushButton(this);
x2->hide();
x2->move(370, 125);
x2->resize(70, 25);
x2->setText(tr("x1"));
connect(x2, SIGNAL(clicked()), this, SLOT(x2_clicked()));
//动态生成标签
label = new QLabel(this);
label->move(100, 125);
label->setText("empty video");
label->hide();
time_label = new QLabel(this);
time_label->move(5, 5);
time_label->setStyleSheet("color:white");
catch_real_time(); //获取当前系统时间
count_label = new QLabel(this);
count_label->move(375, 5);
count_label->resize(100,20);
//申请存储空间
p_rgb = (unsigned char *)malloc(320 * 240 * 3 * sizeof(char));
pics_count = (long *)calloc(MAX_VIDEO, sizeof(long));
painter = new QPainter(this);
frame = new QImage(p_rgb, 320, 240, QImage::Format_RGB888);
play_dir = new QDir();
type_list << "*.jpg"; //后缀存入类型列表,用于目录文件读取
//遍历文件夹,将已存在的视频帧数量保存到pics_count数组中,并增加相应得统计
for (num = 0; num < MAX_VIDEO; num++)
{
get_dir();
if (name_list.count() != 0)
{
pics_count[num] = name_list.count();
video_count++;
}
}
//设置视频数量标签
count_label->setText("video: " + QString::number(video_count) + "/" + QString::number(MAX_VIDEO));
num = 0;
//获取摄像头设备
vd = new VideoDevice(tr("/dev/video0"));
connect(vd, SIGNAL(display_error(QString)), this, SLOT(display_error(QString)));
//开启设备,执行初始化
rs = vd->open_device();
if (-1 == rs)
{
QMessageBox::warning(this, tr("error"), tr("open /dev/dsp error"), QMessageBox::Yes);
vd->close_device();
}
rs = vd->init_device();
if (-1 == rs)
{
QMessageBox::warning(this, tr("error"), tr("init failed"), QMessageBox::Yes);
vd->close_device();
}
//开始视频采集
rs = vd->start_capturing();
if (-1 == rs)
{
QMessageBox::warning(this, tr("error"), tr("start capture failed"), QMessageBox::Yes);
vd->close_device();
}
if (-1 == rs)
{
QMessageBox::warning(this, tr("error"), tr("get frame failed"), QMessageBox::Yes);
vd->stop_capturing();
}
//设置定时器,绑定更新函数,用于更新绘图事件
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));
timer->start(100); //100毫秒一周期
//设置标题
setWindowTitle(tr("Monitor System"));
}
//析构函数
Monitor::~Monitor()
{
rs = vd->stop_capturing();
rs = vd->uninit_device();
rs = vd->close_device();
}
//每100毫秒触发一次绘图事件
void Monitor::paintEvent(QPaintEvent *)
{
//通过是否进入回放界面进行判断
if (!video_flag)
{
//获取帧数据并将二进制转换成图片
rs = vd->get_frame((void **)&p_yuv, &len);
convert_yuv_to_rgb_buffer(p_yuv, p_rgb, 320, 240);
frame->loadFromData((uchar *)p_rgb, 320 * 240 * 3 * sizeof(char));
catch_real_time();
//在屏幕上绘制捕获的画面
painter->begin(this);
painter->drawImage(5, 5, *frame);
painter->end();
//开启录制
if (record_flag)
{
//录制时保存图片
pic_save();
}
rs = vd->unget_frame(); //释放帧数据,即释放缓冲区
}
else
{
//进入回放界面或触发暂停按钮时,只绘制一帧图像
if (!play_flag)
{
QPainter pic_painter(this);
QString str = name_list.at(i); //获取图片名称
time_label->setText(str.left(19)); //截断系统时间字符串用于显示
pic_painter.drawPixmap(5, 5, QPixmap("./pic" + QString::number(num) + "/" + name_list.at(i))); //绘制图片
}
else
{
//录像回放
pic_load();
}
}
}
//出错处理
void Monitor::display_error(QString err)
{
QMessageBox::warning(this, tr("error"), err, QMessageBox::Yes);
}
//yuv格式转换为rgb格式
int Monitor::convert_yuv_to_rgb_buffer(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height)
{
unsigned int in, out = 0;
unsigned int pixel_16;
unsigned char pixel_24[3];
unsigned int pixel32;
int y0, u, y1, v;
for (in = 0; in < width * height * 2; in += 4)
{
pixel_16 =
yuv[in + 3] << 24 |
yuv[in + 2] << 16 |
yuv[in + 1] << 8 |
yuv[in + 0];
y0 = (pixel_16 & 0x000000ff);
u = (pixel_16 & 0x0000ff00) >> 8;
y1 = (pixel_16 & 0x00ff0000) >> 16;
v = (pixel_16 & 0xff000000) >> 24;
pixel32 = convert_yuv_to_rgb_pixel(y0, u, v);
pixel_24[0] = (pixel32 & 0x000000ff);
pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
rgb[out++] = pixel_24[0];
rgb[out++] = pixel_24[1];
rgb[out++] = pixel_24[2];
pixel32 = convert_yuv_to_rgb_pixel(y1, u, v);
pixel_24[0] = (pixel32 & 0x000000ff);
pixel_24[1] = (pixel32 & 0x0000ff00) >> 8;
pixel_24[2] = (pixel32 & 0x00ff0000) >> 16;
rgb[out++] = pixel_24[0];
rgb[out++] = pixel_24[1];
rgb[out++] = pixel_24[2];
}
return 0;
}
int Monitor::convert_yuv_to_rgb_pixel(int y, int u, int v)
{
unsigned int pixel32 = 0;
unsign
没有合适的资源?快使用搜索试试~ 我知道了~
Qt监控系统实习项目.zip
共19个文件
cpp:6个
ui:5个
h:5个
需积分: 0 2 下载量 153 浏览量
2023-12-31
12:19:54
上传
评论 1
收藏 73KB ZIP 举报
温馨提示
QT,C++使用技巧,实战应用开发小系统参考资料,源码参考。 详细介绍了一些Qt框架的各种功能和模块,以及如何使用Qt进行GUI开发、网络编程和跨平台应用开发等。 适用于初学者和有经验的开发者,能够帮助你快速上手Qt并掌握其高级特性。
资源推荐
资源详情
资源评论
收起资源包目录
Qt监控系统实习项目.zip (19个子文件)
new
keyboard.cpp 9KB
final_v2 151KB
login.cpp 4KB
monitor.h 3KB
monitor.cpp 17KB
final_v2.pro.user 13KB
register.cpp 4KB
keyboard.h 2KB
login.ui 7KB
main.cpp 168B
register.ui 5KB
register.h 1017B
videodevice.ui 392B
keyboard.ui 17KB
final_v2.pro 519B
videodevice.h 1KB
videodevice.cpp 8KB
monitor.ui 384B
login.h 1KB
共 19 条
- 1
资源评论
白话Learning
- 粉丝: 3173
- 资源: 2464
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功