繁体   English   中英

使用QPainter显示实时摄像头

[英]Using QPainter to display live camera feed

我很好奇我们是否可以使用QPainter来显示实时摄像机输出。 如果可以,那么任何人都可以告诉我实施情况。 我已经像这样将它与QLabel一起使用了:

cM2 = new QCamera(this);
cV2 = new QCameraViewfinder(this);
cM2->setViewfinder(cV2);
cBox2 = new QVBoxLayout();
cBox2->addWidget(cV2);
ui->label_2->setLayout(cBox2);
cM2->start();

我不确定您打算如何使用QPainter所以我不能对此发表评论。

但是,一种选择是将QCameraViewfinder小部件重新包装在QGraphicsView和对应的QGraphicsScene然后在场景中旋转该项目。

QGraphicsView view;
QGraphicsScene scene;

/*
 * Add the view finder widget to the scene and keep hold of the
 * proxy returned.
 */
auto *proxy = scene.addWidget(my_camera_view_finder);

/*
 * Use the proxy to rotate the view finder by 90 degrees.
 */
proxy->setRotation(90.0);
view.setScene(&scene);
view.show();

请注意,由于通过代理进行绘画,可能会影响性能。 此方法还假定QCameraViewfinder使用Qt执行其绘制逻辑,而不是使用诸如OpenGL之类的更直接的方法-否则它将无法按预期工作。

上面的逻辑可能可以打包为相当独立的东西,例如...

class rotated_widget: public QGraphicsView {
  using super = QGraphicsView;
public:
  explicit rotated_widget (QWidget *parent = nullptr)
    : super(parent)
    , m_widget(nullptr)
    , m_proxy(nullptr)
    , m_degrees(0.0)
    {
      setScene(new QGraphicsScene);
    }
  virtual void set_widget (QWidget *widget)
    {
      scene()->clear();
      m_proxy = nullptr;
      if ((m_widget = widget)) {
        m_proxy = scene()->addWidget(m_widget);
        m_proxy->setRotation(m_degrees);
        fixup();
      }
    }
  virtual void set_angle (double degrees)
    {
      m_degrees = degrees;
      if (m_proxy) {
        m_proxy->setRotation(m_degrees);
        fixup();
      }
    }
  virtual QSize sizeHint () const override
    {
      return(sceneRect().size().toSize());
    }
private:
  void fixup ()
    {
      setSceneRect(scene()->itemsBoundingRect());
    }
  QWidget              *m_widget;
  QGraphicsProxyWidget *m_proxy;
  double                m_degrees;
};

然后用作...

cV2 = new QCameraViewfinder;
auto *rotated = new rotated_widget;
rotated->set_widget(cV2);
rotated->set_angle(90.0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM