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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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代表路径文件名
* arg2保存的类型arg3代表保存的质量等级 */
saveImage.save(fileName, "PNG", 1);
/* 设置拍照的图像为显示在photoLabel上 */
ui->cam_albumBut->setPixmap(QPixmap::fromImage(QImage(fileName)));
qDebug()<<"保存完成!"<<endl;
}
}