Both QT and Opencv can operate the camera. I used QT5 to operate the camera in the ubuntu environment and found the following error:
Normally, QT does not require additional libraries to operate the camera. Why can't these libraries be found? Then I checked /usr/lib/x86_64-linux-gnu and found libraries with similar names.
But there is no library named libpulse-mainloop-glib.so libpulse.so libglib-2.0.so, so I thought it was because the library name was wrong, so I created the following soft connection:
sudo ln -s /usr/lib/x86_64-linux-gnu/libpulse-mainloop-glib.so.0.0.5 /usr/lib/x86_64-linux-gnu/libpulse-mainloop-glib.so
sudo ln -s /usr /lib/x86_64-linux-gnu/libpulse-simple.so.0.1.1 /usr/lib/x86_64-linux-gnu/libpulse.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libglib-2.0 .so.0.5600.4 /usr/lib/x86_64-linux-gnu/libglib-2.0.so
After creation, I found that the compilation was normal:
Table of contents
1.Code
main.cpp
#include <QApplication>
#include <QDebug>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QImage>
#include <QPixmap>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
//a.setStyleSheet("QCameraViewfinder, QLabel { border: 2px dashed grey;}");
a.setStyleSheet("QPushButton { color: red; background-color: blue }");
// 摄像头对象
QCamera *camera = new QCamera();
// 用于实时显示摄像头图像
QCameraViewfinder *viewfinder = new QCameraViewfinder();
// 用于截取摄像头图像
QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);
// camera 使用 viewfinder 实时显示图像
camera->setViewfinder(viewfinder);
// 使 viewfinder 能够使用 QSS
viewfinder->setAttribute(Qt::WA_StyledBackground, true);
// 拍照预览的 label
QLabel *previewLabel = new QLabel("");
// 点击拍照的按钮
QPushButton *captureButton = new QPushButton("拍照");
viewfinder->setFixedHeight(300);
previewLabel->setFixedHeight(300);
previewLabel->setMinimumWidth(300);
previewLabel->setAlignment(Qt::AlignCenter);
// 布局 widgets
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(viewfinder);
layout->addWidget(previewLabel);
layout->addWidget(captureButton);
layout->addStretch();
QWidget *window = new QWidget();
window->setLayout(layout);
window->show();
camera->start();
// 点击拍照
QObject::connect(captureButton, &QPushButton::clicked, [=] {
imageCapture->capture("capture.jpg"); // 如果不传入截图文件的路径,则会使用默认的路径,每次截图生成一个图片
});
// 拍照完成后的槽函数,可以把 image 保存到自己想要的位置
QObject::connect(imageCapture, &QCameraImageCapture::imageCaptured, [=](int id, const QImage &image) {
Q_UNUSED(id)
QImage scaledImage = image.scaled(previewLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
previewLabel->setPixmap(QPixmap::fromImage(scaledImage));
});
return a.exec();
}
use_camera.pro
#-------------------------------------------------
#
# Project created by QtCreator 2023-09-04T00:18:55
#
#-------------------------------------------------
QT += core gui
QT += multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = use_camera
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp