简体   繁体   English

使用QPainter显示实时摄像头

[英]Using QPainter to display live camera feed

I am just curious if we can use QPainter to display the live camera output. 我很好奇我们是否可以使用QPainter来显示实时摄像机输出。 If so can anyone tell me how will be the implementation. 如果可以,那么任何人都可以告诉我实施情况。 I already used it with QLabel like this: 我已经像这样将它与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();

I'm not sure how you propose to use the QPainter so I can't really comment on that. 我不确定您打算如何使用QPainter所以我不能对此发表评论。

However, one option might be to repackage the QCameraViewfinder widget up in a QGraphicsView and corresponding QGraphicsScene and rotate the item within the scene. 但是,一种选择是将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();

Note that there will probably be performance implications due to painting via the proxy. 请注意,由于通过代理进行绘画,可能会影响性能。 This method also assumes that the QCameraViewfinder uses Qt to perform its painting logic rather than something more direct such as OpenGL -- otherwise it won't work as expected. 此方法还假定QCameraViewfinder使用Qt执行其绘制逻辑,而不是使用诸如OpenGL之类的更直接的方法-否则它将无法按预期工作。

The above logic could probably be packaged up as something fairly self-contained such as... 上面的逻辑可能可以打包为相当独立的东西,例如...

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;
};

Then use as... 然后用作...

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