#include "test_capture.h"
#include "ui_test_capture.h"
#include<QPushButton>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QPixmap>
test_capture::test_capture(QWidget *parent) :
QWidget(parent),
ui(new Ui::test_capture)
{
ui->setupUi(this);
Init();
}
test_capture::~test_capture()
{
delete ui;
}
void test_capture::Init()
{
this->setWindowTitle(QString::fromLocal8Bit("摄像头捕获"));
this->resize(600,400);
camera = new QCamera();
cameraviewfinder = new QCameraViewfinder();
cameraImageCapture = new QCameraImageCapture(camera);
QPushButton *captureBtn = new QPushButton(QString::fromLocal8Bit("捕获"));
QPushButton *saveBtn = new QPushButton(QString::fromLocal8Bit("保存"));
QPushButton *exitBtn = new QPushButton(QString::fromLocal8Bit("退出"));
displayLabel = new QLabel();
displayLabel->setFixedSize(150,150);
displayLabel->setScaledContents(true); //打开自动平衡收放图片,显示图像大小,自动调节为QLabel的大小
QVBoxLayout *rightLayout = new QVBoxLayout();
rightLayout->addWidget(displayLabel);
rightLayout->addStretch();
rightLayout->addWidget(captureBtn);
rightLayout->addWidget(saveBtn);
rightLayout->addWidget(exitBtn);
mainlayout = new QHBoxLayout();
mainlayout->addWidget(cameraviewfinder);
mainlayout->addLayout(rightLayout);
connect(captureBtn,SIGNAL(clicked()),this,SLOT(captureBtnResponded()));
connect(saveBtn,SIGNAL(clicked()),this,SLOT(saveBtnResponded()));
connect(exitBtn,SIGNAL(clicked()),this,SLOT(exitBtnResponded()));
connect(cameraImageCapture,SIGNAL(imageCaptured(int,QImage)),this,SLOT(cameraImageCaptured(int id,QImage image)));
cameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->setViewfinder(cameraviewfinder);
camera->start();
this->setLayout(mainlayout);
}
void test_capture::captureBtnResponded()
{
cameraImageCapture->capture();
}
void test_capture::saveBtnResponded()
{
const QPixmap *pximap = displayLabel->pixmap();
if(pximap)
{
pximap->save("F:\\a.jpg");
}
}
void test_capture::exitBtnResponded()
{
camera->stop();
this->close();
}
void test_capture::cameraImageCaptured(int id, QImage image)
{
displayLabel->setPixmap(QPixmap::fromImage(image));
}