您实际上可以创建两个不同的 QCamera 实例,并为它们设置两个不同的取景器,但您将无法启动相机两次(即您最终会遇到某种设备繁忙错误)。
如果您只需要一个简单的取景器实现,您可以QAbstractVideoSurface
通过信号对视频帧进行子类化和转发,这样:
共享取景器.h
#include <QAbstractVideoSurface>
#include <QPixmap>
class SharedViewfinder : public QAbstractVideoSurface
{
Q_OBJECT
public:
SharedViewfinder();
QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const;
bool present(const QVideoFrame &frame);
signals:
void frameReady(QPixmap);
};
共享取景器.cpp
#include "sharedviewfinder.h"
SharedViewfinder::SharedViewfinder() : QAbstractVideoSurface(nullptr){}
QList<QVideoFrame::PixelFormat> SharedViewfinder::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
QList<QVideoFrame::PixelFormat> list;
if (handleType == QAbstractVideoBuffer::NoHandle)
{
list.append(QVideoFrame::Format_RGB32);
}
return list;
}
bool SharedViewfinder::present(const QVideoFrame &frame)
{
QVideoFrame copy(frame);
copy.map(QAbstractVideoBuffer::ReadOnly);
QImage image(copy.bits(), copy.width(), copy.height(), copy.bytesPerLine(), QImage::Format_RGB32);
copy.unmap();
emit frameReady(QPixmap::fromImage(image));
return true;
}
要显示转发的帧,在您选择的小部件中,有一个QLabel
和一个槽,如下所示:
void Widget::frameReady(QPixmap pixmap)
{
label->setPixmap(pixmap);
label->update();
}
您现在可以将共享取景器设置为相机对象,并将其与多个视图连接:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SharedViewfinder viewfinder;
Widget mirror1;
Widget mirror2;
QObject::connect(&viewfinder, &SharedViewfinder::frameReady, &mirror1, &Widget::frameReady);
QObject::connect(&viewfinder, &SharedViewfinder::frameReady, &mirror2, &Widget::frameReady);
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
QCamera camera(cameras[0]);
camera.setViewfinder(&viewfinder);
mirror1.move(0, 0);
mirror1.show();
mirror2.move(1920, 0);
mirror2.show();
camera.start();
return a.exec();
}
我希望这是开箱即用的,无论如何您可能想要更改我设置为 RGB32 的颜色格式。另外,请注意我移动视图以在我拥有的两个屏幕上显示它们。我提供的示例代码已在 Ubuntu 16.10 Asus 笔记本电脑上成功测试(但以非常浅薄的方式)。