Qt调用电脑摄像头写一个相机小程序


前言

今天我们来使用Qt写一个相机小程序。

一、相机里面的主要功能介绍

在这里插入图片描述

  1. 这张是一开始运行的界面,有一个窗口显示摄像头捕捉的画面,还有两个按钮控件,其实左下角还有一个label控件(下张图片可见具体位置)。
  2. 当我们开始点击拍照按钮,此时就会使用label控件来显示一个小的照片。具体如下:
    在这里插入图片描述
  3. 左下角显示的照片就是使用label控件来承载的,这时我们再使用鼠标点击label控件(即左下角的小图片), 这是会创建一个新的窗口, 显示我们刚刚拍的照片。具体如下:
  4. 第3步的时候我们点击保存按钮的话就会弹出一个保存文件的窗口,具体如下:
    在这里插入图片描述
  5. 就是以上的几点小功能了

二、代码实现

1.工程目录结构

在这里插入图片描述
clicklabel.h和clicklabel.cpp:
这两个文件主要实现了label的点击功能,类似于使label也有按钮的功能。

mainwindow.h:
这里面主要定义了许多变量,方便实现信号与槽的功能。

mainwindow.cpp:
这里面主要实现了调用摄像头,显示画面,拍照,保存照片等功能。具体大家可以参考下面代码。

mainwindow.ui
这里面主要是界面布局。

2.各文件代码:

clicklabel.h:

#ifndef CLICKLABEL_H
#define CLICKLABEL_H

#include <QWidget>
#include <QLabel>
#include <QDebug>
#include <QMouseEvent>

class ClickLabel : public QLabel
{
    Q_OBJECT

public:
    ClickLabel(QWidget * parent = 0) : QLabel(parent){}
    ~ClickLabel(){}

public:
    signals:
        void clicked1(ClickLabel * click);

protected:
    void mousePressEvent(QMouseEvent *e);
};

#endif // CLICKLABEL_H


clicklabel.cpp:

#include "clicklabel.h"

void ClickLabel::mousePressEvent(QMouseEvent *)
{
    emit clicked1(this);     //发送自定义信号
}


mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QImage>
#include <QLabel>
#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QString fileName;
    QImage image;
    QWidget * parent;
    char newFlag;
    QLabel * label;

private slots:
    void displayImage(int, QImage);
    void on_capture_clicked();
    void on_pushButton_clicked();
    void showPhoto();

private:
    Ui::MainWindow *ui;
    QCameraImageCapture * imageCapture;
    QCameraViewfinder * viewfinder;
    QCamera * myCamera;
};
#endif // MAINWINDOW_H


mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "clicklabel.h"
#include <QFileDialog>  //文件对话框

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    newFlag = 1;
    this->parent = parent;
    myCamera = new QCamera(this);           //创建相机对象
    viewfinder = new QCameraViewfinder(this);       //创建一个取景器
    imageCapture = new QCameraImageCapture(myCamera, this);         //创建图片捕获对象

    myCamera->setViewfinder(viewfinder);        //绑定相机和取景器

    ui->ImageView->addWidget(viewfinder);

    myCamera->start();

    connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(displayImage(int, QImage)));

    connect(ui->PhotoShow, SIGNAL(clicked1(ClickLabel *)), this, SLOT(showPhoto()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::displayImage(int, QImage image)
{
    ui->PhotoShow->setPixmap(QPixmap::fromImage(image));        //使用label控件来承载照片
    this->image = image;
}

void MainWindow::on_capture_clicked()       //拍照按钮
{
    imageCapture->capture();        //开始拍照(文件默认保存在图片)
}


void MainWindow::showPhoto()      //开新窗口显示拍的照片
{
    if(1 == newFlag)	//此条件保证多次点击label控件也只会创建一个窗口
    {
        MainWindow * wShow = new MainWindow(parent);
        wShow->setFixedSize(800, 480);
        wShow->show();       //设置一个新窗口显示拍好的照片
        label = new QLabel(wShow);
    }
    label->setFixedSize(800, 480);
    label->show();

    label->setPixmap(QPixmap::fromImage(image));
    newFlag = 0;
}

void MainWindow::on_pushButton_clicked()
{
    //使用文件对话框保存图片
    fileName = QFileDialog::getSaveFileName(this, "save file", QDir::homePath(), "*.jpg; *.*");
    if(fileName.isEmpty())
    {
        return;
    }
    //qDebug() << fileName;
}


main.cpp:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


总结

以上就是今天的小程序了。代码写的不是很优美。如果大家有什么效率更高的方法,可以指出来。

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个简单的使用PyQt5和OpenCV调用摄像头实时扫码的程序: ```python import cv2 import numpy as np from PyQt5 import QtWidgets, QtGui, QtCore class CameraWidget(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) # 创建一个定时器用于更新摄像头图像 self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.update_frame) self.timer.start(30) # 创建一个标签用于显示摄像头图像 self.image_label = QtWidgets.QLabel(self) # 创建一个垂直布局并将标签添加到布局中 vbox = QtWidgets.QVBoxLayout(self) vbox.addWidget(self.image_label) # 创建一个OpenCV摄像头对象 self.capture = cv2.VideoCapture(0) def update_frame(self): ret, frame = self.capture.read() if ret: # 将图像转换为Qt图像格式并在标签上显示 qt_image = self.convert_frame(frame) self.image_label.setPixmap(QtGui.QPixmap.fromImage(qt_image)) def convert_frame(self, frame): # 将OpenCV图像转换为RGB图像 rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 将RGB图像转换为Qt图像格式 height, width, channel = rgb_image.shape bytes_per_line = 3 * width qt_image = QtGui.QImage(rgb_image.data, width, height, bytes_per_line, QtGui.QImage.Format_RGB888) return qt_image class QRCodeScanner(QtWidgets.QMainWindow): def __init__(self, parent=None): super().__init__(parent) # 创建一个摄像头窗口部件并设置为CentralWidget self.camera_widget = CameraWidget(self) self.setCentralWidget(self.camera_widget) # 创建一个状态栏 self.statusBar().showMessage('Ready') # 设置窗口标题和大小 self.setWindowTitle('QR Code Scanner') self.setGeometry(100, 100, 640, 480) def closeEvent(self, event): # 关闭窗口时释放摄像头对象 self.camera_widget.capture.release() event.accept() if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) scanner = QRCodeScanner() scanner.show() sys.exit(app.exec_()) ``` 这个程序一个摄像头窗口部件添加到主窗口中,并使用定时器更新摄像头图像。它还将OpenCV图像转换为Qt图像格式,并在标签上显示。您可以使用Zbar库来扫描QR码。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值