Sunday, October 2, 2022
HomeiOS Developmentios - ARKit add AR Reference Photos from url by scanning QR...

ios – ARKit add AR Reference Photos from url by scanning QR code


My app is about AR Picture Monitoring. I wish to obtain Reference photographs to trace by scanning QR code. I’ve carried out: downloading photographs by url and including it into reference photographs, however I wish to obtain photographs after QR code scanned (the place the url is positioned).
I’ve tried to make use of ARSCNView with AVCaptureSession to detect QR code. However my app FREEZED after including capturesession.
May you assist? What am I doing flawed? Please recommendation learn how to do it in correct method?

    sceneView.delegate = self
    let scene = SCNScene(named: "artwork.scnassets/ship.scn")!
    sceneView.scene = scene

    let configuration = ARImageTrackingConfiguration()
    let arImages = newReferenceImages
    configuration.trackingImages = arImages
    sceneView.session.run(configuration)

AR photographs detecting

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) 
  {
    guard anchor is ARImageAnchor else { return }
    guard let referenceImage = ((anchor as? ARImageAnchor)?.referenceImage) else { 
    return }
    
    guard let container = sceneView.scene.rootNode.childNode(withName: "container", 
    recursively: false) else { return }
    container.removeFromParentNode()
    node.addChildNode(container)
    container.isHidden = false
    
    guard let id = referenceImage.title?.break up(separator: "-")[1].description else { 
    return }
    CacheManager.shared.getPathForFiLe(title: "video-" + id) { [weak self] end in
        guard let `self` = self else { return }
        self.videoURL = URL(string: outcome)
    }
    
    guard let videoURL = self.videoURL else { return }
    videoPlayer = AVPlayer(url: videoURL)
    let videoScene = SKScene(dimension: CGSize(width: 720.0, top: 1280.0))
    videoNode = SKVideoNode(avPlayer: videoPlayer)
    videoNode.place = CGPoint(x: videoScene.dimension.width/2, y: videoScene.dimension.top/2)
    videoNode.dimension = videoScene.dimension
    videoNode.yScale = -1
    videoNode.play()
    videoScene.addChild(videoNode)
    
    guard let video = container.childNode(withName: "video", recursively: true) else { return }
    video.geometry?.firstMaterial?.diffuse.contents = videoScene
    video.scale = SCNVector3(x: Float(referenceImage.physicalSize.width), y: Float(referenceImage.physicalSize.top), z: 1.0)
    video.place = node.place
    // For Animation
    guard let videoContainer = container.childNode(withName: "videoContainer", recursively: false) else { return }
    videoContainer.runAction(SCNAction.sequence([SCNAction.wait(duration: 1.0), SCNAction.scale(to: 1.0, duration: 0.5)]))
}

For QR code scanning

 func configureQRsession() {
    captureSession = AVCaptureSession()
    guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
    let videoInput: AVCaptureDeviceInput

    do {
        videoInput = strive AVCaptureDeviceInput(machine: videoCaptureDevice)
    } catch {
        return
    }

    if (captureSession.canAddInput(videoInput)) {
        captureSession.addInput(videoInput)
    } else {
        failed()
        return
    }

    let metadataOutput = AVCaptureMetadataOutput()

    if (captureSession.canAddOutput(metadataOutput)) {
        captureSession.addOutput(metadataOutput)

        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.fundamental)
        metadataOutput.metadataObjectTypes = [.qr]
    } else {
        failed()
        return
    }

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.body = view.layer.bounds
    previewLayer.videoGravity = .resizeAspectFill
    view.layer.addSublayer(previewLayer)

    captureSession.startRunning()
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments