1

我正在尝试检测屏幕中的多个 QR 码并使用 ARKit 在其上添加信息。

我使用 Vision API 进行多个 QR 码检测,它工作得很好,但是在一次测试多个 QR 码时,HitTestResults 始终为 nil。如果检测到 SINGLE QR 码,它与 hittest 一起工作正常,我添加节点及其位置,即 hittest.worldtransform 但是当我尝试点击测试多个检测到的 qr 码边界框时,它总是失败/变为 nil 。任何人都可以有同样的问题或帮助我解决这个问题吗?

我使用 Vision API 进行多个 QR 码检测,它工作得很好,但是在一次测试多个 QR 码时,HitTestResults 始终为 nil。如果检测到 SINGLE QR 码,它与 hittest 一起工作正常,我添加节点及其位置,即 hittest.worldtransform 但是当我尝试点击测试多个检测到的 qr 码边界框时,它总是失败/变为 nil 。

func startQrCodeDetection() {

    self.names.removeAll()

    // Create a Barcode Detection Request
    let request = VNDetectBarcodesRequest(completionHandler: self.requestHandler)

    request.symbologies = [.QR]
    self.qrRequests = [request]
}

func requestHandler(request: VNRequest, error: Error?) {
    // Get the result out of the results, if there are any

    if let results = request.results, let result = results.first as? VNBarcodeObservation {


        // Get the bounding box for the bar code and find the center


        DispatchQueue.main.async {
            for each in results {
                guard let observer = each as? VNBarcodeObservation else { return }

                 let name = observer.payloadStringValue

                var rect = observer.boundingBox
                // Flip coordinates
                rect = rect.applying(CGAffineTransform(scaleX: 1, y: -1))
                rect = rect.applying(CGAffineTransform(translationX: 0, y: 1))
                // Get center
                let center = CGPoint(x: rect.midX, y: rect.midY)
                print(name!)
                self.hitTestQrCode(center: center , result: name ?? "default" )
                self.processing = false
            }

        }
    } else {
        self.processing = false
    }
}


func hitTestQrCode(center: CGPoint , result : String) {

    if let hitTestResults = self.scnView?.hitTest(center, types: [.featurePoint] ) , let camera = session.currentFrame?.camera, case .normal = camera.trackingState , let hittestResult = hitTestResults.first {

        if hittestResult.distance < 0.8 {
            // Add a new anchor to the session

            let statusForName = names.contains(where: { (name) -> Bool in
                name == result
            })
            if statusForName {
                print("Already Loaded")
            } else {


                let detectedAnchor = ARAnchor(transform: hittestResult.worldTransform)
                self.detectedDataAnchor = detectedAnchor
              self.detectedDataAnchor!.setValue(result, forKeyPath:"name")
                self.session.add(anchor: detectedAnchor)



                names.append(result)

            }

        }
    }
}



//Adding the node after the QR is detected

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    let node = SCNNode()
    // If this is our anchor, create a node
    if self.detectedDataAnchor?.identifier == anchor.identifier {

        let name : String = self.detectedDataAnchor!.value(forKey: "name") as! String
        let plane = SCNPlane(width: 0.2, height: 0.2)
        plane.cornerRadius = plane.width / 8
        let skScene = SKScene(size: CGSize(width: 200, height: 200))
        skScene.backgroundColor = UIColor.white.withAlphaComponent(0.5)

        let rectangle = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 200, height: 200), cornerRadius: 10)
        rectangle.fillColor = #colorLiteral(red: 0.807843148708344, green: 0.0274509806185961, blue: 0.333333343267441, alpha: 1.0)
        rectangle.strokeColor = #colorLiteral(red: 0.439215689897537, green: 0.0117647061124444, blue: 0.192156866192818, alpha: 1.0)
        rectangle.lineWidth = 5
        rectangle.alpha = 0.4
        let labelNode = SKLabelNode(text: name)
        labelNode.fontSize = 30
        labelNode.run(SKAction.repeat(SKAction.rotate(byAngle: .pi, duration: 1), count: 1))
        labelNode.fontName = "San Fransisco"
        labelNode.position = CGPoint(x:100,y:100)
        skScene.addChild(rectangle)
        skScene.addChild(labelNode)
        let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(0, 1, 0), duration: 3))
        plane.firstMaterial?.isDoubleSided = true
        plane.firstMaterial?.diffuse.contents = skScene

        let planeNode = SCNNode(geometry: plane)
         planeNode.runAction(action)
        print(planeNode.position)

        planeNode.physicsBody?.isAffectedByGravity = false
        node.addChildNode(planeNode)

    }
    return node
}

我正在尝试查找如何使用 hitTest 处理多个 QR 码检测并在其上添加节点

4

0 回答 0