camera.h
#ifndef CAMERA_H
#define CAMERA_H
#include <QWidget>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QCameraInfo>
#include <QList>
#include <QDebug>
#include <QImage>
namespace Ui {
class camera;
}
class camera : public QWidget
{
Q_OBJECT
public:
explicit camera(QWidget *parent = 0);
~camera();
private:
Ui::camera *ui;
public:
QCamera *mycamera;//摄像头
QCameraViewfinder * viewfinder;//摄像头取景器部件
QCameraImageCapture * imageCapture;//截图部件
public slots:
void ShowTheCapture(); //截图
void SavePicture();
void displayImage(int,QImage image);
void closeEvent(QCloseEvent *event);
};
#endif // CAMERA_H
camera.cpp
#include "camera.h"
#include "ui_camera.h"
camera::camera(QWidget *parent) :
QWidget(parent),
ui(new Ui::camera)
{
ui->setupUi(this);
mycamera=new QCamera;//摄像头
viewfinder=new QCameraViewfinder(ui->label_2);
imageCapture=new QCameraImageCapture(mycamera);//截图部件
mycamera->setViewfinder(viewfinder);
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ShowTheCapture()));
QObject::connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(SavePicture()));
QObject::connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));
imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
mycamera->setCaptureMode(QCamera::CaptureStillImage);
mycamera->setViewfinder(viewfinder);
mycamera->start(); //启动摄像头
}
camera::~camera()
{
delete ui;
}
void camera::ShowTheCapture()
{
imageCapture->capture();
}
void camera::SavePicture()
{
const QPixmap *pixmap = ui->label->pixmap();
if(pixmap)
{
pixmap->save("E:\\hallo.jpg");
qDebug()<<"保存上了"<<endl;
}
}
void camera::displayImage(int, QImage image)
{
//QImage image1(ui->label->x(),ui->label->y(),QImage::Format_RGB888);
// image=new QImage(220,220,QImage::Format_RGB888);
// image.setPixel(,Format_RGB888);
// QColor color(QImage::Format_RGB888);
// image.fill(color);
//QImage image2;
//QImage::Format format = image1.format();
//qDebug() << (int)format;
image=image.convertToFormat(QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(image));
QImage::Format format = image.format();
qDebug() << (int)format;
}
void camera::closeEvent(QCloseEvent *event)
{
mycamera->stop(); //启动摄像头
qDebug()<<"mycamera stop";
delete mycamera;
this->close();
}