0

I am trying to place an object above a QR code in swift. I am able to detect the QR code however the location is wrong for the placement of the box. I don't really understand how the placement works. I know how to place objects down on planes. Do I need to relate the detected planes somehow with where it says it detects the QR code? Any information on how this SCNVector columns thing works would be appreciated as well haha. Also if CIDector is out dated and there is a new method.

Here is a snippet of detecting the QRCode and placing the box: var discoveredQRCodes = String

func session(_ session: ARSession, didUpdate frame: ARFrame) {
        
        //print("Updated")
        if time != 0.5 {
            return
        }
    
        DispatchQueue.global(qos: .background).async {

        let image = CIImage(cvPixelBuffer: frame.capturedImage)
        let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: nil)
        let features = detector!.features(in: image)

        
            
        for feature in features as! [CIQRCodeFeature] {
            if !self.discoveredQRCodes.contains(feature.messageString!) {
                self.discoveredQRCodes.append(feature.messageString!)
                let url = URL(string: feature.messageString!)
                let position = SCNVector3(frame.camera.transform.columns.3.x,
                                          frame.camera.transform.columns.3.y,
                                          frame.camera.transform.columns.3.z)
//                add3DModel(fromURL: url!, toPosition: getPositionBasedOnQRCode(frame: frame, position: "df"))
                print(position)
                print(url)
                
                DispatchQueue.main.async {
                let boxNode = SCNNode()
                boxNode.geometry = SCNBox(width: 0.04, height: 0.04, length: 0.04, chamferRadius: 0.002)
                boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.green
                boxNode.position = position
                boxNode.name = "node"
                self.arView.scene.rootNode.addChildNode(boxNode)
                }
                //add3dInstance(fromURL: url!, toPosition: position)
            }
        }
            
        }
            
        
}

Here is an image of the result:

enter image description here

Here is some debug output: SCNVector3(x: 0.023941405, y: 0.040143043, z: 0.056782123)

Brandan B
  • 464
  • 2
  • 6
  • 21

1 Answers1

0

First of all, you set up the boxNode position to the camera position. It's not what you want.

Secondly, any QR code detector provides 2d bounding box coordinates in the image space. To translate 2d coordinates to scene coordinates you need to find a ray from the camera to the QR code plane.

Please, check the code here.

Vladimir Vlasov
  • 1,860
  • 3
  • 25
  • 38