当前位置:   article > 正文

QT摄像头采集

QT摄像头采集

主界面为显示框,两个下拉框,一个是所有相机,一个是相机支持的分辨率

系统根据UI界面自动生成的部分不再描述,以下为其他部分源码

widget.h

  1. #include <QWidget>
  2. #include <QMouseEvent>
  3. class QCamera;
  4. class QCameraViewfinder;
  5. class QCameraImageCapture;
  6. class QCameraInfo;
  7. class QCameraViewfinderSettings;
  8. namespace Ui {
  9. class Widget;
  10. }
  11. class Widget : public QWidget
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit Widget(QWidget *parent = 0);
  16. ~Widget();
  17. private:
  18. Ui::Widget *ui;
  19. QCamera *camera; //相机
  20. QCameraViewfinder *viewfinder; //取景器
  21. QList<QCameraInfo> cameras; //相机列表
  22. QList<QSize> mResSize = {}; //分辨率列表
  23. };

main.cpp

  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QCamera>
  4. #include <QCameraInfo>
  5. #include <QCameraViewfinder>
  6. #include <QCameraImageCapture>
  7. #include <QList>
  8. #include <QFileDialog>
  9. #include <QDebug>
  10. Widget::Widget(QWidget *parent) :
  11. QWidget(parent),
  12. ui(new Ui::Widget)
  13. {
  14. ui->setupUi(this);
  15. cameras = QCameraInfo::availableCameras();
  16. if(cameras.count() > 0) {
  17. camera = new QCamera(cameras.at(0));
  18. }
  19. viewfinder = new QCameraViewfinder(this);
  20. camera->setViewfinder(viewfinder);
  21. viewfinder->resize(600, 350);
  22. camera->start();
  23. //添加初始化的相机选项
  24. for(const QCameraInfo &cameraInfo:cameras) {
  25. ui->comboBox_cam->addItem(cameraInfo.description());
  26. }
  27. ui->comboBox_cam->setCurrentIndex(0);
  28. //添加初始化相机的分辨率选项
  29. mResSize = camera->supportedViewfinderResolutions();
  30. for (const QSize &msize:mResSize) {
  31. ui->comboBox_format->addItem(QString::number(msize.width(), 10) + "*" + QString::number(msize.height(), 10));
  32. }
  33. ui->comboBox_format->setCurrentIndex(0);
  34. QCameraViewfinderSettings init;
  35. init.setResolution(mResSize[0]);
  36. camera->setViewfinderSettings(init);
  37. connect(ui->comboBox_cam,QOverload<int>::of(&QComboBox::activated),[=](int index){
  38. camera->stop();
  39. delete camera;
  40. camera=new QCamera(cameras[index]);
  41. camera->setViewfinder(viewfinder); //设置取景器
  42. camera->start();
  43. //删除前相机的所有分辨率内容
  44. for (int i = 0; i < mResSize.size(); i++) {
  45. ui->comboBox_format->removeItem(0);
  46. }
  47. mResSize.clear();
  48. mResSize = camera->supportedViewfinderResolutions();
  49. for (const QSize &msize:mResSize) {
  50. ui->comboBox_format->addItem(QString::number(msize.width(), 10) + "*" + QString::number(msize.height(), 10));
  51. }
  52. ui->comboBox_format->setCurrentIndex(0);
  53. QCameraViewfinderSettings reset;
  54. reset.setResolution(mResSize[0]);
  55. camera->setViewfinderSettings(reset);
  56. });
  57. connect(ui->comboBox_format, QOverload<int>::of(&QComboBox::activated), [=](int index){
  58. //重新设置摄像头参数
  59. QCameraViewfinderSettings reset;
  60. reset.setResolution(mResSize[index]);
  61. camera->setViewfinderSettings(reset);
  62. });
  63. }
  64. Widget::~Widget()
  65. {
  66. delete ui;
  67. }

运行效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/196828
推荐阅读
相关标签
  

闽ICP备14008679号