#include "mycamera.h"
#include "ui_mycamera.h"
#include <QDebug>
#include <QDir>
MyCamera::MyCamera(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MyCamera)
{
ui->setupUi(this);
//获取系统中可用的摄像头信息
QList<QCameraInfo> infos = QCameraInfo::availableCameras();
//显示到下拉列表
foreach(QCameraInfo info,infos){
qDebug()<<info.deviceName()<<info.description();
//把摄像头设备名加入列表
ui->comboBox->addItem(info.deviceName());
}
ui->pushButton_stop->setEnabled(false);
ui->pushButton_capture->setEnabled(false);
}
MyCamera::~MyCamera()
{
delete ui;
}
//开始
void MyCamera::on_pushButton_start_clicked()
{
//构造摄像头对象
ca = new QCamera(ui->comboBox->currentText().toUtf8(),this);
//构造截屏对象
cap = new QCameraImageCapture(ca,this);
//连接截屏信号和显示截屏图像的槽
QObject::connect(cap,&QCameraImageCapture::imageCaptured,this,&MyCamera::show_pic);
#if 0
//使用父类对象构造显示窗口类
w = new QVideoWidget(ui->widget);
#else
w = new QCameraViewfinder(ui->widget);
#endif
//调整大小一致
w->resize(ui->widget->size());
//设置摄像头的显示窗口
ca->setViewfinder(w);
w->show();
//启动摄像头
ca->start();
//禁止开始按钮
ui->pushButton_start->setEnabled(false);
ui->pushButton_stop->setEnabled(true);
ui->pushButton_capture->setEnabled(true);
}
//停止
void MyCamera::on_pushButton_stop_clicked()
{
//停止摄像头
ca->stop();
//隐藏显示窗口
w->hide();
//断开信号
QObject::disconnect(cap,&QCameraImageCapture::imageCaptured,this,&MyCamera::show_pic);
delete w;
delete cap;
delete ca;
ui->pushButton_start->setEnabled(true);
ui->pushButton_stop->setEnabled(false);
ui->pushButton_capture->setEnabled(false);
}
//截屏
void MyCamera::on_pushButton_capture_clicked()
{
cap->capture();
//不能立即显示图片
}
//显示截屏图片
void MyCamera::show_pic(int id, const QImage &preview)
{
qDebug()<<id;
//把QImage转换成QPixmap
QPixmap pix = QPixmap::fromImage(preview);
//保存图片
QString picpath = QString("./%1.jpg").arg(id);
pix.save(picpath);
//显示
pix = pix.scaled(ui->label->size());
ui->label->setPixmap(pix);
}
- 1
- 2
- 3
- 4
- 5
前往页