【问题标题】:Using QPainter to display live camera feed使用 QPainter 显示实时摄像机源
【发布时间】:2017-04-10 12:46:51
【问题描述】:

我只是好奇我们是否可以使用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。基本上我想旋转相机的实时提要,但我可以使用 QPainter 小部件来实现它。

标签: c++ qt


【解决方案1】:

我不确定你打算如何使用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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多