QT calls the camera to take pictures

1. Add the module to the Qt pro file

QT + = multimedia multimediawidgets

This line of code is used to use Qt's multimedia module in the project. 

Qt Multimedia is an important module of Qt. It provides many C++ classes and QML modules for display and processing of multimedia content. It also provides some necessary APIs to access the recorder and camera.

Qt Multimedia Widgets provide simple and intuitive multimedia forms. When using it, you need to include the header files corresponding to these two modules. It is recommended to include relevant header files as needed. For the pro file of the Qt project, these two modules need to be added to the Qt pro file to import the module in the project.

2. Class libraries needed to process and operate camera-related functions

①Add header file

#include <QCamera> //General categories for managing cameras
    It is a base class for managing camera devices and their functionality.
#include <QCameraInfo> //Manage camera device table
    This class provides information about available camera devices, such as device name, device ID, etc.
#include <QCameraViewfinder> //Manage camera display area
    Used to manage the camera view finder, which is the area that displays the live camera feed.
#include <QCameraImageCapture> //Manage pictures
    Used to capture images on a camera device and manage the capture and saving of images.

#include <QDateTime> //Manage time
#include <QString> //Manage strings

These class libraries are usually used to build Qt applications with camera functions, such as video recording, picture capture, camera control, etc. By using these libraries, you can easily access, control, and interact with camera devices. Please note that in order to use these libraries, you need to first ensure that your Qt development environment has been configured correctly and supports the camera function.

②Create a camera object in private

private:
    QCamera *Camera; //Use this class, instantiate it, and receive it with a pointer
    QList<QCameraInfo> Infolist; //Used to save available cameras
    QCameraViewfinder *Viewfinder;
    QCameraImageCapture *ImageCapture;

3. Turn on the camera

1. Call the optional camera
//Get the currently available camera equipment
Infolist = QCameraInfo::availableCameras();
//Define it pointer (function: traverse the table of available camera devices)
QList<QCameraInfo>::iterator it;
//Loop operation and add available camera device names to the drop-down box
for(it = Infolist.begin(); it != Infolist.end(); it + + )
{
    //Add a new item in the cbb component (drop-down box) of ui, description() returns a string
    ui->cbb->addItem((*it).description());
}

2. Select the camera and use the QCamera class in the Qt library to initialize a camera object.
//Get the name of the camera being used (get the currently selected index through ui->cbb->currentIndex(), and then get the QCameraInfo object at the index through the At function)
QCameraInfo Infoname = Infolist.at(ui->cbb->currentIndex());
//Then use this QCameraInfo object to initialize a new QCamera object.
Camera = new QCamera(Infoname);

3. Camera display settings
//QCameraViewfinder is a class used to represent the camera preview window in the Qt framework
//Set the area displayed by the camera
Viewfinder = new QCameraViewfinder(ui->lb1);
Camera->setViewfinder(Viewfinder);
//Set the range of camera display
Viewfinder->resize(ui->lb1->width(),ui->lb1->height());

4. Start camera preparation
Viewfinder->show();
Camera->start();

4. Take photos

1. Save format setting of captured pictures
//Get the current time
QDateTime Time(QDateTime::currentDateTime());
//Convert the current time to text format
QString datetime = Time.toString("yyyy-mm-dd hh-mm-ss");
//Assembly file name (path + name)
QString fliename = QString("D:/picture/%1.jpg").arg(datetime);

2. Save the picture
//Used to save the image object (QImage) as a file with the given file name ('filename').
Image.save(filename);

3. Picture display settings
ui->lb2->setPixmap(QPixmap(fliename)); //Display pictures
ui->lb2->setScaledContents(true); //Adaptive image size

5. Connection

1. Capture images
//QCameraImageCapture is used to capture images on the camera device and manage the capture and saving of images.
ImageCapture = new QCameraImageCapture(Camera);
connect(ImageCapture, & amp;QCameraImageCapture::imageCaptured,this, & amp;MainWindow::tack_pic);

6.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCamera> //General categories for managing cameras
#include <QCameraInfo> //Manage camera identification table
#include <QCameraViewfinder> //Manage camera display area
#include <QCameraImageCapture> //Manage pictures

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pb_option_clicked();

    void on_pb_camera_clicked();

    void tack_pic(int id,const QImage & amp;Image);

private:
    Ui::MainWindow *ui;

    QCamera *Camera; //Use this class, instantiate it, and receive it with a pointer
    QList<QCameraInfo> Infolist; //Used to save available cameras
    QCameraViewfinder *Viewfinder;
    QCameraImageCapture *ImageCapture;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QCamera> //General categories for managing cameras
#include <QCameraInfo> //Manage camera device table
#include <QCameraViewfinder> //Manage camera display area
#include <QCameraImageCapture> //Manage pictures
#include <QDateTime> //Manage time
#include <QString> //Manage strings

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle("Take a photo");
    setFixedSize(1020, 690);

    //Get the currently available camera equipment
    Infolist = QCameraInfo::availableCameras();

    //Define it pointer, function: traverse the table of available camera devices
    QList<QCameraInfo>::iterator it;
    //Loop operation and add available camera device names to the drop-down box
    for(it = Infolist.begin(); it != Infolist.end(); it + + )
    {
        //Add a new item in the cbb component (drop-down box) of ui, description() returns a string
        ui->cbb->addItem((*it).description());
    }
}

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

void MainWindow::on_pb_option_clicked()
{
    //Get the name of the camera being used
    QCameraInfo Infoname = Infolist.at(ui->cbb->currentIndex());
    Camera = new QCamera(Infoname);

    Viewfinder = new QCameraViewfinder(ui->lb1);
    //Set the area displayed by the camera
    Camera->setViewfinder(Viewfinder);
    //Set the range of camera display
    Viewfinder->resize(ui->lb1->width(),ui->lb1->height());

    //The camera device starts working
    Viewfinder->show();
    Camera->start();

    ImageCapture = new QCameraImageCapture(Camera);
    connect(ImageCapture, & amp;QCameraImageCapture::imageCaptured,this, & amp;MainWindow::tack_pic);
}

void MainWindow::tack_pic(int id,const QImage & amp;Image)
{
    //Get the current time
    QDateTime Time(QDateTime::currentDateTime());
    //Convert the current time to text format
    QString datetime = Time.toString("yyyy-MM-dd-hh-mm-ss");
    //Assembly file name (path + name)
    QString filename = QString("E:/pic/%1.jpg").arg(datetime);

    //save Picture
    //Used to save the image object (QImage) as a file with the given file name ('filename').
    Image.save(filename);

    ui->lb2->setPixmap(QPixmap(filename)); //Display pictures
    ui->lb2->setScaledContents(true); //Adaptive image size
}

void MainWindow::on_pb_camera_clicked()
{
    ImageCapture->capture();
}