代码下载
https://download.csdn.net/download/u010168781/10373174
头文件
#ifndef CAMERATEST_H
#define CAMERATEST_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QKeyEvent>
#include <QTimer>
namespace Ui {
class CameraTest;
}
class QCamera;
class QCameraImageCapture;
class QGraphicsVideoItem;
class QGraphicsScene;
class QGraphicsView;
class QGraphicsTextItem;
class CameraTest : public QMainWindow
{
Q_OBJECT
public:
explicit CameraTest(QWidget *parent = 0);
~CameraTest();
private:
Ui::CameraTest *ui;
private slots:
void slotExitBtn();
void slotImageCaptureBtn(int, QImage image);
void slotTimer();
private:
QCamera* myCamera;
QCameraImageCapture* myImageCapture;
QGraphicsVideoItem* myGraphicsVideoItem;
QGraphicsScene* myGraphicsScene;
QGraphicsView* myGraphicsView;
QGraphicsTextItem* myGraphicsTextItem;
QTimer* myTimer;
bool showText;
};
#endif // CAMERATEST_H
主程序
#include "CameraTest.h"
#include "ui_CameraTest.h"
#include <QLayout>
#include <QLabel>
#include <QPushButton>
#include <QFileDialog>
#include <QDebug>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QCameraInfo>
#include <QGraphicsVideoItem>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
CameraTest::CameraTest(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CameraTest)
{
ui->setupUi(this);
setWindowTitle("QCamera");
resize(800, 600);
myGraphicsVideoItem = new QGraphicsVideoItem(this);
myGraphicsVideoItem->setSize(QSizeF(640,480));
myGraphicsVideoItem->setPos(0,0);
myGraphicsTextItem = new QGraphicsTextItem(this);
myGraphicsTextItem->setDefaultTextColor(QColor("red"));
myGraphicsTextItem->setPlainText("hello world");
myGraphicsScene = new QGraphicsScene(this);
myGraphicsScene->addItem(myGraphicsVideoItem);
myGraphicsScene->addItem(myGraphicsTextItem);
myGraphicsView = new QGraphicsView(this);
myGraphicsView->setScene(myGraphicsScene);
myCamera = new QCamera(this);
myImageCapture = new QCameraImageCapture(myCamera);
myImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
myCamera->setCaptureMode(QCamera::CaptureStillImage);
myCamera->setViewfinder(myGraphicsVideoItem);
QPushButton* capBtn = new QPushButton("截图",this);
QPushButton* exitBtn = new QPushButton("退出",this);
connect(capBtn, SIGNAL(clicked()), myImageCapture, SLOT(capture()));
connect(exitBtn, SIGNAL(clicked()), this, SLOT(slotExitBtn()));
connect(myImageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(slotImageCaptureBtn(int,QImage)));
QVBoxLayout *mainLayout = new QVBoxLayout(ui->centralWidget);
mainLayout->addWidget(myGraphicsView);
mainLayout->addWidget(capBtn);
mainLayout->addWidget(exitBtn);
//在此处start,没有反应,不显示摄像头内容
//m_pCamera->start();
myTimer = new QTimer(this);
//触发时间100ms,需要根据自己的情况调整,我的测试是在少于50ms触发,不显示摄像头
myTimer->singleShot(100,this,SLOT(slotTimer()));
}
CameraTest::~CameraTest()
{
delete ui;
}
void CameraTest::slotExitBtn()
{
myCamera->stop();
close();
}
void CameraTest::slotImageCaptureBtn(int, QImage image)
{
QString savepath = QFileDialog::getSaveFileName(this,"Save Capture","Capture","Image png(*.png);;Image jpg(*.jpg);;Image bmp(*.bmp)");
if(!savepath.isEmpty()){
image.save(savepath);
}
}
void CameraTest::slotTimer()
{
myCamera->start();
}