📄 videowidget.cpp
字号:
#define _cplusplus
#include "videowidget.h"
#include <QTime>
VideoWidget::VideoWidget(QWidget *parent)
: QWidget(parent)
{
v4l_open(DEFAULT_DEVICE, &v4l_dev) ;/*打开设备*/
v4l_get_capability(&v4l_dev);
v4l_get_picture(&v4l_dev);
v4l_init_mbuf(&v4l_dev);/*初始化设备*/
v4l_get_mbuf(&v4l_dev);/*内存映射*/
v4l_get_window(&v4l_dev);
imgLabel = new QLabel(this);
imgLabel->setGeometry(10,15,220,176);
imgLabel->setScaledContents(true);
infoLabel = new QLabel(this);
infoLabel->setGeometry(10,2,220,10);
infoLabel->setText(QString::number(v4l_dev.window.width)+"x"+QString::number(v4l_dev.window.height));
img = new QImage(MAX_WIDTH,MAX_HEIGHT,QImage::Format_RGB888);
exitButton = new QPushButton(tr("Quit"),this);
connect(exitButton, SIGNAL(clicked()),
qApp,SLOT(quit()));
exitButton->setGeometry(20,240,50,30);
saveButton = new QPushButton(tr("Save"),this);
connect(saveButton,SIGNAL(clicked()),
this,SLOT(sl_saveImage()));
saveButton->setGeometry(170,240,50,30);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(sl_start()));
timer->start(100);
#ifdef X86
resize(240,320);
#endif
}
VideoWidget::~VideoWidget()
{
}
void VideoWidget::sl_start()
{
int i=0;
QRgb point;
int r, g, b;
v4l_grab_movie(&v4l_dev);
qApp->processEvents();
if (v4l_dev.picture.depth == 12)
{
uchar *YUVin = v4l_dev.buffer;
v4l_yuv420p2rgb(RGBout, YUVin, MAX_WIDTH, MAX_HEIGHT,24);
for(int y=0; y<MAX_HEIGHT; y++)
{
for(int x=0; x<MAX_WIDTH; x++)
{
r = (int)RGBout[i+2];
g = (int)RGBout[i+1];
b = (int)RGBout[i];
point = qRgb(r,g,b);
img->setPixel(x,y,point);
i+=3;
}
}
}
else
{
for(int y=0; y<MAX_HEIGHT; y++)
{
for(int x=0; x<MAX_WIDTH; x++)
{
r = (int)v4l_dev.buffer[i+2];
g = (int)v4l_dev.buffer[i+1];
b = (int)v4l_dev.buffer[i];
point = qRgb(r,g,b);
img->setPixel(x,y,point);
i+=3;
}
}
}
imgLabel->setPixmap(QPixmap::fromImage(*img));
}
void VideoWidget::closeEvent(QCloseEvent *event)
{
if(timer->timerId())
{
killTimer(timer->timerId());
}
v4l_munmap(&v4l_dev) ;
v4l_close(&v4l_dev);
event->accept();
}
void VideoWidget::sl_saveImage()
{
QTime time;
timer->stop();
img->save(".tmp.bmp","BMP");
QImage image;
image.load(".tmp.bmp");
image.save(QString("image-%1%2%3.jpg")
.arg(time.currentTime().hour())
.arg(time.currentTime().minute())
.arg(time.currentTime().second())
,"JPEG");
int endTime = QTime::currentTime().second();
imgLabel->setText("saving...");
imgLabel->setAlignment(Qt::AlignCenter);
timer->start();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -