QCamera:系统摄像设备(摄像头)
QCameraViewfinder:摄像取景器部件
QCameraImageCapture:截图部件
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QCameraInfo>
#include <QList>
#include <QDebug>
#include <QImage>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void ShowTheCapture();
void SavePicture();
void displayImage(int,QImage image);
private:
Ui::Widget *ui;
public:
QCamera *camera;//摄像头
QCameraViewfinder * viewfinder;//摄像头取景器部件
QCameraImageCapture * imageCapture;//截图部件
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<Qdebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
camera=new QCamera;//摄像头
viewfinder=new QCameraViewfinder(this);
imageCapture=new QCameraImageCapture(camera);//截图部件
camera->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);
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->setViewfinder(viewfinder);
camera->start(); //启动摄像头
}
Widget::~Widget()
{
delete ui;
}
void Widget::ShowTheCapture()
{
imageCapture->capture();
}
void Widget::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 Widget::SavePicture()
{
const QPixmap *pixmap = ui->label->pixmap();
if(pixmap)
{
pixmap->save("E:\\hallo.jpg");
qDebug()<<"保存上了"<<endl;
}
}