qt 打开摄像头拍照 QCamera 拍照

原文 https://blog.csdn.net/qq_35703848/article/details/79713007

用OpenCV 也可以实现打开拍照功能,这里使用的是qt自动的类QCamera

vs+qt   加   multimedia  multimediawidgets 模块 

mingw 在pro 文件中添加

getview.h

#include <QWidget>
#include "ui_getview.h"
#include <QCamera>
#include <QMediaRecorder>
#include <QCameraViewfinder>
#include <QCameraImageCapture>


class Codeget : public QWidget
{
	Q_OBJECT

public:
	Codeget(QWidget *parent = Q_NULLPTR);
	~Codeget();
private:
	void closeEvent(QCloseEvent *event);
private slots:
	void savePicture();
	void displayImage(int, QImage image);
	void showTheCapure();                //QLabel 展示图片

private:
	Ui::Codeget ui;

	QCamera *m_Cam;
	QCameraViewfinder *m_viewFinder;
	QCameraImageCapture *m_imageCapture;//截图部件
	int m_statu;                        //判断是否打开摄像头
};

getview.cpp

#include "getview.h"
#include <QMessageBox>
#include <QFile>


Codeget::Codeget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	m_Cam = new QCamera;
	m_viewFinder = new QCameraViewfinder();
	m_imageCapture = new QCameraImageCapture(m_Cam);
	
	QObject::connect(ui.open_closeVedio, SIGNAL(clicked()), this, SLOT(showTheCapure()));
	QObject::connect(ui.capture, SIGNAL(clicked()), this, SLOT(savePicture()));
	QObject::connect(m_imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(displayImage(int, QImage)));

	m_imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
	m_statu = 0;
}

Codeget::~Codeget()
{
}

void Codeget::closeEvent(QCloseEvent * event)
{
	m_Cam->stop();
	qDebug() << "delete" << endl;
	delete m_imageCapture;
	delete m_viewFinder;
	delete m_Cam;
	remove("code.jpg");
}

void Codeget::savePicture()
{
	m_imageCapture->capture();
}

void Codeget::displayImage(int, QImage image)
{
	image = image.convertToFormat(QImage::Format_RGB888);
	QImage::Format format = image.format();
	qDebug() << (int)format;

	QPixmap tempixmap = QPixmap::fromImage(image);
	int with = ui.label->width();
	int height = ui.label->width();
	//QPixmap fitpixmap = tempixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);  // 饱满填充  
    QPixmap fitpixmap = tempixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放  
	ui.label->setPixmap(fitpixmap);

	const QPixmap *pixmap = ui.label->pixmap();
	if (pixmap)
	{
		pixmap->save("code.jpg");
		qDebug() << "save ok" << endl;
	}
}

void Codeget::showTheCapure()
{
	if (m_statu == 0)
	{
		m_Cam->setViewfinder(m_viewFinder);
		ui.gridLayout->addWidget(m_viewFinder);
		m_viewFinder->show();

		m_Cam->setCaptureMode(QCamera::CaptureStillImage);
		m_Cam->setViewfinder(m_viewFinder);
		m_Cam->start(); //启动摄像头
		m_statu = 1;
		ui.open_closeVedio->setText(QStringLiteral("关闭视频"));
	}
	else
	{
		m_Cam->stop();
		m_viewFinder->close();
		m_statu = 0;
		ui.open_closeVedio->setText(QStringLiteral("打开视频"));
		ui.label->clear();
	
		remove("code.jpg");
	}
}

杂项:

qt 界面布局调整  QGridLayout 大小

直接把QGridLayout 放在一个QWidget上、那么你QWidget多大,layout也就多大

https://bbs.csdn.net/topics/390155508

猜你喜欢

转载自blog.csdn.net/so_do_it/article/details/87872601