2

从 ARKit3 开始,可以运行同时支持后置和前置摄像头的 ARSession()。

例如,这会为支持 Worldtracking 的前置摄像头创建一个 ARConfiguration。

    // session for the front camera
    let configuration = ARFaceTrackingConfiguration()
    configuration.isWorldTrackingEnabled
    sceneView.session.run(configuration)

此示例为启用人脸跟踪的后置摄像头会话创建配置:

    // session for the back camera
    let configuration = ARWorldTrackingConfiguration()
    configuration.userFaceTrackingEnabled = true
    sceneView.session.run(configuration)

我想创建 2 个同时运行的独立 ARConfigurations 和 ARSessions。像这样:

在此处输入图像描述

到目前为止,我尝试了这段代码:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

// back camera view
@IBOutlet var backView: ARSCNView!

// front camera view
@IBOutlet weak var frontView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the view's delegate
    sceneView.delegate = self
    frontView.delegate = self

    // Create a new scene for back camera
    let scene = SCNScene(named: "art.scnassets/ship.scn")!
    backView.scene = scene
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // session for the back camera
    let configuration = ARWorldTrackingConfiguration()
    configuration.userFaceTrackingEnabled = true
    backView.session.run(configuration)

    // session for the front camera
    guard ARFaceTrackingConfiguration.isSupported else { return }
    let configurationFront = ARFaceTrackingConfiguration()
    configurationFront.isLightEstimationEnabled = true
    frontView.session.run(configurationFront, options: [])
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Pause the view's session
    sceneView.session.pause()
}

func session(_ session: ARSession, didFailWithError error: Error) {
    // Present an error message to the user
}

func sessionWasInterrupted(_ session: ARSession) {
    // Inform the user that the session has been interrupted, for example, by presenting an overlay
}

func sessionInterruptionEnded(_ session: ARSession) {
    // Reset tracking and/or remove existing anchors if consistent tracking is required
}
}

当前置摄像头正常工作时,后置摄像头会停止视频输入。有没有机会解决这个问题?

在另一台相机上运行一个 ARSession 和一个低级视频捕获会话也是一种解决方案,但我遇到了同样的问题。

4

0 回答 0