#include "widget.h" #include "ui_widget.h" #include #include #include #include #include #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()<<"正在保存"<cam_albumBut->setPixmap(QPixmap::fromImage(QImage(fileName))); qDebug()<<"保存完成!"<