QT和Opencv都能操作摄像头,本人在ubuntu环境下使用QT5操作摄像头,发现如下报错:
正常来说QT操作摄像头是不需要额外的库的,为什么会找不到这些库呢,然后从/usr/lib/x86_64-linux-gnu中查看发现有类似名称的库
但是不存在 libpulse-mainloop-glib.so libpulse.so libglib-2.0.so名的库,所以想到就是因为库名不对,所以创建如下软连接:
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
创建后发现编译正常了:
目录
1.代码
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