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 "CAM.h"
#include "ui_CAM.h"
#include <QGuiApplication>
#include <QScreen>
#include <QFile>
#include <QPixmap>
#include <QBuffer>
#include "camera.h"
CAM::CAM(QWidget *parent)
: QWidget(parent)
, ui(new Ui::CAM)
{
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()));
}
CAM::~CAM()
{
delete ui;
}
void CAM::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 CAM::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 CAM::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;
}
}