在pro文件里添加
QT += multimedia QT += multimediawidgets
如下图(第5、6行):
需要的头文件:
#include#include #include #include #include
在自己的头文件类定义成员(其实里面有一些没必要的操作是因为我一开始学艺不精导致的,但是写这篇笔记的时候又懒得改了,反正能用,道理也还是那个道理,没差的)
.hpp文件:
QCamera *myCamera; QCameraImageCapture *cp; QCameraViewfinder *vf; void save_pic(int id, const QImage &preview); void collect_image();
大致如下图:
在cpp文件该类的构造函数那里放个下面的代码(comboBox需要你自己去ui界面进行设置 )
.cpp文件:(相关<>头文件这里还是和上面一样)
//读取所有摄像头信息 QListinfos = QCameraInfo::availableCameras(); foreach(QCameraInfo info, infos) { qDebug()< comboBox->addItem(info.deviceName()); }
然后我这里还用了一个qDebug()输出,它的头文件为
#include
记得加上
大致如下图:
下面是我写这部分功能时用到的代码,主要是开启、关闭摄像头与截图的功能:
//ui界面里的一个pushButton,点击这个按钮打开摄像头
void student_infomation_management_interface::on_open_camera_pushButton_clicked()
{
//open_close_camera=1;
myCamera = new QCamera(ui->comboBox->currentText().toUtf8(), this);
//创建一个获取图片对象
cp = new QCameraImageCapture(myCamera);
//抓取图片的信号
connect(cp, &QCameraImageCapture::imageCaptured,this, &student_infomation_management_interface::save_pic);
#if 0
QVideoWidget *w = new QVideoWidget(ui->widget);
w->resize(ui->widget->size());
myCamera->setViewfinder(w);
w->show();
#else
vf = new QCameraViewfinder(ui->widget);
vf->resize(ui->widget->size());
myCamera->setViewfinder(vf);
vf->show();
#endif
myCamera->start();
}
//
void student_infomation_management_interface::save_pic(int id, const QImage &preview)
{
qDebug()<<"id:"<label->setText(image_number_show);
QPixmap mmp = QPixmap::fromImage(preview);
mmp = mmp.scaled(ui->image_show_label->size());
ui->image_show_label->setPixmap(mmp);
//mmp.load(mat_path_qstr);
}
//截图命令(string mat_front_path是“mat_”;string mat_suffix_path是“.jpg”)
void student_infomation_management_interface::on_collect_image_pushButton_clicked()
{
//image is saved on path
QString mat_front_path_qstr=QString::fromStdString(mat_front_path);
QString mat_suffix_path_qstr=QString::fromStdString(mat_suffix_path);
//mat_path_qstr=mat_front_path_qstr+id_lineEdit_qstr+mat_suffix_path_qstr;
mat_path_qstr=mat_front_path_qstr+id_lineEdit_qstr+mat_suffix_path_qstr;
qDebug()<<"mat_path_qstr:"<stop();
vf->close();
}
void student_infomation_management_interface::collect_image()
{
/
//抓取一帧数据
//mat_path_qstr
//注意:这里用相对路径的话,它会自动保存到默认的绝对路径,而不是自己设置的相对路径,可能是这里只能用绝对路径吧,本人才疏学浅,不是很清楚,欢迎大佬们指正
cp->capture(mat_path_qstr);
/
//将图片转化为二进制
}



