You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

6 months ago
#include "widget.h"
#include "ui_widget.h"
#include <QGuiApplication>
#include <QScreen>
#include <QFile>
#include <QPixmap>
#include <QBuffer>
#include "camera.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
/* 摄像头 */
camera = new Camera(this);
scanCameraDevice();
/* 信号连接槽 */
connect(camera, SIGNAL(readyImage(QImage)),
this, SLOT(showImage(QImage)));
connect(ui->cam_traBut,SIGNAL(clicked(bool)),
camera, SLOT(cameraProcess(bool)));//开启摄像头
connect(ui->cam_playBut, SIGNAL(clicked()),
this, SLOT(saveImageToLocal()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::scanCameraDevice()
{
/* QFile文件指向/dev/video1 */
QFile file("/dev/video1");
file.setFileName("/dev/video1");
if (file.exists())
{
qDebug()<<"camera success" ;
camera->selectCameraDevice(1);//video1
}
else
{
qDebug()<<"camera cannot finded" ;
}
}
void Widget::showImage(const QImage &image)
{
/* 显示图像 */
ui->displayLabel->setPixmap(QPixmap::fromImage(image));
saveImage = image;
/* 判断图像是否为空,空则设置拍照按钮不可用 */
if (!saveImage.isNull())
ui->cam_playBut->setEnabled(true);
else
ui->cam_playBut->setEnabled(false);
}
void Widget::saveImageToLocal()
{
/* 判断图像是否为空 */
if (!saveImage.isNull()) {
QString fileName =
QCoreApplication::applicationDirPath() + "/test.png";
qDebug()<<"正在保存"<<fileName<<"图片,请稍候..."<<endl;
/* save(arg1arg2arg3)重载函数arg1代表路径文件名
* arg2arg3 */
saveImage.save(fileName, "PNG", 1);
/* 设置拍照的图像为显示在photoLabel上 */
ui->cam_albumBut->setPixmap(QPixmap::fromImage(QImage(fileName)));
qDebug()<<"保存完成!"<<endl;
}
}