最近两天苦恼于QT界面中USB摄像头的显示,还好今天成功解决,给大家分享一下
QT界面如下:
项目结构如下(本项目命名为QC_test):
话不多说,工程中四个文件的源码如下:
QT += core gui
QT +=multimedia
QT +=multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QC_test
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include < QMainWindow >
#include < QCamera >
#include < QCameraViewfinder >
#include < QCameraImageCapture >
#include < QCameraInfo >
#include < QFileDialog >
#include < QDebug >
namespace Ui {
class MainWindow;
}
class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void captureImage();
void displayImage(int,QImage);
void saveImage();
private:
Ui::MainWindow *ui;
QCamera *camera;
QCameraViewfinder *viewfinder;
QCameraImageCapture *imageCapture;
};
#endif // MAINWINDOW_H
#include “mainwindow.h”
#include < QApplication >
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include “mainwindow.h”
#include “ui_mainwindow.h”
MainWindow::MainWindow(QWidget parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QList list;
list = QCameraInfo::availableCameras();
for (int i = 0; i < list.size(); i++)
{
qDebug() << list.at(i).deviceName(); //获取设备名称
qDebug() << list.at(i).description();
qDebug() << list.at(i).position();
qDebug() << “###############”;
}
camera=new QCamera("@device:pnp:\\?\usb#vid_0edc&pid_2076&mi_00#6&3455a94a&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global",this); //这是设备名称,注意!!!获取的设备名称中,\要改为\,要不然会和转义字符产生混淆,导致编译器报错,我这里已经做了更改
viewfinder=new QCameraViewfinder(this);
imageCapture=new QCameraImageCapture(camera);
ui->horizontalLayout_View->addWidget(viewfinder);
ui->label_Display->setScaledContents(true);
camera->setViewfinder(viewfinder);
camera->start();
connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));
connect(ui->pushButton_Capture, SIGNAL(clicked()), this, SLOT(captureImage())); //**
connect(ui->pushButton_Save, SIGNAL(clicked()), this, SLOT(saveImage())); //***
connect(ui->pushButton_Quit, SIGNAL(clicked()), qApp, SLOT(quit())); //***
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::captureImage()
{
ui->statusBar->showMessage(tr(“正在捕获图片”), 1000);
imageCapture->capture();
}
void MainWindow::displayImage(int , QImage image)
{
ui->label_Display->setPixmap(QPixmap::fromImage(image));
ui->statusBar->showMessage(tr(“捕获成功”), 5000);
}
void MainWindow::saveImage()
{
QString fileName=QFileDialog::getSaveFileName(this, tr(“保存到文件”), QDir::homePath(), tr("jpeg格式文件(.jpg)"));
if(fileName.isEmpty()) {
ui->statusBar->showMessage(tr(“保存操作已取消”), 5000);
return;
}
const QPixmap pixmap=ui->label_Display->pixmap();
if(pixmap) {
pixmap->save(fileName);
ui->statusBar->showMessage(tr(“保存成功”), 5000);
}
}
程序输出窗口中输出了两个相机设备的设备号 “@device:pnp:\?\usb#vid_0bda&pid_568a&mi_00#6&1ac3aa3a&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global” 和 “@device:pnp:\?\usb#vid_0edc&pid_2076&mi_00#6&3455a94a&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global” 第一个是我笔记本的系统摄像头,第二个是我连接的USB摄像头,在程序中我选择的是第二个,但是要注意,凡是出现\都要改成\,否则编译器会报错。
用QT驱动USB相机算是成功了,我原想使用QT+opencv,通过QLabel来刷新显示,但是没有成功,程序直接crashed,以后再试试吧,如果有人会的话,可以给我留言,感谢~