QCamera绘图

QCamera上绘图

  • 目的
  • 效果
  • 实现方法

目的

QCamera上可显示摄像头,绘制图像、绘制矩形、鼠标点击到哪,图形移动到对应位置

效果

实现方法

  • 自定义myViewFinder类
    myViewFinder.h
#pragma once
#include 
#include 
#include 
#include 
#include 
class myViewFinder :
	public QCameraViewfinder
{
	Q_OBJECT
public:
	myViewFinder(QWidget *parent = nullptr);
	~myViewFinder();
	QPointF current_p;
	QPixmap pix;
	void paintEvent(QPaintEvent *) override;
	void mousePressEvent(QMouseEvent *ev) override;
};

myViewFinder.cpp

#include "myViewFinder.h"
myViewFinder::myViewFinder(QWidget *parent) :QCameraViewfinder(parent)
{
	current_p.setX(100);
	current_p.setY(100);

	pix = (QPixmap("./LuffyQ.png"));
}


myViewFinder::~myViewFinder()
{
}

void myViewFinder::paintEvent(QPaintEvent *e)
{
	QCameraViewfinder::paintEvent (e);
	QPainter painter(this);
	painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing | QPainter::TextAntialiasing);

	//绘制矩形
	painter.setPen(QPen(QColor(255, 0, 0), 1));
	painter.drawRect(current_p.x()-20, current_p.y()-20, 50, 50);

    //加载已有图像
	painter.drawPixmap(current_p, pix);
}

void myViewFinder::mousePressEvent(QMouseEvent *e)
{
	current_p = QPointF(e->pos().x()- pix.width()/2, e->pos().y() - pix.height() / 2);
	update();
}

你可能感兴趣的:(QT,QCamera)