簡體   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