步骤 1 配置设置摄像头在视图 didload 中调用此方法
func setUpCamera ()
{
sceneView.delegate = self
// Do any additional setup after loading the view.
let configuration = ARImageTrackingConfiguration()
// first see if there is a folder called "ARImages" Resource Group in our Assets Folder
if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) {
// if there is, set the images to track
configuration.trackingImages = trackedImages
// at any point in time, only 1 image will be tracked
configuration.maximumNumberOfTrackedImages = 8
}
// Run the view's session
sceneView.session.run(configuration,options: [.resetTracking, .removeExistingAnchors])
}
扫描图像时调用此方法
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
DispatchQueue.main.async {
self.btnFullScreen.isHidden = false
}
// if the anchor is not of type ARImageAnchor (which means image is not detected), just return
guard let imageAnchor = anchor as? ARImageAnchor,
let fileUrlString = Bundle.main.path(forResource: "videoplayback", ofType: "mp4") else {return}
//find our video file
let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString))
player = AVPlayer(playerItem: videoItem)
//initialize video node with avplayer
let videoNode = SKVideoNode(avPlayer: player)
player.play()
// add observer when our player.currentItem finishes player, then start playing from the beginning
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in
self.player.seek(to: CMTime.zero)
self.player.pause()
print("Looping Video")
}
// set the size (just a rough one will do)
let videoScene = SKScene(size: CGSize(width: 480, height: 360))
// center our video to the size of our video scene
videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)
// invert our video so it does not look upside down
videoNode.yScale = -1.0
// add the video to our scene
videoScene.addChild(videoNode)
// create a plan that has the same real world height and width as our detected image
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
// set the first materials content to be our video scene
plane.firstMaterial?.diffuse.contents = videoScene
// create a node out of the plane
let planeNode = SCNNode(geometry: plane)
// since the created node will be vertical, rotate it along the x axis to have it be horizontal or parallel to our detected image
planeNode.eulerAngles.x = -Float.pi / 2
// finally add the plane node (which contains the video node) to the added node
node.addChildNode(planeNode)
}
这对我来说就像魅力一样工作让我知道如果你发现任何困难我会帮助你
这种技术称为 ARkitimage 跟踪...