Qt iOS:更改摄像头接收帧的格式

时间:2015-12-10 16:23:17

标签: ios qt ios-camera qtmultimedia

QVideoFilterRunnable::run方法收到的帧格式为QVideoFrame::Format_NV12,因此在使用glTexImage将其加载到GPU纹理之前,我需要先将其转换为BGRA。有没有办法改变相机的输出格式?

这是原始问题:

1 个答案:

答案 0 :(得分:0)

由于我使用的是QML,因此这是关于获取QML对象并在C ++代码中使用它的说明。我猜纯C ++的东西是一样的。

首先,您需要在QML源中突出显示相机对象:

Camera {
  id: camera
  objectName: "CameraObject"
}

获取QQuickView的根对象:

QQuickView view;

QQuickItem* root = view.rootObject();
assert(root != nullptr);

获取QML相机:

QObject* qmlCamera = root->findChild<QObject*>("CameraObject");
assert(qmlCamera != nullptr);

从QML相机对象获取C ++相机:

QCamera* camera = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));
assert(camera != nullptr);

找到您需要的格式:

QCameraViewfinderSettings bestSetting;
assert(bestSetting.isNull()); // sanity check

auto viewfinderSettings = camera->supportedViewfinderSettings();
for (auto i: viewfinderSettings) {
  if (i.pixelFormat() != QVideoFrame::Format_ARGB32) {
    // skip non-ARGB formats
    continue;
  }
  // check i.resolution()
  // several settings with Format_ARGB32 will be available
  // pick the one with resolution which fits best for you

  bestSetting = i;
}

检查找到的内容并应用设置:

assert(bestSetting.pixelFormat() == QVideoFrame::Format_ARGB32);
camera->setViewfinderSettings(bestSetting);

现在可以显示视图:

view.show();
return app.exec();